Advertisement
DOGGYWOOF

Untitled

Sep 3rd, 2024 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. -- Function to generate a random key of specified length
  2. local function generateKey(length)
  3. local key = ""
  4. for i = 1, length do
  5. key = key .. string.char(math.random(0, 255))
  6. end
  7. return key
  8. end
  9.  
  10. -- XOR encryption/decryption function
  11. local function xorEncryptDecrypt(data, key)
  12. local result = {}
  13. for i = 1, #data do
  14. local keyIndex = (i - 1) % #key + 1
  15. result[i] = string.char(bit.bxor(string.byte(data, i), string.byte(key, keyIndex)))
  16. end
  17. return table.concat(result)
  18. end
  19.  
  20. -- Function to encrypt all files in a directory
  21. local function encryptFilesInDirectory(directory, key)
  22. local files = fs.list(directory)
  23. for _, file in ipairs(files) do
  24. local filePath = fs.combine(directory, file)
  25. if not fs.isDir(filePath) then
  26. -- Read file contents
  27. local fileHandle = fs.open(filePath, "r")
  28. local data = fileHandle.readAll()
  29. fileHandle.close()
  30.  
  31. -- Encrypt data
  32. local encryptedData = xorEncryptDecrypt(data, key)
  33.  
  34. -- Write encrypted data back to the file
  35. fileHandle = fs.open(filePath, "w")
  36. fileHandle.write(encryptedData)
  37. fileHandle.close()
  38. end
  39. end
  40. end
  41.  
  42. -- Function to decrypt all files in a directory
  43. local function decryptFilesInDirectory(directory, key)
  44. local files = fs.list(directory)
  45. for _, file in ipairs(files) do
  46. local filePath = fs.combine(directory, file)
  47. if not fs.isDir(filePath) then
  48. -- Read encrypted file contents
  49. local fileHandle = fs.open(filePath, "r")
  50. local encryptedData = fileHandle.readAll()
  51. fileHandle.close()
  52.  
  53. -- Decrypt data
  54. local decryptedData = xorEncryptDecrypt(encryptedData, key)
  55.  
  56. -- Write decrypted data back to the file
  57. fileHandle = fs.open(filePath, "w")
  58. fileHandle.write(decryptedData)
  59. fileHandle.close()
  60. end
  61. end
  62. end
  63.  
  64. -- Function to encrypt files and save the key
  65. local function mainEncrypt()
  66. -- Path to the disk
  67. local diskPath = "/disk"
  68.  
  69. -- Generate a random encryption key
  70. local key = generateKey(16) -- 16 bytes key
  71.  
  72. -- Save the key to a file
  73. local keyFilePath = fs.combine(diskPath, "encryption_key.txt")
  74. local keyFileHandle = fs.open(keyFilePath, "w")
  75. keyFileHandle.write(key)
  76. keyFileHandle.close()
  77.  
  78. -- Encrypt all files in the disk directory
  79. encryptFilesInDirectory(diskPath, key)
  80.  
  81. print("Encryption complete. Key saved to " .. keyFilePath)
  82. end
  83.  
  84. -- Function to decrypt files using a saved key
  85. local function mainDecrypt()
  86. -- Path to the disk
  87. local diskPath = "/disk"
  88.  
  89. -- Read the encryption key from the file
  90. local keyFilePath = fs.combine(diskPath, "encryption_key.txt")
  91. local keyFileHandle = fs.open(keyFilePath, "r")
  92. local key = keyFileHandle.readAll()
  93. keyFileHandle.close()
  94.  
  95. -- Decrypt all files in the disk directory
  96. decryptFilesInDirectory(diskPath, key)
  97.  
  98. print("Decryption complete using key from " .. keyFilePath)
  99. end
  100.  
  101. -- Main function to prompt user for action
  102. local function main()
  103. print("Do you want to (E)ncrypt or (D)ecrypt files? (E/D): ")
  104. local choice = io.read():upper()
  105.  
  106. if choice == "E" then
  107. mainEncrypt()
  108. elseif choice == "D" then
  109. mainDecrypt()
  110. else
  111. print("Invalid choice. Please enter 'E' for encryption or 'D' for decryption.")
  112. end
  113. end
  114.  
  115. -- Run the main function
  116. main()
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement