Advertisement
DOGGYWOOF

Doggy OS FS encryption

Jul 16th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 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. -- Main program
  43. local function main()
  44. -- Path to the disk
  45. local diskPath = "/disk"
  46.  
  47. -- Generate a random encryption key
  48. local key = generateKey(16) -- 16 bytes key
  49.  
  50. -- Save the key to a file
  51. local keyFilePath = fs.combine(diskPath, "encryption_key.txt")
  52. local keyFileHandle = fs.open(keyFilePath, "w")
  53. keyFileHandle.write(key)
  54. keyFileHandle.close()
  55.  
  56. -- Encrypt all files in the disk directory
  57. encryptFilesInDirectory(diskPath, key)
  58.  
  59. print("Encryption complete. Key saved to " .. keyFilePath)
  60. end
  61.  
  62. -- Run the main program
  63. main()
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement