Advertisement
DOGGYWOOF

API

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