Advertisement
DOGGYWOOF

FS

Nov 26th, 2024 (edited)
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 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, passphrase)
  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, passphrase)
  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, passphrase)
  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, passphrase)
  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
  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. -- Process directory (encrypt or decrypt)
  78. local function processDirectory(dir, passphrase, operation)
  79. local items = fs.list(dir)
  80. local failedFiles = {}
  81.  
  82. for _, item in ipairs(items) do
  83. local fullPath = fs.combine(dir, item)
  84. if fs.isDir(fullPath) then
  85. processDirectory(fullPath, passphrase, operation)
  86. else
  87. local success, err = pcall(function()
  88. if operation == "encrypt" then
  89. encryptFile(fullPath, passphrase)
  90. elseif operation == "decrypt" then
  91. decryptFile(fullPath, passphrase)
  92. end
  93. end)
  94.  
  95. if not success then
  96. -- Log the file path and error message if encryption failed
  97. failedFiles[fullPath] = err
  98. end
  99. end
  100. end
  101.  
  102. -- After processing, retry files that failed
  103. if #failedFiles > 0 then
  104. print("\nRetrying files that failed to encrypt:")
  105. for path, errorMsg in pairs(failedFiles) do
  106. print("Failed to encrypt: " .. path .. " - " .. errorMsg)
  107. print("Retrying...")
  108. local success, err = pcall(function()
  109. if operation == "encrypt" then
  110. encryptFile(path, passphrase)
  111. elseif operation == "decrypt" then
  112. decryptFile(path, passphrase)
  113. end
  114. end)
  115. if not success then
  116. print("Still failed: " .. path .. " - " .. err)
  117. else
  118. print("Successfully retried: " .. path)
  119. end
  120. end
  121. end
  122. end
  123.  
  124. -- Save hashed credentials to .syskey
  125. local function saveCredentials(passphrase)
  126. local hashFile = "/.syskey"
  127. local hashedPassphrase = customHash(passphrase)
  128. local file = fs.open(hashFile, "w")
  129. if file then
  130. file.write(hashedPassphrase)
  131. file.close()
  132. print("Credentials saved to /.syskey.")
  133. else
  134. error("Failed to save credentials.")
  135. end
  136. end
  137.  
  138. -- Load credentials from .syskey
  139. local function loadCredentials()
  140. local hashFile = "/.syskey"
  141. if not fs.exists(hashFile) then
  142. error("Credentials file not found. Please set a passphrase first.")
  143. end
  144.  
  145. local file = fs.open(hashFile, "r")
  146. local storedHash = file.readAll()
  147. file.close()
  148. return storedHash
  149. end
  150.  
  151. -- Validate passphrase
  152. local function validatePassphrase(passphrase)
  153. local storedHash = loadCredentials()
  154. local providedHash = customHash(passphrase)
  155. return storedHash == providedHash
  156. end
  157.  
  158. -- GUI to prompt for passphrase
  159. local function launchGUI()
  160. term.clear()
  161. term.setCursorPos(1, 1)
  162. print("Disk Decryptor")
  163. print("-------------------------")
  164. print("Enter passphrase:")
  165. term.setCursorBlink(true)
  166. local passphrase = read("*") -- Hides input for password security
  167. term.setCursorBlink(false)
  168.  
  169. if not validatePassphrase(passphrase) then
  170. print("\nInvalid passphrase. Decryption aborted.")
  171. return
  172. end
  173.  
  174. print("\nDecrypting /disk/...")
  175.  
  176. local success, err = pcall(function()
  177. processDirectory("/disk", passphrase, "decrypt")
  178. end)
  179.  
  180. if success then
  181. print("Decryption complete.")
  182. print("Executing your code...")
  183. -- Replace this with the actual code to run after decryption
  184. shell.run("/disk/my_program.lua")
  185. else
  186. print("Error: " .. err)
  187. end
  188.  
  189. print("Press any key to exit.")
  190. os.pullEvent("key")
  191. end
  192.  
  193. -- Main script
  194. local args = { ... }
  195. if #args < 1 then
  196. print("Usage:")
  197. print(" Set passphrase: encrypt_disk set <passphrase>")
  198. print(" Encrypt disk: encrypt_disk encrypt")
  199. print(" Decrypt disk: encrypt_disk decrypt <passphrase>")
  200. print(" Launch GUI: encrypt_disk gui")
  201. return
  202. end
  203.  
  204. local command = args[1]:lower()
  205.  
  206. if command == "set" then
  207. if #args < 2 then
  208. error("Usage: encrypt_disk set <passphrase>")
  209. end
  210. local passphrase = args[2]
  211. print("Encrypting all files in /disk...")
  212. processDirectory("/disk", passphrase, "encrypt")
  213. saveCredentials(passphrase)
  214. print("Encryption complete.")
  215.  
  216. elseif command == "encrypt" then
  217. local storedHash = loadCredentials()
  218. local success, err = pcall(function()
  219. print("Encrypting all files in /disk using stored credentials...")
  220. processDirectory("/disk", storedHash, "encrypt")
  221. end)
  222. if success then
  223. print("Encryption complete.")
  224. else
  225. print("Error: " .. err)
  226. end
  227.  
  228. elseif command == "decrypt" then
  229. if #args < 2 then
  230. error("Usage: encrypt_disk decrypt <passphrase>")
  231. end
  232. local passphrase = args[2]
  233. if not validatePassphrase(passphrase) then
  234. error("Invalid passphrase. Decryption aborted.")
  235. end
  236. print("Decrypting all files in /disk...")
  237. processDirectory("/disk", passphrase, "decrypt")
  238. print("Decryption complete.")
  239.  
  240. elseif command == "gui" then
  241. launchGUI()
  242.  
  243. else
  244. print("Unknown command: " .. command)
  245. print("Valid commands: set, encrypt, decrypt, gui")
  246. end
  247.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement