Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- XOR Encryption/Decryption Functions
- local function xorEncrypt(data, key)
- local result = {}
- for i = 1, #data do
- local dataByte = string.byte(data, i)
- local keyByte = string.byte(key, (i - 1) % #key + 1)
- result[i] = string.char(bit.bxor(dataByte, keyByte))
- end
- return table.concat(result)
- end
- local function createEncryptionKey(password, keyPath)
- local encryptionKey = "my_secret_key"
- local encryptedPassword = xorEncrypt(password, encryptionKey)
- local file = fs.open(keyPath, "w")
- file.write(encryptionKey .. ":" .. encryptedPassword)
- file.close()
- end
- local function encryptDirectory(directory, keyPath)
- local file = fs.open(keyPath, "r")
- local content = file.readAll()
- file.close()
- local encryptionKey, _ = content:match("([^:]+):([^:]+)")
- local function encryptFile(path)
- local handle = fs.open(path, "r")
- local data = handle.readAll()
- handle.close()
- local encryptedData = xorEncrypt(data, encryptionKey)
- handle = fs.open(path, "w")
- handle.write(encryptedData)
- handle.close()
- end
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local filePath = fs.combine(directory, file)
- if not fs.isDir(filePath) then
- encryptFile(filePath)
- end
- end
- end
- local function decryptDirectory(directory, keyPath, password)
- local file = fs.open(keyPath, "r")
- local content = file.readAll()
- file.close()
- local encryptionKey, encryptedPassword = content:match("([^:]+):([^:]+)")
- local decryptedPassword = xorEncrypt(encryptedPassword, encryptionKey)
- if password ~= decryptedPassword then
- return false
- end
- local function decryptFile(path)
- local handle = fs.open(path, "r")
- local data = handle.readAll()
- handle.close()
- local decryptedData = xorEncrypt(data, encryptionKey)
- handle = fs.open(path, "w")
- handle.write(decryptedData)
- handle.close()
- end
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local filePath = fs.combine(directory, file)
- if not fs.isDir(filePath) then
- decryptFile(filePath)
- end
- end
- return true
- end
- -- GUI Functions
- local function drawGui(window)
- window.clear()
- window.setBackgroundColor(colors.gray)
- window.setTextColor(colors.white)
- -- Draw Title
- window.setCursorPos(10, 2)
- window.write("Encryption Tool")
- -- Draw Password Box Label
- window.setCursorPos(6, 5)
- window.write("Password:")
- -- Draw Buttons
- window.setCursorPos(8, 10)
- window.setBackgroundColor(colors.lightGray)
- window.setTextColor(colors.black)
- window.write(" Encrypt ")
- window.setCursorPos(20, 10)
- window.write(" Decrypt ")
- window.setBackgroundColor(colors.gray)
- window.setTextColor(colors.white)
- end
- local function drawStatus(window, status)
- window.setBackgroundColor(colors.gray)
- window.setTextColor(colors.white)
- window.setCursorPos(6, 12)
- window.clearLine()
- window.write("Status: " .. status)
- end
- -- GUI Interaction
- local function encryptionGui()
- local window = window.create(term.current(), 1, 1, 40, 15)
- drawGui(window)
- local password = ""
- -- Capture password input
- while true do
- local event, param1, param2, param3 = os.pullEvent()
- -- Handle button click events
- if event == "mouse_click" then
- local x, y = param2, param3
- -- Encrypt button click
- if x >= 8 and x <= 16 and y == 10 then
- if password ~= "" then
- createEncryptionKey(password, "/disk/encryption_key.txt")
- encryptDirectory("/disk/", "/disk/encryption_key.txt")
- drawStatus(window, "Encryption Complete")
- else
- drawStatus(window, "Enter Password")
- end
- -- Decrypt button click
- elseif x >= 20 and x <= 28 and y == 10 then
- if password ~= "" then
- local success = decryptDirectory("/disk/", "/disk/encryption_key.txt", password)
- if success then
- drawStatus(window, "Decryption Complete")
- else
- drawStatus(window, "Incorrect Password")
- end
- else
- drawStatus(window, "Enter Password")
- end
- end
- -- Handle keyboard input for password
- elseif event == "char" then
- password = password .. param1
- window.setCursorPos(16, 5)
- window.clearLine()
- window.write(string.rep("*", #password))
- end
- end
- end
- -- Start the GUI
- encryptionGui()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement