Advertisement
DOGGYWOOF

DELETA FTER

Sep 3rd, 2024 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 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 randomNumber = math.random(1, 1000)
  60. local fileName = "key" .. randomNumber .. ".txt"
  61. local file = fs.open(fileName, "w")
  62. file.write(key)
  63. file.close()
  64. return fileName
  65. end
  66.  
  67. -- Function to load the key from a file and delete it after loading
  68. function loadKeyFromFileAndDelete(fileName)
  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. fs.delete(path) -- Delete the key file after loading
  76. return key
  77. else
  78. print("Error: Unable to open key file.")
  79. return nil
  80. end
  81. else
  82. print("Error: Key file does not exist.")
  83. return nil
  84. end
  85. end
  86.  
  87. -- Function to show protection screen options
  88. function showProtectionScreen(keyFileName)
  89. term.clear()
  90. term.setCursorPos(1, 1)
  91. print("Doggy OS File System Protection")
  92. print()
  93. print("F1 - Unlock")
  94. print("F8 - Shutdown")
  95. print("F9 - Recover System")
  96.  
  97. while true do
  98. local event, key = os.pullEvent("key")
  99. if key == keys.f1 then
  100. local key = loadKeyFromFileAndDelete(keyFileName)
  101. if key then
  102. print("Enter password:")
  103. local enteredPassword = read("*")
  104. local file = fs.open("hpass.txt", "r")
  105. local savedEncryptedPassword = file.readAll()
  106. file.close()
  107.  
  108. local encryptedPassword = encrypt(enteredPassword, key)
  109. if encryptedPassword == savedEncryptedPassword then
  110. processDirectory("/disk", decrypt, key)
  111. shell.run("/disk/boot/BIOS")
  112. else
  113. print("Error: Incorrect password.")
  114. end
  115. else
  116. print("Error: Unable to load encryption key.")
  117. end
  118. return
  119. elseif key == keys.f8 then
  120. os.shutdown()
  121. return
  122. elseif key == keys.f9 then
  123. recoverSystem()
  124. return
  125. end
  126. end
  127. end
  128.  
  129. -- Function to recover the system (delete disk and replace with recovery)
  130. function recoverSystem()
  131. local files = fs.list("/disk")
  132. for _, file in ipairs(files) do
  133. local filePath = fs.combine("/disk", file)
  134. if not fs.isDir(filePath) then
  135. fs.delete(filePath)
  136. end
  137. end
  138. -- Optionally copy recovery files from a backup location or setup
  139. print("System recovered. Please restart your system.")
  140. end
  141.  
  142. -- Function to encrypt files in a directory
  143. function encryptDirectory(path, key)
  144. local files = fs.list(path)
  145. for _, file in ipairs(files) do
  146. local filePath = fs.combine(path, file)
  147. if fs.isDir(filePath) then
  148. encryptDirectory(filePath, key)
  149. else
  150. local file = fs.open(filePath, "r")
  151. local content = file.readAll()
  152. file.close()
  153.  
  154. local encryptedContent = encrypt(content, key)
  155.  
  156. file = fs.open(filePath, "w")
  157. file.write(encryptedContent)
  158. file.close()
  159. end
  160. end
  161. end
  162.  
  163. -- Function to process files in a directory with a given process function
  164. function processDirectory(path, processFunction, key)
  165. local files = fs.list(path)
  166. for _, file in ipairs(files) do
  167. local filePath = fs.combine(path, file)
  168. if fs.isDir(filePath) then
  169. processDirectory(filePath, processFunction, key)
  170. else
  171. local file = fs.open(filePath, "r")
  172. local content = file.readAll()
  173. file.close()
  174.  
  175. local processedContent = processFunction(content, key)
  176.  
  177. file = fs.open(filePath, "w")
  178. file.write(processedContent)
  179. file.close()
  180. end
  181. end
  182. end
  183.  
  184. -- Function to set the password
  185. function setPassword(newPassword)
  186. local key = generateKey()
  187. local encryptedPassword = encrypt(newPassword, key)
  188.  
  189. -- Save the encrypted password to hpass.txt
  190. local file = fs.open("hpass.txt", "w")
  191. file.write(encryptedPassword)
  192. file.close()
  193.  
  194. local keyFileName = saveKeyToFile(key)
  195. print("Password set successfully.")
  196. print("Key saved to file: " .. keyFileName)
  197. end
  198.  
  199. -- Main Program
  200. term.clear()
  201. term.setCursorPos(1, 1)
  202. print("Doggy OS File System Encryption")
  203. print()
  204. print("Initializing...")
  205.  
  206. local tArgs = {...}
  207. local mode = tArgs[1]
  208.  
  209. if mode == "set" and tArgs[2] then
  210. local newPassword = tArgs[2]
  211. setPassword(newPassword)
  212. elseif mode == "encrypt" then
  213. local key = generateKey()
  214. encryptDirectory("/disk", key)
  215.  
  216. -- Save the password to hpass.txt
  217. print("Enter a password to secure your system:")
  218. local password = read("*")
  219. local encryptedPassword = encrypt(password, key)
  220.  
  221. -- Save the encrypted password to hpass.txt
  222. local file = fs.open("hpass.txt", "w")
  223. file.write(encryptedPassword)
  224. file.close()
  225.  
  226. local keyFileName = saveKeyToFile(key)
  227. print("All files in /disk/ encrypted.")
  228. print("Key saved to file: " .. keyFileName)
  229. print()
  230. print("Encryption complete.")
  231. else
  232. print("Doggy OS File System Protection")
  233. print()
  234. -- Check if a key file exists in the root directory
  235. local rootFiles = fs.list("/")
  236. local keyFile = nil
  237.  
  238. for _, file in ipairs(rootFiles) do
  239. if string.match(file, "^key%d+%.txt$") then
  240. keyFile = file
  241. break
  242. end
  243. end
  244.  
  245. if keyFile then
  246. showProtectionScreen(keyFile)
  247. else
  248. print("No encryption key found. Automatically encrypting files...")
  249. local key = generateKey()
  250. encryptDirectory("/disk", key)
  251.  
  252. -- Save the password to hpass.txt
  253. print("Enter a password to secure your system:")
  254. local password = read("*")
  255. local encryptedPassword = encrypt(password, key)
  256.  
  257. -- Save the encrypted password to hpass.txt
  258. local file = fs.open("hpass.txt", "w")
  259. file.write(encryptedPassword)
  260. file.close()
  261.  
  262. local keyFileName = saveKeyToFile(key)
  263. print("All files in /disk/ encrypted.")
  264. print("Key saved to file: " .. keyFileName)
  265. print()
  266. print("Encryption complete.")
  267. sleep(0.5)
  268. os.reboot()
  269. end
  270. end
  271.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement