Advertisement
DOGGYWOOF

Untitled

Jul 16th, 2024
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 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 or decrypt all files in a directory
  21. local function processFilesInDirectory(directory, key, mode)
  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 or decrypt data
  32. local processedData = xorEncryptDecrypt(data, key)
  33.  
  34. -- Write processed data back to the file
  35. fileHandle = fs.open(filePath, "w")
  36. fileHandle.write(processedData)
  37. fileHandle.close()
  38. end
  39. end
  40. end
  41.  
  42. -- Main program
  43. local function main()
  44. -- Path to the disk
  45. local diskPath = "/disk"
  46.  
  47. print("Would you like to encrypt or decrypt the files? (e/d)")
  48. local mode = read()
  49.  
  50. if mode == "e" then
  51. -- Encryption mode
  52. print("Enter the name to save the decryption key as (e.g., decryption_key.txt):")
  53. local keyFileName = read()
  54.  
  55. print("Enter the directory path to save the decryption key:")
  56. local keyDirectory = read()
  57.  
  58. -- Generate a random encryption key
  59. local key = generateKey(16) -- 16 bytes key
  60.  
  61. -- Save the key to a file
  62. local keyFilePath = fs.combine(keyDirectory, keyFileName)
  63. local keyFileHandle = fs.open(keyFilePath, "w")
  64. keyFileHandle.write(key)
  65. keyFileHandle.close()
  66.  
  67. -- Encrypt all files in the disk directory
  68. processFilesInDirectory(diskPath, key, "encrypt")
  69.  
  70. print("Encryption complete. Key saved to " .. keyFilePath)
  71. elseif mode == "d" then
  72. -- Decryption mode
  73. print("Enter the path to the decryption key file:")
  74. local keyFilePath = read()
  75.  
  76. -- Read the decryption key from the file
  77. local keyFileHandle = fs.open(keyFilePath, "r")
  78. local key = keyFileHandle.readAll()
  79. keyFileHandle.close()
  80.  
  81. -- Decrypt all files in the disk directory
  82. processFilesInDirectory(diskPath, key, "decrypt")
  83.  
  84. print("Decryption complete.")
  85. else
  86. print("Invalid option. Please run the program again and choose 'e' for encrypt or 'd' for decrypt.")
  87. end
  88. end
  89.  
  90. -- Run the main program
  91. main()
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement