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 or decrypt all files in a directory
- local function processFilesInDirectory(directory, key, mode)
- 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 or decrypt data
- local processedData = xorEncryptDecrypt(data, key)
- -- Write processed data back to the file
- fileHandle = fs.open(filePath, "w")
- fileHandle.write(processedData)
- fileHandle.close()
- end
- end
- end
- -- Main program
- local function main()
- -- Path to the disk
- local diskPath = "/disk"
- print("Would you like to encrypt or decrypt the files? (e/d)")
- local mode = read()
- if mode == "e" then
- -- Encryption mode
- print("Enter the name to save the decryption key as (e.g., decryption_key.txt):")
- local keyFileName = read()
- print("Enter the directory path to save the decryption key:")
- local keyDirectory = read()
- -- Generate a random encryption key
- local key = generateKey(16) -- 16 bytes key
- -- Save the key to a file
- local keyFilePath = fs.combine(keyDirectory, keyFileName)
- local keyFileHandle = fs.open(keyFilePath, "w")
- keyFileHandle.write(key)
- keyFileHandle.close()
- -- Encrypt all files in the disk directory
- processFilesInDirectory(diskPath, key, "encrypt")
- print("Encryption complete. Key saved to " .. keyFilePath)
- elseif mode == "d" then
- -- Decryption mode
- print("Enter the path to the decryption key file:")
- local keyFilePath = read()
- -- Read the decryption key from the file
- local keyFileHandle = fs.open(keyFilePath, "r")
- local key = keyFileHandle.readAll()
- keyFileHandle.close()
- -- Decrypt all files in the disk directory
- processFilesInDirectory(diskPath, key, "decrypt")
- print("Decryption complete.")
- else
- print("Invalid option. Please run the program again and choose 'e' for encrypt or 'd' for decrypt.")
- end
- end
- -- Run the main program
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement