Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to generate a random key of specified length
- local function generateKey(length)
- local key = ""
- for i = 1, length do
- key = key .. string.char(math.random(0, 255))
- end
- return key
- end
- -- XOR encryption/decryption function
- local function xorEncryptDecrypt(data, key)
- local result = {}
- for i = 1, #data do
- local keyIndex = (i - 1) % #key + 1
- result[i] = string.char(bit.bxor(string.byte(data, i), string.byte(key, keyIndex)))
- end
- return table.concat(result)
- end
- -- Function to encrypt all files in a directory
- local function encryptFilesInDirectory(directory, key)
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local filePath = fs.combine(directory, file)
- if not fs.isDir(filePath) then
- -- Read file contents
- local fileHandle = fs.open(filePath, "r")
- local data = fileHandle.readAll()
- fileHandle.close()
- -- Encrypt data
- local encryptedData = xorEncryptDecrypt(data, key)
- -- Write encrypted data back to the file
- fileHandle = fs.open(filePath, "w")
- fileHandle.write(encryptedData)
- fileHandle.close()
- end
- end
- end
- -- Function to decrypt all files in a directory
- local function decryptFilesInDirectory(directory, key)
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local filePath = fs.combine(directory, file)
- if not fs.isDir(filePath) then
- -- Read encrypted file contents
- local fileHandle = fs.open(filePath, "r")
- local encryptedData = fileHandle.readAll()
- fileHandle.close()
- -- Decrypt data
- local decryptedData = xorEncryptDecrypt(encryptedData, key)
- -- Write decrypted data back to the file
- fileHandle = fs.open(filePath, "w")
- fileHandle.write(decryptedData)
- fileHandle.close()
- end
- end
- end
- -- Function to encrypt files and save the key
- local function mainEncrypt()
- -- Path to the disk
- local diskPath = "/disk"
- -- Generate a random encryption key
- local key = generateKey(16) -- 16 bytes key
- -- Save the key to a file
- local keyFilePath = fs.combine(diskPath, "encryption_key.txt")
- local keyFileHandle = fs.open(keyFilePath, "w")
- keyFileHandle.write(key)
- keyFileHandle.close()
- -- Encrypt all files in the disk directory
- encryptFilesInDirectory(diskPath, key)
- print("Encryption complete. Key saved to " .. keyFilePath)
- end
- -- Function to decrypt files using a saved key
- local function mainDecrypt()
- -- Path to the disk
- local diskPath = "/disk"
- -- Read the encryption key from the file
- local keyFilePath = fs.combine(diskPath, "encryption_key.txt")
- local keyFileHandle = fs.open(keyFilePath, "r")
- local key = keyFileHandle.readAll()
- keyFileHandle.close()
- -- Decrypt all files in the disk directory
- decryptFilesInDirectory(diskPath, key)
- print("Decryption complete using key from " .. keyFilePath)
- end
- -- Main function to prompt user for action
- local function main()
- print("Do you want to (E)ncrypt or (D)ecrypt files? (E/D): ")
- local choice = io.read():upper()
- if choice == "E" then
- mainEncrypt()
- elseif choice == "D" then
- mainDecrypt()
- else
- print("Invalid choice. Please enter 'E' for encryption or 'D' for decryption.")
- end
- end
- -- Run the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement