Advertisement
DOGGYWOOF

Untitled

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