Advertisement
DOGGYWOOF

Doggy OS Bitlocker

Sep 3rd, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.20 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. print("F2 - Change Password")
  97. print("F3 - Force Reset Password")
  98.  
  99. -- If password protection is enabled
  100. if fs.exists("hash.txt") then
  101. if not promptForPassword() then
  102. print("Incorrect password.")
  103. sleep(2)
  104. return
  105. end
  106. end
  107.  
  108. while true do
  109. local event, key = os.pullEvent("key")
  110. if key == keys.f1 then
  111. local key = loadKeyFromFileAndDelete(keyFileName)
  112. if key then
  113. processDirectory("/disk", decrypt, key)
  114. shell.run("/disk/boot/BIOS")
  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. elseif key == keys.f2 then
  126. if fs.exists("hash.txt") then
  127. changePassword() -- Allow password change if hash.txt exists
  128. else
  129. print("No password is set.")
  130. sleep(2)
  131. end
  132. return
  133. elseif key == keys.f3 then
  134. forceResetPassword() -- Allow password reset
  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. -- Function to hash a password
  196. function hashPassword(password)
  197. local hash = ""
  198. for i = 1, #password do
  199. local char = password:sub(i, i)
  200. local ascii = string.byte(char)
  201. hash = hash .. string.char((ascii + 5) % 256) -- Simple "hash" by shifting ASCII values
  202. end
  203. return hash
  204. end
  205.  
  206. -- Function to verify a password
  207. function verifyPassword(inputPassword)
  208. local hashFileName = "hash.txt"
  209. if fs.exists(hashFileName) then
  210. local file = fs.open(hashFileName, "r")
  211. local storedHash = file.readAll()
  212. file.close()
  213.  
  214. local inputHash = hashPassword(inputPassword)
  215. return inputHash == storedHash
  216. else
  217. return false
  218. end
  219. end
  220.  
  221. -- Function to set a new password
  222. function setPassword()
  223. term.clear()
  224. term.setCursorPos(1, 1)
  225. print("Set a new password:")
  226. local password = read()
  227. local hash = hashPassword(password)
  228.  
  229. local file = fs.open("hash.txt", "w")
  230. file.write(hash)
  231. file.close()
  232.  
  233. print("Password set successfully.")
  234. sleep(2)
  235. end
  236.  
  237. -- Function to change the password
  238. function changePassword()
  239. term.clear()
  240. term.setCursorPos(1, 1)
  241. print("Enter current password:")
  242. local oldPassword = read()
  243.  
  244. if not verifyPassword(oldPassword) then
  245. print("Incorrect password.")
  246. sleep(2)
  247. return
  248. end
  249.  
  250. print("Enter new password:")
  251. local newPassword = read()
  252. local newHash = hashPassword(newPassword)
  253.  
  254. local file = fs.open("hash.txt", "w")
  255. file.write(newHash)
  256. file.close()
  257.  
  258. print("Password changed successfully.")
  259. sleep(2)
  260. end
  261.  
  262. -- Function to force reset the password
  263. function forceResetPassword()
  264. term.clear()
  265. term.setCursorPos(1, 1)
  266. print("Warning: This will reset the password.")
  267. print("Enter new password:")
  268. local newPassword = read()
  269. local newHash = hashPassword(newPassword)
  270.  
  271. local file = fs.open("hash.txt", "w")
  272. file.write(newHash)
  273. file.close()
  274.  
  275. print("Password reset successfully.")
  276. sleep(2)
  277. end
  278.  
  279. -- Function to prompt for the password
  280. function promptForPassword()
  281. term.clear()
  282. term.setCursorPos(1, 1)
  283. print("Enter password to unlock:")
  284. local password = read()
  285. return verifyPassword(password)
  286. end
  287.  
  288. -- Main Program
  289. term.clear()
  290. term.setCursorPos(1, 1)
  291. print("Doggy OS File System Encryption")
  292. print()
  293. print("Initializing...")
  294.  
  295. local tArgs = {...}
  296. local mode = tArgs[1]
  297.  
  298. if mode == "encrypt" then
  299. -- Check if the password file exists
  300. if not fs.exists("hash.txt") then
  301. setPassword() -- Prompt user to set a password if not already set
  302. end
  303.  
  304. local key = generateKey()
  305. encryptDirectory("/disk", key)
  306. local keyFileName = saveKeyToFile(key)
  307. print("All files in /disk/ encrypted.")
  308. print("Key saved to file: " .. keyFileName)
  309. print()
  310. print("Encryption complete.")
  311. else
  312. print("Doggy OS File System Protection")
  313. print()
  314. -- Check if a key file exists in the root directory
  315. local rootFiles = fs.list("/")
  316. local keyFile = nil
  317.  
  318. for _, file in ipairs(rootFiles) do
  319. if string.match(file, "^key%d+%.txt$") then
  320. keyFile = file
  321. break
  322. end
  323. end
  324.  
  325. if keyFile then
  326. showProtectionScreen(keyFile)
  327. else
  328. print("No encryption key found. Automatically encrypting files...")
  329. local key = generateKey()
  330. encryptDirectory("/disk", key)
  331. local keyFileName = saveKeyToFile(key)
  332. print("All files in /disk/ encrypted.")
  333. print("Key saved to file: " .. keyFileName)
  334. print()
  335. print("Encryption complete.")
  336. end
  337. end
  338.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement