Advertisement
DOGGYWOOF

DogLocker

Feb 11th, 2025 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. -- Helper function to generate a seed-based pseudo-random number generator
  2. function createPRNG(seed)
  3. local state = seed
  4. return function()
  5. state = (state * 16807) % 2147483647 -- A simple PRNG algorithm (multiplicative congruential)
  6. return state
  7. end
  8. end
  9.  
  10. -- XOR encryption function
  11. function xorEncrypt(data, key)
  12. local encryptedData = {}
  13. local prng = createPRNG(key) -- PRNG with the key
  14. for i = 1, #data do
  15. local keyByte = prng() % 256 -- Get a random byte for XORing
  16. encryptedData[i] = string.char(bit32.bxor(string.byte(data, i), keyByte))
  17. end
  18. return table.concat(encryptedData)
  19. end
  20.  
  21. -- XOR decryption function (symmetric)
  22. function xorDecrypt(encryptedData, key)
  23. return xorEncrypt(encryptedData, key) -- Decryption is the same as encryption with XOR
  24. end
  25.  
  26. -- Encrypt the entire disk directory (all files and subdirectories)
  27. function encryptDiskDirectory(diskPath, key)
  28. for _, fileName in pairs(fs.list(diskPath)) do
  29. local fullPath = diskPath .. "/" .. fileName
  30. if fs.isDir(fullPath) then
  31. encryptDiskDirectory(fullPath, key) -- Recursively encrypt subdirectories
  32. else
  33. local file = fs.open(fullPath, "r")
  34. local data = file.readAll()
  35. file.close()
  36.  
  37. -- Encrypt the file data and overwrite it
  38. local encryptedFile = xorEncrypt(data, key)
  39. local encryptedFilePath = fullPath .. ".enc"
  40. local encFile = fs.open(encryptedFilePath, "w")
  41. encFile.write(encryptedFile)
  42. encFile.close()
  43.  
  44. -- Optionally, delete the original file after encryption
  45. fs.delete(fullPath)
  46. end
  47. end
  48. end
  49.  
  50. -- Decrypt the entire disk directory (all files and subdirectories)
  51. function decryptDiskDirectory(diskPath, key)
  52. for _, fileName in pairs(fs.list(diskPath)) do
  53. local fullPath = diskPath .. "/" .. fileName
  54. if fs.isDir(fullPath) then
  55. decryptDiskDirectory(fullPath, key) -- Recursively decrypt subdirectories
  56. else
  57. local file = fs.open(fullPath, "r")
  58. local data = file.readAll()
  59. file.close()
  60.  
  61. -- Decrypt the file data and overwrite it
  62. local decryptedFile = xorDecrypt(data, key)
  63. local decryptedFilePath = fullPath:sub(1, -5) -- Remove the ".enc" suffix
  64. local decFile = fs.open(decryptedFilePath, "w")
  65. decFile.write(decryptedFile)
  66. decFile.close()
  67.  
  68. -- Optionally, delete the encrypted file after decryption
  69. fs.delete(fullPath)
  70. end
  71. end
  72. end
  73.  
  74. -- Load disk encryption key and data from the DogLocker subdirectory
  75. function loadDiskData(diskPath)
  76. local dirPath = "/disk/DogLocker/" .. diskPath:sub(6) -- Remove the "/disk" part of the path to use the name
  77.  
  78. if not fs.exists(dirPath) then
  79. return nil -- Disk directory not found
  80. end
  81.  
  82. -- Load the data from files in the subdirectory
  83. local labelFile = fs.open(dirPath .. "/label.txt", "r")
  84. local passwordFile = fs.open(dirPath .. "/password.txt", "r")
  85.  
  86. local label = labelFile.readLine()
  87. local encryptedPassword = passwordFile.readLine() -- Get the encrypted password
  88. labelFile.close()
  89. passwordFile.close()
  90.  
  91. -- Generate key from the disk path length (it will match what was used in encryption)
  92. local key = diskPath:len()
  93.  
  94. return { label = label, encryptedPassword = encryptedPassword, key = key }
  95. end
  96.  
  97. -- Store disk encryption data (Disk data, password, label) in the DogLocker subdirectory
  98. function storeDiskData(diskPath, label, password)
  99. local dirPath = "/disk/DogLocker/" .. diskPath:sub(6) -- Remove the "/disk" part of the path to use the name
  100.  
  101. if not fs.exists(dirPath) then
  102. fs.makeDir(dirPath)
  103. end
  104.  
  105. -- Encrypt the password before storing it
  106. local encryptedPassword = xorEncrypt(password, diskPath:len())
  107.  
  108. -- Save the label and encrypted password
  109. local labelFile = fs.open(dirPath .. "/label.txt", "w")
  110. local passwordFile = fs.open(dirPath .. "/password.txt", "w")
  111.  
  112. labelFile.writeLine(label)
  113. passwordFile.writeLine(encryptedPassword)
  114.  
  115. labelFile.close()
  116. passwordFile.close()
  117. end
  118.  
  119. -- Encrypt and store the entire disk directory
  120. function encryptDiskData(diskPath)
  121. local diskData = loadDiskData(diskPath)
  122. if not diskData then
  123. error("Disk data not found.")
  124. end
  125.  
  126. encryptDiskDirectory(diskPath, diskData.key)
  127. end
  128.  
  129. -- Decrypt the entire disk directory
  130. function decryptDiskData(diskPath, password)
  131. local diskData = loadDiskData(diskPath)
  132. if not diskData then
  133. error("Disk data not found.")
  134. end
  135.  
  136. -- Decrypt the password with the disk path length as the key
  137. local decryptedPassword = xorDecrypt(diskData.encryptedPassword, diskPath:len())
  138.  
  139. -- Validate password input when unlocking
  140. if decryptedPassword ~= password then
  141. error("Incorrect password.")
  142. end
  143.  
  144. decryptDiskDirectory(diskPath, diskData.key)
  145. end
  146.  
  147. -- List connected drives
  148. function listConnectedDrives()
  149. local drives = {}
  150. local driveCount = 0
  151. for _, disk in pairs(fs.list("/disk/")) do
  152. if fs.isDir("/disk/" .. disk) then
  153. driveCount = driveCount + 1
  154. table.insert(drives, "/disk/" .. disk)
  155. end
  156. end
  157. return drives
  158. end
  159.  
  160. -- UI for protecting and unlocking drives
  161. function displayUI()
  162. print("Welcome to DogLocker!")
  163. print("1. Protect (Encrypt) Drive")
  164. print("2. Unlock (Decrypt) Drive")
  165. print("3. Exit")
  166.  
  167. local choice = read()
  168.  
  169. if choice == "1" then
  170. protectDriveUI()
  171. elseif choice == "2" then
  172. unlockDriveUI()
  173. elseif choice == "3" then
  174. print("Exiting...")
  175. return
  176. else
  177. print("Invalid choice. Please try again.")
  178. displayUI()
  179. end
  180. end
  181.  
  182. -- UI for protecting a drive (encrypting data)
  183. function protectDriveUI()
  184. -- Ask the user for the exact path to encrypt
  185. print("Enter the full path of the drive to protect (e.g., /disk/, /disk2/):")
  186. local diskPath = read()
  187.  
  188. if not fs.exists(diskPath) or not fs.isDir(diskPath) then
  189. print("Invalid disk path. Please try again.")
  190. return
  191. end
  192.  
  193. print("You selected: " .. diskPath)
  194.  
  195. print("Enter label for the disk:")
  196. local label = read()
  197.  
  198. print("Enter password to protect the disk:")
  199. local password = read("#") -- Hide password input
  200.  
  201. -- Store the disk's information and encrypt its data
  202. storeDiskData(diskPath, label, password)
  203. encryptDiskData(diskPath)
  204.  
  205. print("Drive protected successfully.")
  206. displayUI()
  207. end
  208.  
  209. -- UI for unlocking a drive (decrypting data)
  210. function unlockDriveUI()
  211. -- Ask the user for the exact path to unlock
  212. print("Enter the full path of the drive to unlock (e.g., /disk/, /disk2/):")
  213. local diskPath = read()
  214.  
  215. if not fs.exists(diskPath) or not fs.isDir(diskPath) then
  216. print("Invalid disk path. Please try again.")
  217. return
  218. end
  219.  
  220. print("You selected: " .. diskPath)
  221.  
  222. print("Enter password to unlock the disk:")
  223. local password = read("#") -- Hide password input
  224.  
  225. -- Decrypt the disk's data
  226. local status, err = pcall(function() decryptDiskData(diskPath, password) end)
  227.  
  228. if status then
  229. print("Drive unlocked successfully.")
  230. else
  231. print("Error: " .. err)
  232. end
  233.  
  234. displayUI()
  235. end
  236.  
  237. -- Start the UI
  238. displayUI()
  239.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement