Advertisement
DOGGYWOOF

Encrypted security card manager

Jul 15th, 2024 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.14 KB | None | 0 0
  1. -- Encryption Functions
  2.  
  3. -- Function to generate a key for encryption and decryption
  4. function generateKey()
  5. local chars = {}
  6. for i = 32, 126 do -- ASCII range for printable characters
  7. table.insert(chars, string.char(i))
  8. end
  9.  
  10. local key = {}
  11. while #chars > 0 do
  12. local index = math.random(#chars)
  13. table.insert(key, table.remove(chars, index))
  14. end
  15.  
  16. return table.concat(key)
  17. end
  18.  
  19. -- Function to create a map from a key
  20. function createMap(key)
  21. local map = {}
  22. for i = 32, 126 do
  23. map[string.char(i)] = key:sub(i - 31, i - 31)
  24. end
  25. return map
  26. end
  27.  
  28. -- Function to invert a map
  29. function invertMap(map)
  30. local invMap = {}
  31. for k, v in pairs(map) do
  32. invMap[v] = k
  33. end
  34. return invMap
  35. end
  36.  
  37. -- Function to encrypt text using a key
  38. function encrypt(text, key)
  39. local map = createMap(key)
  40. local encrypted = {}
  41. for i = 1, #text do
  42. local char = text:sub(i, i)
  43. table.insert(encrypted, map[char] or char)
  44. end
  45. return table.concat(encrypted)
  46. end
  47.  
  48. -- Function to decrypt text using a key
  49. function decrypt(text, key)
  50. local map = invertMap(createMap(key))
  51. local decrypted = {}
  52. for i = 1, #text do
  53. local char = text:sub(i, i)
  54. table.insert(decrypted, map[char] or char)
  55. end
  56. return table.concat(decrypted)
  57. end
  58.  
  59. -- Function to save the key to a file
  60. function saveKeyToFile(key, dir)
  61. local randomNumber = math.random(1, 1000)
  62. local fileName = dir .. "/key" .. randomNumber .. ".txt"
  63. local file = fs.open(fileName, "w")
  64. file.write(key)
  65. file.close()
  66. return fileName
  67. end
  68.  
  69. -- Function to load the key from a file
  70. function loadKeyFromFile(fileName)
  71. local file = fs.open(fileName, "r")
  72. local key = file.readAll()
  73. file.close()
  74. return key
  75. end
  76.  
  77. -- Function to find the key file in a directory
  78. function findKeyFile(dir)
  79. local files = fs.list(dir)
  80. for _, file in ipairs(files) do
  81. if file:match("^key%d+%.txt$") then
  82. return dir .. "/" .. file
  83. end
  84. end
  85. return nil
  86. end
  87.  
  88. -- Main Program Execution
  89.  
  90. -- Function to select and decrypt username
  91. function selectAndDecryptUsername()
  92. term.clear()
  93. term.setCursorPos(1, 1)
  94. print("Enter encrypted username:")
  95. local encryptedUsername = read("*")
  96.  
  97. -- Load key from file in user's directory
  98. local keyFile = findKeyFile("/disk/users/<username>") -- Replace <username> with actual username
  99. if not keyFile then
  100. print("Encryption key not found.")
  101. return nil
  102. end
  103.  
  104. local key = loadKeyFromFile(keyFile)
  105. local decryptedUsername = decrypt(encryptedUsername, key)
  106. return decryptedUsername
  107. end
  108.  
  109. -- Function to encrypt username and save key
  110. function encryptAndSaveUsername(username)
  111. local key = generateKey()
  112. local encryptedUsername = encrypt(username, key)
  113.  
  114. -- Save key to file in user's directory
  115. local keyFileName = saveKeyToFile(key, "/disk/users/" .. username)
  116.  
  117. return { encryptedUsername = encryptedUsername, keyFileName = keyFileName }
  118. end
  119.  
  120. -- Function to encrypt password
  121. function encryptPassword(password, key)
  122. return encrypt(password, key)
  123. end
  124.  
  125. -- Function to decrypt password
  126. function decryptPassword(encryptedPassword, key)
  127. return decrypt(encryptedPassword, key)
  128. end
  129.  
  130. -- Function to save encrypted password to file in user's directory
  131. function saveEncryptedPassword(username, encryptedPassword)
  132. local userDir = "/disk/users/" .. username
  133.  
  134. -- Create user directory if it doesn't exist
  135. if not fs.exists(userDir) then
  136. fs.makeDir(userDir)
  137. end
  138.  
  139. local passwordFile = fs.open(userDir .. "/password.txt", "w")
  140. passwordFile.write(encryptedPassword)
  141. passwordFile.close()
  142. end
  143.  
  144. -- Function to load encrypted password from file in user's directory
  145. function loadEncryptedPassword(username)
  146. local userDir = "/disk/users/" .. username
  147. local fileName = userDir .. "/password.txt"
  148.  
  149. if not fs.exists(fileName) then
  150. print("Password file does not exist for user: " .. username)
  151. return nil
  152. end
  153.  
  154. local file = fs.open(fileName, "r")
  155. local encryptedPassword = file.readAll()
  156. file.close()
  157.  
  158. return encryptedPassword
  159. end
  160.  
  161. -- Function for Security Card Login (insert or enter ID)
  162. function securityCardLogin(username)
  163. while true do
  164. term.clear()
  165. term.setCursorPos(1, 1)
  166. print("Security Card Manager for User:", username)
  167. print("1. Add Card")
  168. print("2. Remove Cards")
  169. print("3. Show All Security Cards")
  170. print("4. Disable Security Card Login")
  171. print("5. Back to Main Menu")
  172.  
  173. local choice = read()
  174.  
  175. if choice == "1" then
  176. addCardMenu(username)
  177. elseif choice == "2" then
  178. removeCardsMenu(username)
  179. elseif choice == "3" then
  180. showAllSecurityCards(username)
  181. elseif choice == "4" then
  182. disableSecurityCardLogin(username)
  183. elseif choice == "5" then
  184. return -- Back to main menu
  185. else
  186. print("Invalid choice. Try again.")
  187. sleep(2)
  188. end
  189. end
  190. end
  191.  
  192. -- Function for Add Card submenu
  193. function addCardMenu(username)
  194. while true do
  195. term.clear()
  196. term.setCursorPos(1, 1)
  197. print("Add Card")
  198. print("1. Insert Security Card")
  199. print("2. Enter Security Card ID")
  200. print("3. Back to Security Card Manager")
  201.  
  202. local choice = read()
  203.  
  204. if choice == "1" then
  205. grabAndSaveDiskID(username)
  206. elseif choice == "2" then
  207. saveDiskID(username)
  208. elseif choice == "3" then
  209. return -- Back to Security Card Manager
  210. else
  211. print("Invalid choice. Try again.")
  212. sleep(2)
  213. end
  214. end
  215. end
  216.  
  217. -- Function for Remove Cards submenu
  218. function removeCardsMenu(username)
  219. while true do
  220. term.clear()
  221. term.setCursorPos(1, 1)
  222. print("Remove Cards")
  223. print("1. Insert Security Card to delete")
  224. print("2. Enter Security Card ID to delete")
  225. print("3. Back to Security Card Manager")
  226.  
  227. local choice = read()
  228.  
  229. if choice == "1" then
  230. grabAndDeleteDiskID(username)
  231. elseif choice == "2" then
  232. deleteSpecificCard(username)
  233. elseif choice == "3" then
  234. return -- Back to Security Card Manager
  235. else
  236. print("Invalid choice. Try again.")
  237. sleep(2)
  238. end
  239. end
  240. end
  241.  
  242. -- Function to delete a specific security card by ID
  243. function deleteSpecificCard(username)
  244. term.clear()
  245. term.setCursorPos(1, 1)
  246. print("Enter Card ID to delete:")
  247. local cardID = read()
  248.  
  249. local filePath = "/disk/users/" .. username .. "/ID/" .. cardID .. ".file"
  250.  
  251. if fs.exists(filePath) then
  252. fs.delete(filePath)
  253. print("Security Card deleted successfully.")
  254. else
  255. print("Security Card not found.")
  256. end
  257.  
  258. sleep(2)
  259. end
  260.  
  261. -- Function to grab disk ID and delete it for a user
  262. function grabAndDeleteDiskID(username)
  263. term.clear()
  264. term.setCursorPos(1, 1)
  265. print("Insert Security Card to delete ID...")
  266.  
  267. while true do
  268. local peripherals = peripheral.getNames()
  269.  
  270. for _, name in ipairs(peripherals) do
  271. if peripheral.getType(name) == "drive" then
  272. local diskID = disk.getID(name)
  273. if diskID then
  274. local filePath = "/disk/users/" .. username .. "/ID/" .. diskID .. ".file"
  275.  
  276. if fs.exists(filePath) then
  277. fs.delete(filePath)
  278. print("Security Card ID deleted successfully.")
  279. else
  280. print("Security Card ID not found.")
  281. end
  282.  
  283. return -- Exit function after deleting ID
  284. end
  285. end
  286. end
  287.  
  288. sleep(1) -- Check every second
  289. end
  290. end
  291.  
  292. -- Function to save disk ID for a user
  293. function saveDiskID(username)
  294. term.clear()
  295. term.setCursorPos(1, 1)
  296. print("Enter Card ID:")
  297. local cardID = read()
  298.  
  299. -- Create directory if it doesn't exist
  300. local idDir = "/disk/users/" .. username .. "/ID"
  301. if not fs.exists(idDir) then
  302. fs.makeDir(idDir)
  303. end
  304.  
  305. -- Save disk ID as a file
  306. local filePath = idDir .. "/" .. cardID .. ".file"
  307. local file = fs.open(filePath, "w")
  308. file.writeLine("Disk ID: " .. cardID)
  309. file.close()
  310.  
  311. print("Disk ID saved successfully.")
  312. sleep(2)
  313. end
  314.  
  315. -- Function to disable Security Card Login (delete all IDs)
  316. function disableSecurityCardLogin(username)
  317. term.clear()
  318. term.setCursorPos(1, 1)
  319. print("Are you sure you want to disable Security Card Login for user: " .. username .. "? (y/n)")
  320. local choice = read()
  321.  
  322. if choice:lower() == "y" then
  323. local idDir = "/disk/users/" .. username .. "/ID"
  324. if fs.exists(idDir) then
  325. fs.delete(idDir)
  326. print("Security Card Login disabled successfully.")
  327. else
  328. print("No Security Cards found to disable.")
  329. end
  330. else
  331. print("Security Card Login disable operation cancelled.")
  332. end
  333.  
  334. sleep(2)
  335. end
  336.  
  337. -- Function to show all security cards for a user
  338. function showAllSecurityCards(username)
  339. term.clear()
  340. term.setCursorPos(1, 1)
  341. print("Security Cards for User: " .. username)
  342.  
  343. local idDir = "/disk/users/" .. username .. "/ID"
  344. if fs.exists(idDir) then
  345. local files = fs.list(idDir)
  346. for _, file in ipairs(files) do
  347. if fs.isDir(idDir .. "/" .. file) == false then
  348. print(file)
  349. end
  350. end
  351. else
  352. print("No security cards found.")
  353. end
  354.  
  355. print("Press any key to continue...")
  356. read()
  357. end
  358.  
  359. -- API Functions
  360. function create(username, text)
  361. local userDir = "/disk/users/" .. username
  362.  
  363. if not fs.exists(userDir) then
  364. fs.makeDir(userDir)
  365. end
  366.  
  367. local key = generateKey()
  368. local encryptedText = encrypt(text, key)
  369. local keyFileName = saveKeyToFile(key, userDir)
  370.  
  371. local passwordFile = fs.open(userDir .. "/password.txt", "w")
  372. passwordFile.write(encryptedText)
  373. passwordFile.close()
  374.  
  375. return { status = "success", encryptedText = encryptedText, keyFileName = keyFileName }
  376. end
  377.  
  378. function delete(username)
  379. local userDir = "/disk/users/" .. username
  380.  
  381. if fs.exists(userDir) then
  382. fs.delete(userDir)
  383. return { status = "success", message = "User directory deleted." }
  384. else
  385. return { status = "error", message = "User directory not found." }
  386. end
  387. end
  388.  
  389. function authenticate(adminUsername, adminPassword)
  390. -- Replace with actual admin credential verification
  391. if adminUsername == "admin" and adminPassword == "password" then
  392. return { status = "success", message = "Authenticated successfully." }
  393. else
  394. return { status = "error", message = "Authentication failed." }
  395. end
  396. end
  397.  
  398. -- Example Usage
  399. local tArgs = {...}
  400. if #tArgs < 1 then
  401. print("Usage: api <command> <args>")
  402. return
  403. end
  404.  
  405. local command = tArgs[1]
  406. if command == "create" then
  407. if #tArgs < 3 then
  408. print("Usage: api create <username> <text>")
  409. return
  410. end
  411. local username = tArgs[2]
  412. local text = table.concat(tArgs, " ", 3)
  413. local result = create(username, text)
  414. print(textutils.serialize(result))
  415. elseif command == "delete" then
  416. if #tArgs < 2 then
  417. print("Usage: api delete <username>")
  418. return
  419. end
  420. local username = tArgs[2]
  421. local result = delete(username)
  422. print(textutils.serialize(result))
  423. elseif command == "authenticate" then
  424. if #tArgs < 3 then
  425. print("Usage: api authenticate <adminUsername> <adminPassword>")
  426. return
  427. end
  428. local adminUsername = tArgs[2]
  429. local adminPassword = tArgs[3]
  430. local result = authenticate(adminUsername, adminPassword)
  431. print(textutils.serialize(result))
  432. else
  433. print("Invalid command. Use 'create', 'delete', or 'authenticate'.")
  434. end
  435.  
  436. -- Main Menu
  437. while true do
  438. term.clear()
  439. term.setCursorPos(1, 1)
  440. print("Welcome to Security Card System")
  441. print("1. Login")
  442. print("2. Exit")
  443.  
  444. local choice = read()
  445.  
  446. if choice == "1" then
  447. local username = selectAndDecryptUsername()
  448. if username then
  449. term.clear()
  450. term.setCursorPos(1, 1)
  451. print("Enter password for user: " .. username)
  452. local password = read("*")
  453.  
  454. if validateUserCredentials(username, password) then
  455. securityCardLogin(username)
  456. else
  457. print("Invalid password. Access denied.")
  458. sleep(2)
  459. end
  460. else
  461. print("Invalid username.")
  462. sleep(2)
  463. end
  464. elseif choice == "2" then
  465. print("Exiting...")
  466. break
  467. else
  468. print("Invalid choice. Try again.")
  469. sleep(2)
  470. end
  471. end
  472.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement