Advertisement
DOGGYWOOF

Untitled

Jul 17th, 2024 (edited)
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 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 map = invertMap(createMap(key))
  49. local decrypted = {}
  50. for i = 1, #text do
  51. local char = text:sub(i, i)
  52. table.insert(decrypted, map[char] or char)
  53. end
  54. return table.concat(decrypted)
  55. end
  56.  
  57. -- Function to save the key to a file
  58. function saveKeyToFile(key)
  59. local fileName = "key.txt" -- Fixed key file name
  60. local file = fs.open(fileName, "w")
  61. file.write(key)
  62. file.close()
  63. return fileName
  64. end
  65.  
  66. -- Function to load the key from a file without deleting it
  67. function loadKeyFromFile()
  68. local fileName = "key.txt" -- Fixed key file name
  69. local path = fs.combine("/", fileName)
  70. if fs.exists(path) then
  71. local file = fs.open(path, "r")
  72. if file then
  73. local key = file.readAll()
  74. file.close()
  75. return key
  76. else
  77. print("Error: Unable to open key file.")
  78. return nil
  79. end
  80. else
  81. print("Error: Key file does not exist.")
  82. return nil
  83. end
  84. end
  85.  
  86. -- Function to set or reset the password in the password file
  87. -- and update the key file
  88. function setPassword(newPassword)
  89. local key = generateKey() -- Generate a new encryption key
  90. local encryptedPassword = encrypt(newPassword, key)
  91.  
  92. local fileName = "pass.txt" -- Fixed password file name
  93. local file = fs.open(fileName, "w")
  94. file.write(encryptedPassword)
  95. file.close()
  96.  
  97. -- Save the key to key.txt
  98. saveKeyToFile(key)
  99.  
  100. return true
  101. end
  102.  
  103. -- Function to verify password during system startup
  104. function verifyPassword(enteredPassword)
  105. local fileName = "pass.txt" -- Fixed password file name
  106. local path = fs.combine("/", fileName)
  107. if fs.exists(path) then
  108. local file = fs.open(path, "r")
  109. if file then
  110. local encryptedPassword = file.readAll()
  111. file.close()
  112.  
  113. local key = loadKeyFromFile()
  114. if key then
  115. local decryptedPassword = decrypt(encryptedPassword, key)
  116. return enteredPassword == decryptedPassword
  117. else
  118. print("Error: Unable to load encryption key.")
  119. return false
  120. end
  121. else
  122. print("Error: Unable to open password file.")
  123. return false
  124. end
  125. else
  126. print("Error: Password file does not exist.")
  127. return false
  128. end
  129. end
  130.  
  131. -- Function to show protection screen options
  132. function showProtectionScreen()
  133. term.clear()
  134. term.setCursorPos(1, 1)
  135. print("Doggy OS File System Protection")
  136. print()
  137. print("F1 - Unlock")
  138. print("F8 - Shutdown")
  139. print("F9 - Recover System")
  140.  
  141. while true do
  142. local event, key = os.pullEvent("key")
  143. if key == keys.f1 then
  144. term.clear()
  145. term.setCursorPos(1, 1)
  146. print("Enter decryption password:")
  147. local password = read("*")
  148.  
  149. if verifyPassword(password) then
  150. local key = loadKeyFromFile()
  151. if key then
  152. processDirectory("/disk", decrypt, key)
  153. shell.run("/disk/boot/BIOS")
  154. else
  155. print("Error: Unable to load encryption key.")
  156. end
  157. else
  158. print("Incorrect password.")
  159. end
  160.  
  161. return
  162. elseif key == keys.f8 then
  163. os.shutdown()
  164. return
  165. elseif key == keys.f9 then
  166. recoverSystem()
  167. return
  168. end
  169. end
  170. end
  171.  
  172. -- Function to recover the system (delete disk and replace with recovery)
  173. function recoverSystem()
  174. local files = fs.list("/disk")
  175. for _, file in ipairs(files) do
  176. local filePath = fs.combine("/disk", file)
  177. if not fs.isDir(filePath) then
  178. fs.delete(filePath)
  179. end
  180. end
  181. -- Optionally copy recovery files from a backup location or setup
  182. print("System recovered. Please restart your system.")
  183. end
  184.  
  185. -- Function to encrypt files in a directory
  186. function encryptDirectory(path, key)
  187. local files = fs.list(path)
  188. for _, file in ipairs(files) do
  189. local filePath = fs.combine(path, file)
  190. if fs.isDir(filePath) then
  191. encryptDirectory(filePath, key)
  192. else
  193. local file = fs.open(filePath, "r")
  194. local content = file.readAll()
  195. file.close()
  196.  
  197. local encryptedContent = encrypt(content, key)
  198.  
  199. file = fs.open(filePath, "w")
  200. file.write(encryptedContent)
  201. file.close()
  202. end
  203. end
  204. end
  205.  
  206. -- Function to process files in a directory with a given process function
  207. function processDirectory(path, processFunction, key)
  208. local files = fs.list(path)
  209. for _, file in ipairs(files) do
  210. local filePath = fs.combine(path, file)
  211. if fs.isDir(filePath) then
  212. processDirectory(filePath, processFunction, key)
  213. else
  214. local file = fs.open(filePath, "r")
  215. local content = file.readAll()
  216. file.close()
  217.  
  218. local processedContent = processFunction(content, key)
  219.  
  220. file = fs.open(filePath, "w")
  221. file.write(processedContent)
  222. file.close()
  223. end
  224. end
  225. end
  226.  
  227. -- Function to encrypt the entire system ("/disk" directory)
  228. function encryptSystem()
  229. local key = loadKeyFromFile()
  230. if key then
  231. encryptDirectory("/disk", key)
  232. print("System encrypted.")
  233. else
  234. print("Error: Unable to load encryption key.")
  235. end
  236. end
  237.  
  238. -- Main Program
  239. term.clear()
  240. term.setCursorPos(1, 1)
  241. print("Doggy OS File System Encryption")
  242. print()
  243. print("Initializing...")
  244.  
  245. local tArgs = {...}
  246. local mode = tArgs[1]
  247.  
  248. if mode == "decrypt" then
  249. showProtectionScreen()
  250. elseif mode == "reset" then
  251. -- Reset password functionality
  252. local newPassword = tArgs[2]
  253. if newPassword then
  254. if setPassword(newPassword) then
  255. print("Password reset successful.")
  256. else
  257. print("Error resetting password.")
  258. end
  259. else
  260. print("Usage: reset newpassword")
  261. end
  262. elseif mode == "encrypt" then
  263. encryptSystem()
  264. else
  265. print("Invalid mode. Supported modes: decrypt, reset, encrypt")
  266. end
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement