Advertisement
DOGGYWOOF

Untitled

Jul 17th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 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. processDirectory("/disk", decrypt, key)
  103. shell.run("/disk/boot/BIOS")
  104. else
  105. print("Error: Unable to load encryption key.")
  106. end
  107. return
  108. elseif key == keys.f8 then
  109. os.shutdown()
  110. return
  111. elseif key == keys.f9 then
  112. recoverSystem()
  113. return
  114. end
  115. end
  116. end
  117.  
  118. -- Function to recover the system (delete disk and replace with recovery)
  119. function recoverSystem()
  120. local files = fs.list("/disk")
  121. for _, file in ipairs(files) do
  122. local filePath = fs.combine("/disk", file)
  123. if not fs.isDir(filePath) then
  124. fs.delete(filePath)
  125. end
  126. end
  127. -- Optionally copy recovery files from a backup location or setup
  128. print("System recovered. Please restart your system.")
  129. end
  130.  
  131. -- Function to encrypt files in a directory
  132. function encryptDirectory(path, key)
  133. local files = fs.list(path)
  134. for _, file in ipairs(files) do
  135. local filePath = fs.combine(path, file)
  136. if fs.isDir(filePath) then
  137. encryptDirectory(filePath, key)
  138. else
  139. local file = fs.open(filePath, "r")
  140. local content = file.readAll()
  141. file.close()
  142.  
  143. local encryptedContent = encrypt(content, key)
  144.  
  145. file = fs.open(filePath, "w")
  146. file.write(encryptedContent)
  147. file.close()
  148. end
  149. end
  150. end
  151.  
  152. -- Function to process files in a directory with a given process function
  153. function processDirectory(path, processFunction, key)
  154. local files = fs.list(path)
  155. for _, file in ipairs(files) do
  156. local filePath = fs.combine(path, file)
  157. if fs.isDir(filePath) then
  158. processDirectory(filePath, processFunction, key)
  159. else
  160. local file = fs.open(filePath, "r")
  161. local content = file.readAll()
  162. file.close()
  163.  
  164. local processedContent = processFunction(content, key)
  165.  
  166. file = fs.open(filePath, "w")
  167. file.write(processedContent)
  168. file.close()
  169. end
  170. end
  171. end
  172.  
  173. -- Main Program
  174. term.clear()
  175. term.setCursorPos(1, 1)
  176. print("Doggy OS File System Encryption")
  177. print()
  178. print("Initializing...")
  179.  
  180. local tArgs = {...}
  181. local mode = tArgs[1]
  182.  
  183. if mode == "encrypt" then
  184. local key = generateKey()
  185. encryptDirectory("/disk", key)
  186. local keyFileName = saveKeyToFile(key)
  187. print("All files in /disk/ encrypted.")
  188. print("Key saved to file: " .. keyFileName)
  189. print()
  190. print("Encryption complete.")
  191. else
  192. print("Doggy OS File System Protection")
  193. print()
  194. -- Check if a key file exists in the root directory
  195. local rootFiles = fs.list("/")
  196. local keyFile = nil
  197.  
  198. for _, file in ipairs(rootFiles) do
  199. if string.match(file, "^key%d+%.txt$") then
  200. keyFile = file
  201. break
  202. end
  203. end
  204.  
  205. if keyFile then
  206. showProtectionScreen(keyFile)
  207. else
  208. print("No encryption key found. Automatically encrypting files...")
  209. local key = generateKey()
  210. encryptDirectory("/disk", key)
  211. local keyFileName = saveKeyToFile(key)
  212. print("All files in /disk/ encrypted.")
  213. print("Key saved to file: " .. keyFileName)
  214. print()
  215. print("Encryption complete.")
  216. sleep(0.5)
  217. os.reboot()
  218. end
  219. end
  220.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement