Advertisement
DOGGYWOOF

Password on boot encryption

Jul 17th, 2024 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 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("password.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. -- Main Program
  185. term.clear()
  186. term.setCursorPos(1, 1)
  187. print("Doggy OS File System Encryption")
  188. print()
  189. print("Initializing...")
  190.  
  191. local tArgs = {...}
  192. local mode = tArgs[1]
  193.  
  194. if mode == "encrypt" then
  195. local key = generateKey()
  196. encryptDirectory("/disk", key)
  197.  
  198. -- Ask user for password
  199. print("Enter a password to secure your system:")
  200. local password = read("*")
  201. local encryptedPassword = encrypt(password, key)
  202.  
  203. -- Save the encrypted password to password.txt
  204. local file = fs.open("password.txt", "w")
  205. file.write(encryptedPassword)
  206. file.close()
  207.  
  208. local keyFileName = saveKeyToFile(key)
  209. print("All files in /disk/ encrypted.")
  210. print("Key saved to file: " .. keyFileName)
  211. print()
  212. print("Encryption complete.")
  213. else
  214. print("Doggy OS File System Protection")
  215. print()
  216. -- Check if a key file exists in the root directory
  217. local rootFiles = fs.list("/")
  218. local keyFile = nil
  219.  
  220. for _, file in ipairs(rootFiles) do
  221. if string.match(file, "^key%d+%.txt$") then
  222. keyFile = file
  223. break
  224. end
  225. end
  226.  
  227. if keyFile then
  228. showProtectionScreen(keyFile)
  229. else
  230. print("No encryption key found. Automatically encrypting files...")
  231. local key = generateKey()
  232. encryptDirectory("/disk", key)
  233.  
  234. -- Ask user for password
  235. print("Enter a password to secure your system:")
  236. local password = read("*")
  237. local encryptedPassword = encrypt(password, key)
  238.  
  239. -- Save the encrypted password to password.txt
  240. local file = fs.open("password.txt", "w")
  241. file.write(encryptedPassword)
  242. file.close()
  243.  
  244. local keyFileName = saveKeyToFile(key)
  245. print("All files in /disk/ encrypted.")
  246. print("Key saved to file: " .. keyFileName)
  247. print()
  248. print("Encryption complete.")
  249. sleep(0.5)
  250. os.reboot()
  251. end
  252. end
  253.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement