Advertisement
DOGGYWOOF

Untitled

Jul 18th, 2024 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.09 KB | None | 0 0
  1. -- Function to generate a key for encryption and decryption
  2. function generateKey()
  3. local chars = {}
  4. for i = 32, 126 do -- ASCII range for printable characters
  5. table.insert(chars, string.char(i))
  6. end
  7.  
  8. local key = {}
  9. while #chars > 0 do
  10. local index = math.random(#chars)
  11. table.insert(key, table.remove(chars, index))
  12. end
  13.  
  14. return table.concat(key)
  15. end
  16.  
  17. -- Function to create a map from a key
  18. function createMap(key)
  19. local map = {}
  20. for i = 32, 126 do
  21. map[string.char(i)] = key:sub(i - 31, i - 31)
  22. end
  23. return map
  24. end
  25.  
  26. -- Function to invert a map
  27. function invertMap(map)
  28. local invMap = {}
  29. for k, v in pairs(map) do
  30. invMap[v] = k
  31. end
  32. return invMap
  33. end
  34.  
  35. -- Function to encrypt text using a key
  36. function encrypt(text, key)
  37. local map = createMap(key)
  38. local encrypted = {}
  39. for i = 1, #text do
  40. local char = text:sub(i, i)
  41. table.insert(encrypted, map[char] or char)
  42. end
  43. return table.concat(encrypted)
  44. end
  45.  
  46. -- Function to decrypt text using a key
  47. function decrypt(text, key)
  48. local blockedPath = "/disk/blocked"
  49. if fs.exists(blockedPath) then
  50. -- Display error message and prompt to reboot
  51. term.clear()
  52. term.setCursorPos(1, 1)
  53. print("Doggy OS File System Protection")
  54. print()
  55. print("Something happened and the file system cannot be decrypted.")
  56. print("To protect the data, this device needs to be reset and Doggy OS has to be reinstalled.")
  57. print()
  58. print("Press Enter to reboot...")
  59. read() -- Wait for user to press Enter
  60. os.reboot()
  61. else
  62. local map = invertMap(createMap(key))
  63. local decrypted = {}
  64. for i = 1, #text do
  65. local char = text:sub(i, i)
  66. table.insert(decrypted, map[char] or char)
  67. end
  68. return table.concat(decrypted)
  69. end
  70. end
  71.  
  72. -- Function to save the key to a file
  73. function saveKeyToFile(key)
  74. local fileName = "key.txt" -- Fixed key file name
  75. local file = fs.open(fileName, "w")
  76. file.write(key)
  77. file.close()
  78. return fileName
  79. end
  80.  
  81. -- Function to load the key from a file without deleting it
  82. function loadKeyFromFile()
  83. local fileName = "key.txt" -- Fixed key file name
  84. local path = fs.combine("/", fileName)
  85. if fs.exists(path) then
  86. local file = fs.open(path, "r")
  87. if file then
  88. local key = file.readAll()
  89. file.close()
  90. return key
  91. else
  92. return nil, "Error: Unable to open key file."
  93. end
  94. else
  95. return nil, "Error: Key file does not exist."
  96. end
  97. end
  98.  
  99. -- Function to set or reset the password in the password file
  100. -- and update the key file
  101. function setPassword(newPassword)
  102. local key = generateKey() -- Generate a new encryption key
  103. local encryptedPassword = encrypt(newPassword, key)
  104.  
  105. local fileName = "pass.txt" -- Fixed password file name
  106. local file = fs.open(fileName, "w")
  107. file.write(encryptedPassword)
  108. file.close()
  109.  
  110. -- Save the key to key.txt
  111. saveKeyToFile(key)
  112.  
  113. return true
  114. end
  115.  
  116. -- Function to verify password during system startup
  117. function verifyPassword(enteredPassword)
  118. local fileName = "pass.txt" -- Fixed password file name
  119. local path = fs.combine("/", fileName)
  120. if fs.exists(path) then
  121. local file = fs.open(path, "r")
  122. if file then
  123. local encryptedPassword = file.readAll()
  124. file.close()
  125.  
  126. local key, loadError = loadKeyFromFile()
  127. if key then
  128. local decryptedPassword = decrypt(encryptedPassword, key)
  129. if enteredPassword == decryptedPassword then
  130. return true
  131. else
  132. -- Create blocked file on incorrect password and initiate reboot
  133. local blockedFile = fs.open("/disk/blocked", "w")
  134. blockedFile.write("This file indicates decryption is blocked.")
  135. blockedFile.close()
  136.  
  137. print("Incorrect password.")
  138. print("Press Enter to reboot...")
  139. read() -- Wait for user to press Enter
  140. os.reboot()
  141. return false
  142. end
  143. else
  144. print(loadError)
  145. return false
  146. end
  147. else
  148. print("Error: Unable to open password file.")
  149. return false
  150. end
  151. else
  152. print("Error: Password file does not exist.")
  153. return false
  154. end
  155. end
  156.  
  157. -- Function to create a blocked file
  158. function createBlockedFile()
  159. local fileName = "/disk/blocked"
  160. local file = fs.open(fileName, "w")
  161. file.write("This file indicates decryption is blocked.")
  162. file.close()
  163. end
  164.  
  165. -- Function to delete /disk and /recovery directories, and key/password files
  166. function formatSystem()
  167. -- Delete /disk and /recovery directories
  168. fs.delete("/disk")
  169. fs.delete("/recovery")
  170.  
  171. -- Remove key.txt and pass.txt
  172. fs.delete("/key.txt")
  173. fs.delete("/pass.txt")
  174.  
  175. term.clear()
  176. term.setCursorPos(1, 1)
  177. print("Doggy OS File System Protection")
  178. print()
  179. print("This device has been reset and needs to be repaired.")
  180. print("All user and system data has been cleared.")
  181. print("Insert this device into a Doggy OS Machine to reinstall all firmware and software.")
  182. end
  183.  
  184. -- Function to recover the system (delete disk and replace with recovery)
  185. function recoverSystem()
  186. local files = fs.list("/disk")
  187. for _, file in ipairs(files) do
  188. local filePath = fs.combine("/disk", file)
  189. if not fs.isDir(filePath) then
  190. fs.delete(filePath)
  191. end
  192. end
  193. -- Optionally copy recovery files from a backup location or setup
  194. print("System recovered. Please restart your system.")
  195. end
  196.  
  197. -- Function to encrypt files in a directory
  198. function encryptDirectory(path, key)
  199. local files = fs.list(path)
  200. for _, file in ipairs(files) do
  201. local filePath = fs.combine(path, file)
  202. if fs.isDir(filePath) then
  203. encryptDirectory(filePath, key)
  204. else
  205. local file = fs.open(filePath, "r")
  206. local content = file.readAll()
  207. file.close()
  208.  
  209. local encryptedContent = encrypt(content, key)
  210.  
  211. file = fs.open(filePath, "w")
  212. file.write(encryptedContent)
  213. file.close()
  214. end
  215. end
  216. end
  217.  
  218. -- Function to process files in a directory with a given process function
  219. function processDirectory(path, processFunction, key)
  220. local files = fs.list(path)
  221. for _, file in ipairs(files) do
  222. local filePath = fs.combine(path, file)
  223. if fs.isDir(filePath) then
  224. processDirectory(filePath, processFunction, key)
  225. else
  226. local file = fs.open(filePath, "r")
  227. local content = file.readAll()
  228. file.close()
  229.  
  230. local processedContent = processFunction(content, key)
  231.  
  232. file = fs.open(filePath, "w")
  233. file.write(processedContent)
  234. file.close()
  235. end
  236. end
  237. end
  238.  
  239. -- Function to encrypt the entire system ("/disk" directory)
  240. function encryptSystem()
  241. local key = loadKeyFromFile()
  242. if key then
  243. encryptDirectory("/disk", key)
  244. print("System encrypted.")
  245. else
  246. print("Error: Unable to load encryption key.")
  247. end
  248. end
  249.  
  250. -- Function to show protection screen options
  251. function showProtectionScreen()
  252. term.clear()
  253. term.setCursorPos(1, 1)
  254. print("Doggy OS File System Protection")
  255. print()
  256. print("F1 - Unlock")
  257. print("F8 - Shutdown")
  258. print("F9 - Recover System")
  259.  
  260. while true do
  261. local event, key = os.pullEvent("key")
  262. if key == keys.f1 then
  263. term.clear()
  264. term.setCursorPos(1, 1)
  265. print("Enter decryption password:")
  266. local password = read("*")
  267.  
  268. if verifyPassword(password) then
  269. local key = loadKeyFromFile()
  270. if key then
  271. processDirectory("/disk", decrypt, key)
  272. shell.run("/disk/boot/BIOS")
  273. else
  274. print("Error: Unable to load encryption key.")
  275. end
  276. else
  277. print("Incorrect password.")
  278. end
  279.  
  280. return
  281. elseif key == keys.f8 then
  282. os.shutdown()
  283. return
  284. elseif key == keys.f9 then
  285. recoverSystem()
  286. return
  287. end
  288. end
  289. end
  290.  
  291. -- Main Program
  292. term.clear()
  293. term.setCursorPos(1, 1)
  294. print("Doggy OS File System Encryption")
  295. print()
  296. print("Initializing...")
  297.  
  298. local tArgs = {...}
  299. local mode = tArgs[1]
  300.  
  301. if mode == "decrypt" then
  302. -- Check if decryption is blocked
  303. local blockedPath = "/disk/blocked"
  304. if fs.exists(blockedPath) then
  305. -- Display error message
  306. term.clear()
  307. term.setCursorPos(1, 1)
  308. print("Doggy OS File System Protection")
  309. print()
  310. print("Something happened and the file system cannot be decrypted.")
  311. print("To protect the data, this device needs to be reset and Doggy OS has to be reinstalled.")
  312. print()
  313. print("Press Enter to reboot...")
  314. read() -- Wait for user to press Enter
  315. os.reboot()
  316. else
  317. showProtectionScreen()
  318. end
  319. elseif mode == "reset" then
  320. -- Reset password functionality
  321. local newPassword = tArgs[2]
  322. if newPassword then
  323. if setPassword(newPassword) then
  324. print("Password reset successful.")
  325. else
  326. print("Error resetting password.")
  327. end
  328. else
  329. print("Usage: reset newpassword")
  330. end
  331. elseif mode == "encrypt" then
  332. encryptSystem()
  333. elseif mode == "format" then
  334. formatSystem()
  335. else
  336. print("Invalid mode. Supported modes: decrypt, reset, encrypt, format")
  337. end
  338.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement