Advertisement
DOGGYWOOF

Doggy OS Secure External ecnryption system

Sep 6th, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. -- Define a key for XOR encryption/decryption
  2. local key = 123 -- Change this to a more secure key in practice
  3.  
  4. -- Function to encrypt/decrypt a string using XOR
  5. local function xorEncryptDecrypt(data, key)
  6. local result = ""
  7. for i = 1, #data do
  8. result = result .. string.char(bit.bxor(data:byte(i), key))
  9. end
  10. return result
  11. end
  12.  
  13. -- Encrypt function
  14. local function encryptDirectory(directory)
  15. local files = fs.list(directory)
  16. for _, file in ipairs(files) do
  17. local path = fs.combine(directory, file)
  18. if fs.isDir(path) then
  19. -- Recursively encrypt directories
  20. encryptDirectory(path)
  21. else
  22. local fileHandle = fs.open(path, "r")
  23. local content = fileHandle.readAll()
  24. fileHandle.close()
  25. local encryptedContent = xorEncryptDecrypt(content, key)
  26. fileHandle = fs.open(path, "w")
  27. fileHandle.write(encryptedContent)
  28. fileHandle.close()
  29. print("Encrypted: " .. path)
  30. end
  31. end
  32. end
  33.  
  34. -- Decrypt function
  35. local function decryptDirectory(directory)
  36. local files = fs.list(directory)
  37. for _, file in ipairs(files) do
  38. local path = fs.combine(directory, file)
  39. if fs.isDir(path) then
  40. -- Recursively decrypt directories
  41. decryptDirectory(path)
  42. else
  43. local fileHandle = fs.open(path, "r")
  44. local content = fileHandle.readAll()
  45. fileHandle.close()
  46. local decryptedContent = xorEncryptDecrypt(content, key)
  47. fileHandle = fs.open(path, "w")
  48. fileHandle.write(decryptedContent)
  49. fileHandle.close()
  50. print("Decrypted: " .. path)
  51. end
  52. end
  53. end
  54.  
  55. -- Function to prompt for input and return a trimmed string
  56. local function prompt(promptText)
  57. write(promptText)
  58. return read():gsub("^%s*(.-)%s*$", "%1") -- Trim spaces
  59. end
  60.  
  61. -- Function to check if user credentials are correct
  62. local function checkCredentials(username, password)
  63. local userFolder = "/disk/users/" .. username
  64. if not fs.exists(userFolder) then
  65. print("User folder does not exist: " .. userFolder)
  66. return false
  67. end
  68.  
  69. local passwordFile = userFolder .. "/password.txt"
  70. if not fs.exists(passwordFile) then
  71. print("Password file does not exist: " .. passwordFile)
  72. return false
  73. end
  74.  
  75. local storedPassword = fs.open(passwordFile, "r").readAll()
  76. if storedPassword ~= password then
  77. print("Incorrect password.")
  78. return false
  79. end
  80.  
  81. return true
  82. end
  83.  
  84. -- Function to encrypt a disk
  85. local function encryptDisk()
  86. local username = prompt("Enter username: ")
  87. local password = prompt("Enter password: ")
  88.  
  89. if not checkCredentials(username, password) then
  90. print("Access denied.")
  91. return
  92. end
  93.  
  94. -- Check for disk insertion
  95. local disk = "/disk2/"
  96. if not fs.exists(disk) then
  97. print("No disk detected.")
  98. return
  99. end
  100.  
  101. -- Check for files containing VA11-ILLA or setup
  102. local function containsRestrictedFiles(directory)
  103. local files = fs.list(directory)
  104. for _, file in ipairs(files) do
  105. local path = fs.combine(directory, file)
  106. if fs.isDir(path) then
  107. if containsRestrictedFiles(path) then
  108. return true
  109. end
  110. elseif file:find("VA11-ILLA") or file:find("setup") then
  111. return true
  112. end
  113. end
  114. return false
  115. end
  116.  
  117. if containsRestrictedFiles(disk) then
  118. print("Access denied. Disk contains restricted files.")
  119. return
  120. end
  121.  
  122. -- Ask if user wants to keep a backup
  123. local backup = prompt("Do you want to keep a backup of the data? (yes/no): ")
  124. if backup:lower() == "yes" then
  125. local backupDir = disk .. "-backup/"
  126. if fs.exists(backupDir) then
  127. fs.delete(backupDir) -- Delete if it exists
  128. end
  129. fs.copy(disk, backupDir)
  130. print("Backup created.")
  131. end
  132.  
  133. -- Encrypt the disk
  134. encryptDirectory(disk)
  135. print("Disk encrypted.")
  136.  
  137. -- Store the encryption key
  138. local keyFile = fs.open("/disk/users/" .. username .. "/encryption_key.txt", "w")
  139. keyFile.write(tostring(key))
  140. keyFile.close()
  141. print("Encryption key saved.")
  142. end
  143.  
  144. -- Function to decrypt a disk
  145. local function decryptDisk()
  146. local username = prompt("Enter username: ")
  147. local password = prompt("Enter password: ")
  148.  
  149. if not checkCredentials(username, password) then
  150. print("Access denied.")
  151. return
  152. end
  153.  
  154. -- Check for disk insertion
  155. local disk = "/disk2/"
  156. if not fs.exists(disk) then
  157. print("No disk detected.")
  158. return
  159. end
  160.  
  161. -- Load the encryption key
  162. local keyFile = "/disk/users/" .. username .. "/encryption_key.txt"
  163. if not fs.exists(keyFile) then
  164. print("No encryption key found.")
  165. return
  166. end
  167.  
  168. key = tonumber(fs.open(keyFile, "r").readAll())
  169.  
  170. -- Decrypt the disk
  171. decryptDirectory(disk)
  172. print("Disk decrypted.")
  173.  
  174. -- Show directories and files
  175. local function showFiles(directory, indent)
  176. indent = indent or ""
  177. local files = fs.list(directory)
  178. for _, file in ipairs(files) do
  179. local path = fs.combine(directory, file)
  180. if fs.isDir(path) then
  181. print(indent .. "[" .. file .. "]")
  182. showFiles(path, indent .. " ")
  183. else
  184. print(indent .. file)
  185. end
  186. end
  187. end
  188.  
  189. showFiles(disk)
  190. end
  191.  
  192. -- Main program loop
  193. while true do
  194. print("1. Encrypt Disk")
  195. print("2. Decrypt Disk")
  196. print("3. Exit")
  197. local choice = tonumber(read())
  198.  
  199. if choice == 1 then
  200. encryptDisk()
  201. elseif choice == 2 then
  202. decryptDisk()
  203. elseif choice == 3 then
  204. break
  205. else
  206. print("Invalid choice.")
  207. end
  208. end
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement