Advertisement
DOGGYWOOF

Untitled

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