Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Binary-safe XOR encryption/decryption
- local function xorCipherBinary(data, key)
- if type(data) ~= "string" then
- error("Data must be a string. Type found: " .. type(data))
- end
- local result = {}
- local keyLength = #key
- for i = 1, #data do
- local keyChar = key:byte((i - 1) % keyLength + 1)
- result[i] = string.char(bit.bxor(data:byte(i), keyChar))
- end
- return table.concat(result)
- end
- -- Encrypt a file (binary-safe)
- local function encryptFile(path, passphrase)
- local file = fs.open(path, "rb")
- if not file then
- error("Failed to open file for reading: " .. path)
- end
- local data = file.readAll()
- file.close()
- if not data then
- error("Failed to read data from file: " .. path)
- end
- print("Read " .. #data .. " bytes from file.")
- local encryptedData = xorCipherBinary(data, passphrase)
- local encryptedFile = fs.open(path, "wb")
- if encryptedFile then
- encryptedFile.write(encryptedData)
- encryptedFile.close()
- print("Encrypted: " .. path)
- else
- error("Failed to open file for writing: " .. path)
- end
- end
- -- Decrypt a file (binary-safe)
- local function decryptFile(path, passphrase)
- local file = fs.open(path, "rb")
- if not file then
- error("Failed to open file for reading: " .. path)
- end
- local encryptedData = file.readAll()
- file.close()
- if not encryptedData then
- error("Failed to read data from file: " .. path)
- end
- print("Read " .. #encryptedData .. " bytes from file.")
- local decryptedData = xorCipherBinary(encryptedData, passphrase)
- local decryptedFile = fs.open(path, "wb")
- if decryptedFile then
- decryptedFile.write(decryptedData)
- decryptedFile.close()
- print("Decrypted: " .. path)
- else
- error("Failed to open file for writing: " .. path)
- end
- end
- -- Custom hash function
- local function customHash(data)
- local hash = 0
- for i = 1, #data do
- hash = bit.bxor(hash, data:byte(i) * i)
- hash = (hash * 31) % 2^32 -- Keep hash within 32-bit range
- end
- return tostring(hash)
- end
- -- Process directory (encrypt or decrypt)
- local function processDirectory(dir, passphrase, operation)
- local items = fs.list(dir)
- local failedFiles = {}
- for _, item in ipairs(items) do
- local fullPath = fs.combine(dir, item)
- if fs.isDir(fullPath) then
- processDirectory(fullPath, passphrase, operation)
- else
- local success, err = pcall(function()
- if operation == "encrypt" then
- encryptFile(fullPath, passphrase)
- elseif operation == "decrypt" then
- decryptFile(fullPath, passphrase)
- end
- end)
- if not success then
- -- Log the file path and error message if encryption failed
- failedFiles[fullPath] = err
- end
- end
- end
- -- After processing, retry files that failed
- if #failedFiles > 0 then
- print("\nRetrying files that failed to encrypt:")
- for path, errorMsg in pairs(failedFiles) do
- print("Failed to encrypt: " .. path .. " - " .. errorMsg)
- print("Retrying...")
- local success, err = pcall(function()
- if operation == "encrypt" then
- encryptFile(path, passphrase)
- elseif operation == "decrypt" then
- decryptFile(path, passphrase)
- end
- end)
- if not success then
- print("Still failed: " .. path .. " - " .. err)
- else
- print("Successfully retried: " .. path)
- end
- end
- end
- end
- -- Save hashed credentials to .syskey
- local function saveCredentials(passphrase)
- local hashFile = "/.syskey"
- local hashedPassphrase = customHash(passphrase)
- local file = fs.open(hashFile, "w")
- if file then
- file.write(hashedPassphrase)
- file.close()
- print("Credentials saved to /.syskey.")
- else
- error("Failed to save credentials.")
- end
- end
- -- Load credentials from .syskey
- local function loadCredentials()
- local hashFile = "/.syskey"
- if not fs.exists(hashFile) then
- error("Credentials file not found. Please set a passphrase first.")
- end
- local file = fs.open(hashFile, "r")
- local storedHash = file.readAll()
- file.close()
- return storedHash
- end
- -- Validate passphrase
- local function validatePassphrase(passphrase)
- local storedHash = loadCredentials()
- local providedHash = customHash(passphrase)
- return storedHash == providedHash
- end
- -- GUI to prompt for passphrase
- local function launchGUI()
- term.clear()
- term.setCursorPos(1, 1)
- print("Disk Decryptor")
- print("-------------------------")
- print("Enter passphrase:")
- term.setCursorBlink(true)
- local passphrase = read("*") -- Hides input for password security
- term.setCursorBlink(false)
- if not validatePassphrase(passphrase) then
- print("\nInvalid passphrase. Decryption aborted.")
- return
- end
- print("\nDecrypting /disk/...")
- local success, err = pcall(function()
- processDirectory("/disk", passphrase, "decrypt")
- end)
- if success then
- print("Decryption complete.")
- print("Executing your code...")
- -- Replace this with the actual code to run after decryption
- shell.run("/disk/my_program.lua")
- else
- print("Error: " .. err)
- end
- print("Press any key to exit.")
- os.pullEvent("key")
- end
- -- Main script
- local args = { ... }
- if #args < 1 then
- print("Usage:")
- print(" Set passphrase: encrypt_disk set <passphrase>")
- print(" Encrypt disk: encrypt_disk encrypt")
- print(" Decrypt disk: encrypt_disk decrypt <passphrase>")
- print(" Launch GUI: encrypt_disk gui")
- return
- end
- local command = args[1]:lower()
- if command == "set" then
- if #args < 2 then
- error("Usage: encrypt_disk set <passphrase>")
- end
- local passphrase = args[2]
- print("Encrypting all files in /disk...")
- processDirectory("/disk", passphrase, "encrypt")
- saveCredentials(passphrase)
- print("Encryption complete.")
- elseif command == "encrypt" then
- local storedHash = loadCredentials()
- local success, err = pcall(function()
- print("Encrypting all files in /disk using stored credentials...")
- processDirectory("/disk", storedHash, "encrypt")
- end)
- if success then
- print("Encryption complete.")
- else
- print("Error: " .. err)
- end
- elseif command == "decrypt" then
- if #args < 2 then
- error("Usage: encrypt_disk decrypt <passphrase>")
- end
- local passphrase = args[2]
- if not validatePassphrase(passphrase) then
- error("Invalid passphrase. Decryption aborted.")
- end
- print("Decrypting all files in /disk...")
- processDirectory("/disk", passphrase, "decrypt")
- print("Decryption complete.")
- elseif command == "gui" then
- launchGUI()
- else
- print("Unknown command: " .. command)
- print("Valid commands: set, encrypt, decrypt, gui")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement