Advertisement
DOGGYWOOF

Untitled

Nov 26th, 2024 (edited)
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. -- Binary-safe XOR encryption/decryption
  2. local function xorCipherBinary(data, key)
  3. if type(data) ~= "string" then
  4. error("Data must be a string. Type found: " .. type(data))
  5. end
  6. local result = {}
  7. local keyLength = #key
  8. for i = 1, #data do
  9. local keyChar = key:byte((i - 1) % keyLength + 1)
  10. result[i] = string.char(bit.bxor(data:byte(i), keyChar))
  11. end
  12. return table.concat(result)
  13. end
  14.  
  15. -- Encrypt a file (binary-safe)
  16. local function encryptFile(path, key)
  17. local file = fs.open(path, "rb")
  18. if not file then
  19. error("Failed to open file for reading: " .. path)
  20. end
  21. local data = file.readAll()
  22. file.close()
  23.  
  24. if not data then
  25. error("Failed to read data from file: " .. path)
  26. end
  27.  
  28. print("Read " .. #data .. " bytes from file.")
  29.  
  30. local encryptedData = xorCipherBinary(data, key)
  31. local encryptedFile = fs.open(path, "wb")
  32. if encryptedFile then
  33. encryptedFile.write(encryptedData)
  34. encryptedFile.close()
  35. print("Encrypted: " .. path)
  36. else
  37. error("Failed to open file for writing: " .. path)
  38. end
  39. end
  40.  
  41. -- Decrypt a file (binary-safe)
  42. local function decryptFile(path, key)
  43. local file = fs.open(path, "rb")
  44. if not file then
  45. error("Failed to open file for reading: " .. path)
  46. end
  47. local encryptedData = file.readAll()
  48. file.close()
  49.  
  50. if not encryptedData then
  51. error("Failed to read data from file: " .. path)
  52. end
  53.  
  54. print("Read " .. #encryptedData .. " bytes from file.")
  55.  
  56. local decryptedData = xorCipherBinary(encryptedData, key)
  57. local decryptedFile = fs.open(path, "wb")
  58. if decryptedFile then
  59. decryptedFile.write(decryptedData)
  60. decryptedFile.close()
  61. print("Decrypted: " .. path)
  62. else
  63. error("Failed to open file for writing: " .. path)
  64. end
  65. end
  66.  
  67. -- Custom hash function (for password)
  68. local function customHash(data)
  69. local hash = 0
  70. for i = 1, #data do
  71. hash = bit.bxor(hash, data:byte(i) * i)
  72. hash = (hash * 31) % 2^32 -- Keep hash within 32-bit range
  73. end
  74. return tostring(hash)
  75. end
  76.  
  77. -- Generate random key
  78. local function generateRandomKey(length)
  79. local key = {}
  80. for i = 1, length do
  81. table.insert(key, string.char(math.random(0, 255)))
  82. end
  83. return table.concat(key)
  84. end
  85.  
  86. -- Save encrypted key to .syskey
  87. local function saveKeyFile(passphrase, randomKey)
  88. local hashFile = "/.syskey"
  89. local hashedPassphrase = customHash(passphrase)
  90. local encryptedKey = xorCipherBinary(randomKey, hashedPassphrase)
  91.  
  92. local file = fs.open(hashFile, "w")
  93. if file then
  94. file.write(encryptedKey)
  95. file.close()
  96. print("Credentials and key saved to /.syskey.")
  97. else
  98. error("Failed to save key to /.syskey.")
  99. end
  100. end
  101.  
  102. -- Load the encrypted key from .syskey
  103. local function loadKeyFile(passphrase)
  104. local hashFile = "/.syskey"
  105. if not fs.exists(hashFile) then
  106. error("Credentials file not found. Please set a passphrase first.")
  107. end
  108.  
  109. local file = fs.open(hashFile, "r")
  110. local encryptedKey = file.readAll()
  111. file.close()
  112.  
  113. -- Decrypt the key using the passphrase
  114. return xorCipherBinary(encryptedKey, customHash(passphrase))
  115. end
  116.  
  117. -- Process directory (encrypt or decrypt)
  118. local function processDirectory(dir, key, operation)
  119. local items = fs.list(dir)
  120. local failedFiles = {}
  121.  
  122. for _, item in ipairs(items) do
  123. local fullPath = fs.combine(dir, item)
  124. if fs.isDir(fullPath) then
  125. processDirectory(fullPath, key, operation)
  126. else
  127. local success, err = pcall(function()
  128. if operation == "encrypt" then
  129. encryptFile(fullPath, key)
  130. elseif operation == "decrypt" then
  131. decryptFile(fullPath, key)
  132. end
  133. end)
  134.  
  135. if not success then
  136. -- Log the file path and error message if encryption failed
  137. failedFiles[fullPath] = err
  138. end
  139. end
  140. end
  141.  
  142. -- After processing, retry files that failed
  143. if #failedFiles > 0 then
  144. print("\nRetrying files that failed to encrypt:")
  145. for path, errorMsg in pairs(failedFiles) do
  146. print("Failed to encrypt: " .. path .. " - " .. errorMsg)
  147. print("Retrying...")
  148. local success, err = pcall(function()
  149. if operation == "encrypt" then
  150. encryptFile(path, key)
  151. elseif operation == "decrypt" then
  152. decryptFile(path, key)
  153. end
  154. end)
  155. if not success then
  156. print("Still failed: " .. path .. " - " .. err)
  157. else
  158. print("Successfully retried: " .. path)
  159. end
  160. end
  161. end
  162. end
  163.  
  164. -- Main script
  165. local args = { ... }
  166. if #args < 1 then
  167. print("Usage:")
  168. print(" Set passphrase: encrypt_disk set <passphrase>")
  169. print(" Encrypt disk: encrypt_disk encrypt")
  170. print(" Decrypt disk: encrypt_disk decrypt <passphrase>")
  171. return
  172. end
  173.  
  174. local command = args[1]:lower()
  175.  
  176. if command == "set" then
  177. if #args < 2 then
  178. error("Usage: encrypt_disk set <passphrase>")
  179. end
  180. local passphrase = args[2]
  181. local randomKey = generateRandomKey(64) -- Generate a random key of length 64 bytes
  182. print("Encrypting all files in /disk...")
  183. processDirectory("/disk", randomKey, "encrypt")
  184. saveKeyFile(passphrase, randomKey)
  185. print("Encryption complete.")
  186.  
  187. elseif command == "encrypt" then
  188. if #args < 2 then
  189. error("Usage: encrypt_disk encrypt <passphrase>")
  190. end
  191. local passphrase = args[2]
  192. local key = loadKeyFile(passphrase) -- Load the encrypted key from file
  193. print("Encrypting all files in /disk using the key...")
  194. processDirectory("/disk", key, "encrypt")
  195. print("Encryption complete.")
  196.  
  197. elseif command == "decrypt" then
  198. if #args < 2 then
  199. error("Usage: encrypt_disk decrypt <passphrase>")
  200. end
  201. local passphrase = args[2]
  202. local key = loadKeyFile(passphrase) -- Load the encrypted key from file
  203. print("Decrypting all files in /disk...")
  204. processDirectory("/disk", key, "decrypt")
  205. print("Decryption complete.")
  206. else
  207. print("Unknown command: " .. command)
  208. print("Valid commands: set, encrypt, decrypt")
  209. end
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement