Advertisement
DOGGYWOOF

DGYOS Bitlocker

Oct 5th, 2024 (edited)
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 KB | None | 0 0
  1. -- XOR Encryption/Decryption Functions
  2. local function xorEncrypt(data, key)
  3.     local result = {}
  4.     for i = 1, #data do
  5.         local dataByte = string.byte(data, i)
  6.         local keyByte = string.byte(key, (i - 1) % #key + 1)
  7.         result[i] = string.char(bit.bxor(dataByte, keyByte))
  8.     end
  9.     return table.concat(result)
  10. end
  11.  
  12. local function createEncryptionKey(password, keyPath)
  13.     local encryptionKey = "my_secret_key"
  14.     local encryptedPassword = xorEncrypt(password, encryptionKey)
  15.    
  16.     local file = fs.open(keyPath, "w")
  17.     file.write(encryptionKey .. ":" .. encryptedPassword)
  18.     file.close()
  19. end
  20.  
  21. local function encryptDirectory(directory, keyPath)
  22.     local file = fs.open(keyPath, "r")
  23.     local content = file.readAll()
  24.     file.close()
  25.  
  26.     local encryptionKey, _ = content:match("([^:]+):([^:]+)")
  27.    
  28.     local function encryptFile(path)
  29.         local handle = fs.open(path, "r")
  30.         local data = handle.readAll()
  31.         handle.close()
  32.  
  33.         local encryptedData = xorEncrypt(data, encryptionKey)
  34.        
  35.         handle = fs.open(path, "w")
  36.         handle.write(encryptedData)
  37.         handle.close()
  38.     end
  39.    
  40.     local files = fs.list(directory)
  41.     for _, file in ipairs(files) do
  42.         local filePath = fs.combine(directory, file)
  43.         if not fs.isDir(filePath) then
  44.             encryptFile(filePath)
  45.         end
  46.     end
  47. end
  48.  
  49. local function decryptDirectory(directory, keyPath, password)
  50.     local file = fs.open(keyPath, "r")
  51.     local content = file.readAll()
  52.     file.close()
  53.  
  54.     local encryptionKey, encryptedPassword = content:match("([^:]+):([^:]+)")
  55.    
  56.     local decryptedPassword = xorEncrypt(encryptedPassword, encryptionKey)
  57.    
  58.     if password ~= decryptedPassword then
  59.         return false
  60.     end
  61.  
  62.     local function decryptFile(path)
  63.         local handle = fs.open(path, "r")
  64.         local data = handle.readAll()
  65.         handle.close()
  66.  
  67.         local decryptedData = xorEncrypt(data, encryptionKey)
  68.        
  69.         handle = fs.open(path, "w")
  70.         handle.write(decryptedData)
  71.         handle.close()
  72.     end
  73.    
  74.     local files = fs.list(directory)
  75.     for _, file in ipairs(files) do
  76.         local filePath = fs.combine(directory, file)
  77.         if not fs.isDir(filePath) then
  78.             decryptFile(filePath)
  79.         end
  80.     end
  81.    
  82.     return true
  83. end
  84.  
  85. -- GUI Functions
  86. local function drawGui(window)
  87.     window.clear()
  88.     window.setBackgroundColor(colors.gray)
  89.     window.setTextColor(colors.white)
  90.  
  91.     -- Draw Title
  92.     window.setCursorPos(10, 2)
  93.     window.write("Encryption Tool")
  94.  
  95.     -- Draw Password Box Label
  96.     window.setCursorPos(6, 5)
  97.     window.write("Password:")
  98.  
  99.     -- Draw Buttons
  100.     window.setCursorPos(8, 10)
  101.     window.setBackgroundColor(colors.lightGray)
  102.     window.setTextColor(colors.black)
  103.     window.write(" Encrypt ")
  104.  
  105.     window.setCursorPos(20, 10)
  106.     window.write(" Decrypt ")
  107.  
  108.     window.setBackgroundColor(colors.gray)
  109.     window.setTextColor(colors.white)
  110. end
  111.  
  112. local function drawStatus(window, status)
  113.     window.setBackgroundColor(colors.gray)
  114.     window.setTextColor(colors.white)
  115.     window.setCursorPos(6, 12)
  116.     window.clearLine()
  117.     window.write("Status: " .. status)
  118. end
  119.  
  120. -- GUI Interaction
  121. local function encryptionGui()
  122.     local window = window.create(term.current(), 1, 1, 40, 15)
  123.     drawGui(window)
  124.     local password = ""
  125.    
  126.     -- Capture password input
  127.     while true do
  128.         local event, param1, param2, param3 = os.pullEvent()
  129.        
  130.         -- Handle button click events
  131.         if event == "mouse_click" then
  132.             local x, y = param2, param3
  133.            
  134.             -- Encrypt button click
  135.             if x >= 8 and x <= 16 and y == 10 then
  136.                 if password ~= "" then
  137.                     createEncryptionKey(password, "/disk/encryption_key.txt")
  138.                     encryptDirectory("/disk/", "/disk/encryption_key.txt")
  139.                     drawStatus(window, "Encryption Complete")
  140.                 else
  141.                     drawStatus(window, "Enter Password")
  142.                 end
  143.            
  144.             -- Decrypt button click
  145.             elseif x >= 20 and x <= 28 and y == 10 then
  146.                 if password ~= "" then
  147.                     local success = decryptDirectory("/disk/", "/disk/encryption_key.txt", password)
  148.                     if success then
  149.                         drawStatus(window, "Decryption Complete")
  150.                     else
  151.                         drawStatus(window, "Incorrect Password")
  152.                     end
  153.                 else
  154.                     drawStatus(window, "Enter Password")
  155.                 end
  156.             end
  157.        
  158.         -- Handle keyboard input for password
  159.         elseif event == "char" then
  160.             password = password .. param1
  161.             window.setCursorPos(16, 5)
  162.             window.clearLine()
  163.             window.write(string.rep("*", #password))
  164.         end
  165.     end
  166. end
  167.  
  168. -- Start the GUI
  169. encryptionGui()
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement