Advertisement
DOGGYWOOF

Untitled

Jul 15th, 2024 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.47 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 save encrypted password to a file
  87. function saveEncryptedPassword(username, encryptedPassword)
  88. local userDir = "/disk/users/" .. username
  89. local passwordFile = fs.open(userDir .. "/password.txt", "w")
  90. passwordFile.write(encryptedPassword)
  91. passwordFile.close()
  92. end
  93.  
  94. -- Function to load encrypted password from a file
  95. function loadEncryptedPassword(username)
  96. local userDir = "/disk/users/" .. username
  97. local fileName = userDir .. "/password.txt"
  98. if not fs.exists(fileName) then
  99. print("Password file does not exist.")
  100. return nil
  101. end
  102. local file = fs.open(fileName, "r")
  103. local encryptedPassword = file.readAll()
  104. file.close()
  105. return encryptedPassword
  106. end
  107.  
  108. -- Function to encrypt a password using a key
  109. function encryptPassword(password, key)
  110. return encrypt(password, key)
  111. end
  112.  
  113. -- Function to decrypt a password using a key
  114. function decryptPassword(encryptedPassword, key)
  115. return decrypt(encryptedPassword, key)
  116. end
  117.  
  118. -- Function to select and encrypt username
  119. function selectAndEncryptUsername()
  120. term.clear()
  121. term.setCursorPos(1, 1)
  122. print("Enter username:")
  123. local username = read()
  124.  
  125. local key = generateKey()
  126. local encryptedUsername = encrypt(username, key)
  127. local keyFileName = saveKeyToFile(key, "/disk/users/" .. username)
  128.  
  129. return {
  130. encryptedUsername = encryptedUsername,
  131. keyFileName = keyFileName
  132. }
  133. end
  134.  
  135. -- Function to decrypt username
  136. function decryptUsername(encryptedUsername, keyFileName)
  137. local key = loadKeyFromFile(keyFileName)
  138. if not key then
  139. print("Encryption key not found.")
  140. return nil
  141. end
  142. return decrypt(encryptedUsername, key)
  143. end
  144.  
  145. -- Function for Security Card Login (insert or enter ID)
  146. function securityCardLogin(username)
  147. while true do
  148. term.clear()
  149. term.setCursorPos(1, 1)
  150. print("Security Card Manager for User:", username)
  151. print("1. Add Card")
  152. print("2. Remove Cards")
  153. print("3. Show All Security Cards")
  154. print("4. Disable Security Card Login")
  155. print("5. Back to Main Menu")
  156.  
  157. local choice = read()
  158.  
  159. if choice == "1" then
  160. addCardMenu(username)
  161. elseif choice == "2" then
  162. removeCardsMenu(username)
  163. elseif choice == "3" then
  164. showAllSecurityCards(username)
  165. elseif choice == "4" then
  166. disableSecurityCardLogin(username)
  167. elseif choice == "5" then
  168. return -- Back to main menu
  169. else
  170. print("Invalid choice. Try again.")
  171. sleep(2)
  172. end
  173. end
  174. end
  175.  
  176. -- Function for Add Card submenu
  177. function addCardMenu(username)
  178. while true do
  179. term.clear()
  180. term.setCursorPos(1, 1)
  181. print("Add Card")
  182. print("1. Insert Security Card")
  183. print("2. Enter Security Card ID")
  184. print("3. Back to Security Card Manager")
  185.  
  186. local choice = read()
  187.  
  188. if choice == "1" then
  189. grabAndSaveDiskID(username)
  190. elseif choice == "2" then
  191. saveDiskID(username)
  192. elseif choice == "3" then
  193. return -- Back to Security Card Manager
  194. else
  195. print("Invalid choice. Try again.")
  196. sleep(2)
  197. end
  198. end
  199. end
  200.  
  201. -- Function for Remove Cards submenu
  202. function removeCardsMenu(username)
  203. while true do
  204. term.clear()
  205. term.setCursorPos(1, 1)
  206. print("Remove Cards")
  207. print("1. Insert Security Card to delete")
  208. print("2. Enter Security Card ID to delete")
  209. print("3. Back to Security Card Manager")
  210.  
  211. local choice = read()
  212.  
  213. if choice == "1" then
  214. grabAndDeleteDiskID(username)
  215. elseif choice == "2" then
  216. deleteSpecificCard(username)
  217. elseif choice == "3" then
  218. return -- Back to Security Card Manager
  219. else
  220. print("Invalid choice. Try again.")
  221. sleep(2)
  222. end
  223. end
  224. end
  225.  
  226. -- Function to delete a specific security card by ID
  227. function deleteSpecificCard(username)
  228. term.clear()
  229. term.setCursorPos(1, 1)
  230. print("Enter Card ID to delete:")
  231. local cardID = read()
  232.  
  233. local filePath = "/disk/users/" .. username .. "/ID/" .. cardID .. ".file"
  234.  
  235. if fs.exists(filePath) then
  236. fs.delete(filePath)
  237. print("Security Card deleted successfully.")
  238. else
  239. print("Security Card not found.")
  240. end
  241.  
  242. sleep(2)
  243. end
  244.  
  245. -- Function to grab disk ID and delete it for a user
  246. function grabAndDeleteDiskID(username)
  247. term.clear()
  248. term.setCursorPos(1, 1)
  249. print("Insert Security Card to delete ID...")
  250.  
  251. while true do
  252. local peripherals = peripheral.getNames()
  253.  
  254. for _, name in ipairs(peripherals) do
  255. if peripheral.getType(name) == "drive" then
  256. local diskID = disk.getID(name)
  257. if diskID then
  258. local filePath = "/disk/users/" .. username .. "/ID/" .. diskID .. ".file"
  259.  
  260. if fs.exists(filePath) then
  261. fs.delete(filePath)
  262. print("Security Card ID deleted successfully.")
  263. else
  264. print("Security Card ID not found.")
  265. end
  266.  
  267. return -- Exit function after deleting ID
  268. end
  269. end
  270. end
  271.  
  272. sleep(1) -- Check every second
  273. end
  274. end
  275.  
  276. -- Function to save disk ID for a user
  277. function saveDiskID(username)
  278. term.clear()
  279. term.setCursorPos(1, 1)
  280. print("Enter Card ID:")
  281. local cardID = read()
  282.  
  283. -- Create directory if it doesn't exist
  284. local idDir = "/disk/users/" .. username .. "/ID"
  285. if not fs.exists(idDir) then
  286. fs.makeDir(idDir)
  287. end
  288.  
  289. -- Save disk ID as a file
  290. local filePath = idDir .. "/" .. cardID .. ".file"
  291. local file = fs.open(filePath, "w")
  292. file.writeLine("Disk ID: " .. cardID)
  293. file.close()
  294.  
  295. print("Disk ID saved successfully.")
  296. sleep(2)
  297. end
  298.  
  299. -- Function to grab disk ID and save it for a user
  300. function grabAndSaveDiskID(username)
  301. term.clear()
  302. term.setCursorPos(1, 1)
  303. print("Insert Security Card to grab ID...")
  304.  
  305. while true do
  306. local peripherals = peripheral.getNames()
  307.  
  308. for _, name in ipairs(peripherals) do
  309. if peripheral.getType(name) == "drive" then
  310. local diskID = disk.getID(name)
  311. if diskID then
  312. -- Create directory if it doesn't exist
  313. local idDir = "/disk/users/" .. username .. "/ID"
  314. if not fs.exists(idDir) then
  315. fs.makeDir(idDir)
  316. end
  317.  
  318. -- Save disk ID as a file
  319. local filePath = idDir .. "/" .. diskID .. ".file"
  320. local file = fs.open(filePath, "w")
  321. file.writeLine("Disk ID: " .. diskID)
  322. file.close()
  323.  
  324. print("Disk ID grabbed and saved successfully.")
  325. return -- Exit function after saving
  326. end
  327. end
  328. end
  329.  
  330. sleep(1) -- Check every second
  331. end
  332. end
  333.  
  334. -- Function to disable Security Card Login (delete all IDs)
  335. function disableSecurityCardLogin(username)
  336. term.clear()
  337. term.setCursorPos(1, 1)
  338. print("Are you sure you want to disable Security Card Login? (y/n)")
  339. local confirm = read()
  340. if confirm:lower() == "y" then
  341. local idDir = "/disk/users/" .. username .. "/ID"
  342. if fs.exists(idDir) then
  343. fs.delete(idDir)
  344. print("Security Card Login disabled successfully.")
  345. else
  346. print("No security cards found to delete.")
  347. end
  348. else
  349. print("Operation canceled.")
  350. end
  351.  
  352. sleep(2)
  353. end
  354.  
  355. -- Function to show all security cards for a user
  356. function showAllSecurityCards(username)
  357. term.clear()
  358. term.setCursorPos(1, 1)
  359. print("Security Cards for User: " .. username)
  360.  
  361. local idDir = "/disk/users/" .. username .. "/ID"
  362. if fs.exists(idDir) then
  363. local files = fs.list(idDir)
  364. for _, file in ipairs(files) do
  365. if fs.isDir(idDir .. "/" .. file) == false then
  366. print(file)
  367. end
  368. end
  369. else
  370. print("No security cards found.")
  371. end
  372.  
  373. print("Press any key to continue...")
  374. read()
  375. end
  376.  
  377. -- Main function to manage user selection and login
  378. function main()
  379. term.clear()
  380. term.setCursorPos(1, 1)
  381. print("Enter encrypted username:")
  382. local encryptedUsername = read()
  383.  
  384. term.clear()
  385. term.setCursorPos(1, 1)
  386. print("Enter encrypted password:")
  387. local encryptedPassword = read()
  388.  
  389. -- Assume key for decrypting username and password is stored in the user directory
  390. local keyFileName = "/disk/users/" .. decryptedUsername .. "/key.txt"
  391.  
  392. local username = decryptUsername(encryptedUsername, keyFileName)
  393. local password = decryptPassword(encryptedPassword, keyFileName)
  394.  
  395. if username and password then
  396. print("Authenticated as:", username)
  397. -- Proceed with security card manager or other functionalities
  398. securityCardLogin(username)
  399. else
  400. print("Authentication failed.")
  401. end
  402. end
  403.  
  404. -- Run the main function
  405. main()
  406.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement