Advertisement
DOGGYWOOF

Doggy OS File system encryption API

Aug 27th, 2024 (edited)
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. -- cryptoAPI.lua
  2.  
  3. local cryptoAPI = {}
  4.  
  5. local key = 123 -- Define a key for XOR encryption/decryption
  6.  
  7. -- Function to encrypt/decrypt a string using XOR
  8. local function xorEncryptDecrypt(data, key)
  9. local result = ""
  10. for i = 1, #data do
  11. result = result .. string.char(bit.bxor(data:byte(i), key))
  12. end
  13. return result
  14. end
  15.  
  16. -- Encrypt function
  17. function cryptoAPI.Encrypt(directory)
  18. local function encryptDirectory(directory)
  19. local files = fs.list(directory)
  20. for _, file in ipairs(files) do
  21. local path = fs.combine(directory, file)
  22. if fs.isDir(path) then
  23. -- Recursively encrypt directories
  24. encryptDirectory(path)
  25. else
  26. local fileHandle = fs.open(path, "r")
  27. local content = fileHandle.readAll()
  28. fileHandle.close()
  29. local encryptedContent = xorEncryptDecrypt(content, key)
  30. fileHandle = fs.open(path, "w")
  31. fileHandle.write(encryptedContent)
  32. fileHandle.close()
  33. print("Encrypted: " .. path)
  34. end
  35. end
  36. end
  37.  
  38. -- Start encryption
  39. encryptDirectory(directory)
  40. print("Encryption completed.")
  41. end
  42.  
  43. -- Decrypt function
  44. function cryptoAPI.Decrypt(directory)
  45. local function decryptDirectory(directory)
  46. local files = fs.list(directory)
  47. for _, file in ipairs(files) do
  48. local path = fs.combine(directory, file)
  49. if fs.isDir(path) then
  50. -- Recursively decrypt directories
  51. decryptDirectory(path)
  52. else
  53. local fileHandle = fs.open(path, "r")
  54. local content = fileHandle.readAll()
  55. fileHandle.close()
  56. local decryptedContent = xorEncryptDecrypt(content, key)
  57. fileHandle = fs.open(path, "w")
  58. fileHandle.write(decryptedContent)
  59. fileHandle.close()
  60. print("Decrypted: " .. path)
  61. end
  62. end
  63. end
  64.  
  65. -- Start decryption
  66. decryptDirectory(directory)
  67. print("Decryption completed.")
  68. end
  69.  
  70. return cryptoAPI
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement