Advertisement
DOGGYWOOF

Untitled

Oct 5th, 2024 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 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 = "encryption_key.txt"
  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
  67. function loadKeyFromFile(fileName)
  68. local path = fs.combine("/", fileName)
  69. if fs.exists(path) then
  70. local file = fs.open(path, "r")
  71. if file then
  72. local key = file.readAll()
  73. file.close()
  74. return key
  75. else
  76. print("Error: Unable to open key file.")
  77. return nil
  78. end
  79. else
  80. print("Error: Key file does not exist.")
  81. return nil
  82. end
  83. end
  84.  
  85. -- Function to show protection screen options
  86. function showProtectionScreen(key)
  87. term.clear()
  88. term.setCursorPos(1, 1)
  89. print("Doggy OS File System Protection")
  90. print()
  91. print("F1 - Unlock")
  92. print("F8 - Shutdown")
  93. print("F9 - Recover System")
  94.  
  95. while true do
  96. local event, keyPress = os.pullEvent("key")
  97. if keyPress == keys.f1 then
  98. print("Enter password:")
  99. local enteredPassword = read("*")
  100. local file = fs.open("password.txt", "r")
  101. local savedEncryptedPassword = file.readAll()
  102. file.close()
  103.  
  104. local encryptedPassword = encrypt(enteredPassword, key)
  105. if encryptedPassword == savedEncryptedPassword then
  106. processDirectory("/disk", decrypt, key)
  107. shell.run("/disk/boot/BIOS")
  108. else
  109. print("Error: Incorrect password.")
  110. end
  111. return
  112. elseif keyPress == keys.f8 then
  113. os.shutdown()
  114. return
  115. elseif keyPress == keys.f9 then
  116. recoverSystem()
  117. return
  118. end
  119. end
  120. end
  121.  
  122. -- Function to recover the system (delete disk and replace with recovery)
  123. function recoverSystem()
  124. local files = fs.list("/disk")
  125. for _, file in ipairs(files) do
  126. local filePath = fs.combine("/disk", file)
  127. if not fs.isDir(filePath) then
  128. fs.delete(filePath)
  129. end
  130. end
  131. print("System recovered. Please restart your system.")
  132. end
  133.  
  134. -- Function to encrypt files in a directory
  135. function encryptDirectory(path, key)
  136. local files = fs.list(path)
  137. for _, file in ipairs(files) do
  138. local filePath = fs.combine(path, file)
  139. if fs.isDir(filePath) then
  140. encryptDirectory(filePath, key)
  141. else
  142. local file = fs.open(filePath, "r")
  143. local content = file.readAll()
  144. file.close()
  145.  
  146. local encryptedContent = encrypt(content, key)
  147.  
  148. file = fs.open(filePath, "w")
  149. file.write(encryptedContent)
  150. file.close()
  151. end
  152. end
  153. end
  154.  
  155. -- Function to process files in a directory with a given process function
  156. function processDirectory(path, processFunction, key)
  157. local files = fs.list(path)
  158. for _, file in ipairs(files) do
  159. local filePath = fs.combine(path, file)
  160. if fs.isDir(filePath) then
  161. processDirectory(filePath, processFunction, key)
  162. else
  163. local file = fs.open(filePath, "r")
  164. local content = file.readAll()
  165. file.close()
  166.  
  167. local processedContent = processFunction(content, key)
  168.  
  169. file = fs.open(filePath, "w")
  170. file.write(processedContent)
  171. file.close()
  172. end
  173. end
  174. end
  175.  
  176. -- Function to setup the encryption system
  177. function setupEncryptionSystem()
  178. local key = generateKey()
  179. print("Enter a password to secure your system:")
  180. local password = read("*")
  181. local encryptedPassword = encrypt(password, key)
  182.  
  183. -- Save the encrypted password to password.txt
  184. local file = fs.open("password.txt", "w")
  185. file.write(encryptedPassword)
  186. file.close()
  187.  
  188. saveKeyToFile(key)
  189. print("Setup complete. Encryption key and password saved.")
  190. end
  191.  
  192. -- Main Program
  193. term.clear()
  194. term.setCursorPos(1, 1)
  195. print("Doggy OS File System Encryption")
  196. print()
  197. print("Initializing...")
  198.  
  199. local tArgs = {...}
  200. local mode = tArgs[1]
  201.  
  202. if mode == "setup" then
  203. setupEncryptionSystem()
  204. else
  205. local key = loadKeyFromFile("encryption_key.txt")
  206. if not key then
  207. print("No encryption key found. Please run with 'setup' to create a new encryption system.")
  208. return
  209. end
  210.  
  211. print("Doggy OS File System Protection")
  212. showProtectionScreen(key)
  213. end
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement