Advertisement
DOGGYWOOF

e

Jul 15th, 2024 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 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, dir)
  59. local randomNumber = math.random(1, 1000)
  60. local fileName = dir .. "/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
  68. function loadKeyFromFile(fileName)
  69. local file = fs.open(fileName, "r")
  70. local key = file.readAll()
  71. file.close()
  72. return key
  73. end
  74.  
  75. -- Function to find the key file in a directory
  76. function findKeyFile(dir)
  77. local files = fs.list(dir)
  78. for _, file in ipairs(files) do
  79. if file:match("^key%d+%.txt$") then
  80. return dir .. "/" .. file
  81. end
  82. end
  83. return nil
  84. end
  85.  
  86. -- Function to load the encrypted text from the password file
  87. function loadEncryptedText(userDir)
  88. local fileName = userDir .. "/password.txt"
  89. if not fs.exists(fileName) then
  90. print("Password file does not exist.")
  91. return nil
  92. end
  93. local file = fs.open(fileName, "r")
  94. local encryptedText = file.readAll()
  95. file.close()
  96. return encryptedText
  97. end
  98.  
  99. -- Function to create a new user
  100. function createUser()
  101. term.clear()
  102. term.setCursorPos(1, 1)
  103. print("Enter new username:")
  104. local username = read()
  105.  
  106. -- Check if the username already exists
  107. if fs.exists("/disk/users/" .. username) then
  108. print("Username already exists. Try again.")
  109. sleep(2)
  110. return
  111. end
  112.  
  113. -- Create a directory for the new user
  114. fs.makeDir("/disk/users/" .. username)
  115.  
  116. -- Prompt for password
  117. local password
  118. repeat
  119. term.clear()
  120. term.setCursorPos(1, 1)
  121. print("Enter password:")
  122. password = read("*")
  123.  
  124. print("Confirm password:")
  125. local confirmPassword = read("*")
  126.  
  127. if password ~= confirmPassword then
  128. print("Passwords do not match. Try again.")
  129. sleep(2)
  130. end
  131. until password == confirmPassword
  132.  
  133. -- Generate encryption key
  134. local key = generateKey()
  135.  
  136. -- Encrypt the password
  137. local encryptedPassword = encrypt(password, key)
  138.  
  139. -- Save the key to a file
  140. local keyFileName = saveKeyToFile(key, "/disk/users/" .. username)
  141.  
  142. -- Store the encrypted password in a text file
  143. local passwordFile = fs.open("/disk/users/" .. username .. "/password.txt", "w")
  144. passwordFile.write(encryptedPassword)
  145. passwordFile.close()
  146.  
  147. print("User created successfully.")
  148. sleep(2)
  149. end
  150.  
  151. -- Function to delete a user
  152. function deleteUser()
  153. term.clear()
  154. term.setCursorPos(1, 1)
  155. print("Enter username to delete:")
  156. local username = read()
  157.  
  158. -- Check if the username exists
  159. if not fs.exists("/disk/users/" .. username) then
  160. print("User not found. Try again.")
  161. sleep(2)
  162. return
  163. end
  164.  
  165. -- Check for the presence of block.txt
  166. if fs.exists("/disk/users/" .. username .. "/block.txt") then
  167. -- Display content of block.txt as an error message
  168. local file = fs.open("/disk/users/" .. username .. "/block.txt", "r")
  169. local errorMessage = file.readAll()
  170. file.close()
  171. print(errorMessage)
  172. sleep(2)
  173. return
  174. end
  175.  
  176. -- Prompt for password to confirm deletion
  177. print("Enter password to confirm deletion:")
  178. local inputPassword = read("*")
  179.  
  180. -- Load the key from the file
  181. local keyFileName = findKeyFile("/disk/users/" .. username)
  182. local key = loadKeyFromFile(keyFileName)
  183.  
  184. -- Read and decrypt stored password from the file
  185. local encryptedPassword = loadEncryptedText("/disk/users/" .. username)
  186. local storedPassword = decrypt(encryptedPassword, key)
  187.  
  188. -- Compare entered password with stored password
  189. if inputPassword == storedPassword then
  190. -- Delete the user directory
  191. fs.delete("/disk/users/" .. username)
  192. print("User deleted successfully.")
  193. else
  194. print("Incorrect password. Deletion failed.")
  195. end
  196.  
  197. sleep(2)
  198. end
  199.  
  200. -- Function for password recovery by admin
  201. function passwordRecovery()
  202. term.clear()
  203. term.setCursorPos(1, 1)
  204. print("Enter admin username:")
  205. local adminUsername = read()
  206.  
  207. -- Check if the entered user has admin privileges
  208. if not fs.exists("/disk/users/" .. adminUsername .. "/admin.txt") then
  209. print("Permission denied. Admin access required.")
  210. sleep(2)
  211. return
  212. end
  213.  
  214. -- Prompt for admin password
  215. print("Enter admin password:")
  216. local adminPassword = read("*")
  217.  
  218. -- Load the key from the file
  219. local keyFileName = findKeyFile("/disk/users/" .. adminUsername)
  220. local key = loadKeyFromFile(keyFileName)
  221.  
  222. -- Read and decrypt stored admin password from the file
  223. local encryptedAdminPassword = loadEncryptedText("/disk/users/" .. adminUsername)
  224. local storedAdminPassword = decrypt(encryptedAdminPassword, key)
  225.  
  226. -- Compare entered admin password with stored admin password
  227. if adminPassword == storedAdminPassword then
  228. print("Enter username for password reset:")
  229. local username = read()
  230.  
  231. -- Check if the username exists
  232. if not fs.exists("/disk/users/" .. username) then
  233. print("User not found. Try again.")
  234. sleep(2)
  235. return
  236. end
  237.  
  238. -- Confirm password reset
  239. print("Are you sure you want to reset the password for " .. username .. "? (y/n)")
  240. local confirm = read()
  241. if confirm:lower() ~= "y" then
  242. print("Password reset canceled.")
  243. sleep(2)
  244. return
  245. end
  246.  
  247. -- Prompt for new password
  248. term.clear()
  249. term.setCursorPos(1, 1)
  250. print("Enter new password for " .. username .. ":")
  251. local newPassword = read("*")
  252.  
  253. -- Generate encryption key
  254. local key = generateKey()
  255.  
  256. -- Encrypt the new password
  257. local encryptedPassword = encrypt(newPassword, key)
  258.  
  259. -- Save the key to a file
  260. local keyFileName = saveKeyToFile(key, "/disk/users/" .. username)
  261.  
  262. -- Store the encrypted password in the text file
  263. local passwordFile = fs.open("/disk/users/" .. username .. "/password.txt", "w")
  264. passwordFile.write(encryptedPassword)
  265. passwordFile.close()
  266.  
  267. print("Password reset successfully.")
  268. else
  269. print("Incorrect admin password. Password reset failed.")
  270. end
  271.  
  272. sleep(2)
  273. end
  274.  
  275. -- Function to exit and run /disk/os/gui
  276. function exitProgram()
  277. term.clear()
  278. term.setCursorPos(1, 1)
  279. print("Exiting program...")
  280. sleep(1)
  281. shell.run("/disk/os/gui")
  282. error("Exiting program.")
  283. end
  284.  
  285. -- Function for Security Card Login (insert or enter ID)
  286. function securityCardLogin(username)
  287. while true do
  288. term.clear()
  289. term.setCursorPos(1, 1)
  290. print("Insert Security Card for " .. username .. " or enter User ID:")
  291. local input = read()
  292.  
  293. -- Check if the username exists
  294. if not fs.exists("/disk/users/" .. username) then
  295. print("User not found. Try again.")
  296. sleep(2)
  297. else
  298. -- If input matches the user ID, login successful
  299. if input == username then
  300. print("Security Card login successful.")
  301. sleep(2)
  302. return
  303. else
  304. print("Security Card login failed. Try again.")
  305. sleep(2)
  306. end
  307. end
  308. end
  309. end
  310.  
  311. -- Function to display the main menu
  312. function mainMenu()
  313. while true do
  314. term.clear()
  315. term.setCursorPos(1, 1)
  316. print("Main Menu:")
  317. print("1. Create User")
  318. print("2. Delete User")
  319. print("3. Password Recovery")
  320. print("4. Exit")
  321.  
  322. local choice = read()
  323.  
  324. if choice == "1" then
  325. createUser()
  326. elseif choice == "2" then
  327. deleteUser()
  328. elseif choice == "3" then
  329. passwordRecovery()
  330. elseif choice == "4" then
  331. exitProgram()
  332. else
  333. print("Invalid choice. Try again.")
  334. sleep(2)
  335. end
  336. end
  337. end
  338.  
  339. -- Main program entry point
  340. mainMenu()
  341.  
  342.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement