Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- cryptoAPI.lua
- local cryptoAPI = {}
- local key = 123 -- Define a key for XOR encryption/decryption
- -- Function to encrypt/decrypt a string using XOR
- local function xorEncryptDecrypt(data, key)
- local result = ""
- for i = 1, #data do
- result = result .. string.char(bit.bxor(data:byte(i), key))
- end
- return result
- end
- -- Encrypt function
- function cryptoAPI.Encrypt(directory)
- local function encryptDirectory(directory)
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local path = fs.combine(directory, file)
- if fs.isDir(path) then
- -- Recursively encrypt directories
- encryptDirectory(path)
- else
- local fileHandle = fs.open(path, "r")
- local content = fileHandle.readAll()
- fileHandle.close()
- local encryptedContent = xorEncryptDecrypt(content, key)
- fileHandle = fs.open(path, "w")
- fileHandle.write(encryptedContent)
- fileHandle.close()
- print("Encrypted: " .. path)
- end
- end
- end
- -- Start encryption
- encryptDirectory(directory)
- print("Encryption completed.")
- end
- -- Decrypt function
- function cryptoAPI.Decrypt(directory)
- local function decryptDirectory(directory)
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local path = fs.combine(directory, file)
- if fs.isDir(path) then
- -- Recursively decrypt directories
- decryptDirectory(path)
- else
- local fileHandle = fs.open(path, "r")
- local content = fileHandle.readAll()
- fileHandle.close()
- local decryptedContent = xorEncryptDecrypt(content, key)
- fileHandle = fs.open(path, "w")
- fileHandle.write(decryptedContent)
- fileHandle.close()
- print("Decrypted: " .. path)
- end
- end
- end
- -- Start decryption
- decryptDirectory(directory)
- print("Decryption completed.")
- end
- return cryptoAPI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement