Advertisement
DOGGYWOOF

Encrypted security card manager

Jul 15th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.53 KB | None | 0 0
  1. -- Function for Security Card Login (insert or enter ID)
  2. function securityCardLogin(username)
  3. while true do
  4. term.clear()
  5. term.setCursorPos(1, 1)
  6. print("Security Card Manager")
  7. print("1. Add Card")
  8. print("2. Remove Cards")
  9. print("3. Show All Security Cards")
  10. print("4. Disable Security Card Login")
  11. print("5. Back to Main Menu")
  12.  
  13. local choice = read()
  14.  
  15. if choice == "1" then
  16. addCardMenu(username)
  17. elseif choice == "2" then
  18. removeCardsMenu(username)
  19. elseif choice == "3" then
  20. showAllSecurityCards(username)
  21. elseif choice == "4" then
  22. disableSecurityCardLogin(username)
  23. elseif choice == "5" then
  24. return -- Back to main menu
  25. else
  26. print("Invalid choice. Try again.")
  27. sleep(2)
  28. end
  29. end
  30. end
  31.  
  32. -- Function for Add Card submenu
  33. function addCardMenu(username)
  34. while true do
  35. term.clear()
  36. term.setCursorPos(1, 1)
  37. print("Add Card")
  38. print("1. Insert Security Card")
  39. print("2. Enter Security Card ID")
  40. print("3. Back to Security Card Manager")
  41.  
  42. local choice = read()
  43.  
  44. if choice == "1" then
  45. grabAndSaveDiskID(username)
  46. elseif choice == "2" then
  47. saveDiskID(username)
  48. elseif choice == "3" then
  49. return -- Back to Security Card Manager
  50. else
  51. print("Invalid choice. Try again.")
  52. sleep(2)
  53. end
  54. end
  55. end
  56.  
  57. -- Function for Remove Cards submenu
  58. function removeCardsMenu(username)
  59. while true do
  60. term.clear()
  61. term.setCursorPos(1, 1)
  62. print("Remove Cards")
  63. print("1. Insert Security Card to delete")
  64. print("2. Enter Security Card ID to delete")
  65. print("3. Back to Security Card Manager")
  66.  
  67. local choice = read()
  68.  
  69. if choice == "1" then
  70. grabAndDeleteDiskID(username)
  71. elseif choice == "2" then
  72. deleteSpecificCard(username)
  73. elseif choice == "3" then
  74. return -- Back to Security Card Manager
  75. else
  76. print("Invalid choice. Try again.")
  77. sleep(2)
  78. end
  79. end
  80. end
  81.  
  82. -- Function to delete a specific security card by ID
  83. function deleteSpecificCard(username)
  84. term.clear()
  85. term.setCursorPos(1, 1)
  86. print("Enter Card ID to delete:")
  87. local cardID = read()
  88.  
  89. local result = delete(username, cardID)
  90. print(result.message)
  91.  
  92. sleep(2)
  93. end
  94.  
  95. -- Function to grab disk ID and delete it for a user
  96. function grabAndDeleteDiskID(username)
  97. term.clear()
  98. term.setCursorPos(1, 1)
  99. print("Insert Security Card to delete ID...")
  100.  
  101. while true do
  102. local peripherals = peripheral.getNames()
  103.  
  104. for _, name in ipairs(peripherals) do
  105. if peripheral.getType(name) == "drive" then
  106. local diskID = disk.getID(name)
  107. if diskID then
  108. local result = delete(username, diskID)
  109. print(result.message)
  110.  
  111. return -- Exit function after deleting ID
  112. end
  113. end
  114. end
  115.  
  116. sleep(1) -- Check every second
  117. end
  118. end
  119.  
  120. -- Function to save disk ID for a user
  121. function saveDiskID(username)
  122. term.clear()
  123. term.setCursorPos(1, 1)
  124. print("Enter Card ID:")
  125. local cardID = read()
  126.  
  127. local result = create(username, cardID)
  128. print(result.status)
  129.  
  130. sleep(2)
  131. end
  132.  
  133. -- Function to grab disk ID and save it for a user
  134. function grabAndSaveDiskID(username)
  135. term.clear()
  136. term.setCursorPos(1, 1)
  137. print("Insert Security Card to grab ID...")
  138.  
  139. while true do
  140. local peripherals = peripheral.getNames()
  141.  
  142. for _, name in ipairs(peripherals) do
  143. if peripheral.getType(name) == "drive" then
  144. local diskID = disk.getID(name)
  145. if diskID then
  146. local result = create(username, diskID)
  147. print(result.status)
  148.  
  149. return -- Exit function after saving
  150. end
  151. end
  152. end
  153.  
  154. sleep(1) -- Check every second
  155. end
  156. end
  157.  
  158. -- Function to disable Security Card Login (delete all IDs)
  159. function disableSecurityCardLogin(username)
  160. term.clear()
  161. term.setCursorPos(1, 1)
  162. print("Are you sure you want to disable Security Card Login? (y/n)")
  163. local confirm = read()
  164. if confirm:lower() == "y" then
  165. local result = delete(username)
  166. print(result.message)
  167. else
  168. print("Operation canceled.")
  169. end
  170.  
  171. sleep(2)
  172. end
  173.  
  174. -- Function to show all security cards for a user
  175. function showAllSecurityCards(username)
  176. term.clear()
  177. term.setCursorPos(1, 1)
  178. print("Security Cards for User: " .. username)
  179.  
  180. local idDir = "/disk/users/" .. username .. "/ID"
  181. if fs.exists(idDir) then
  182. local files = fs.list(idDir)
  183. for _, file in ipairs(files) do
  184. if fs.isDir(idDir .. "/" .. file) == false then
  185. print(file)
  186. end
  187. end
  188. else
  189. print("No security cards found.")
  190. end
  191.  
  192. print("Press any key to continue...")
  193. read()
  194. end
  195.  
  196. -- Function for authentication
  197. function authenticate(adminUsername, adminPassword)
  198. -- Replace with actual admin credential verification
  199. if adminUsername == "admin" and adminPassword == "password" then
  200. return true
  201. else
  202. return false
  203. end
  204. end
  205.  
  206. -- Function to generate a key for encryption and decryption
  207. function generateKey()
  208. local chars = {}
  209. for i = 32, 126 do -- ASCII range for printable characters
  210. table.insert(chars, string.char(i))
  211. end
  212.  
  213. local key = {}
  214. while #chars > 0 do
  215. local index = math.random(#chars)
  216. table.insert(key, table.remove(chars, index))
  217. end
  218.  
  219. return table.concat(key)
  220. end
  221.  
  222. -- Function to create a map from a key
  223. function createMap(key)
  224. local map = {}
  225. for i = 32, 126 do
  226. map[string.char(i)] = key:sub(i - 31, i - 31)
  227. end
  228. return map
  229. end
  230.  
  231. -- Function to invert a map
  232. function invertMap(map)
  233. local invMap = {}
  234. for k, v in pairs(map) do
  235. invMap[v] = k
  236. end
  237. return invMap
  238. end
  239.  
  240. -- Function to encrypt text using a key
  241. function encrypt(text, key)
  242. local map = createMap(key)
  243. local encrypted = {}
  244. for i = 1, #text do
  245. local char = text:sub(i, i)
  246. table.insert(encrypted, map[char] or char)
  247. end
  248. return table.concat(encrypted)
  249. end
  250.  
  251. -- Function to decrypt text using a key
  252. function decrypt(text, key)
  253. local map = invertMap(createMap(key))
  254. local decrypted = {}
  255. for i = 1, #text do
  256. local char = text:sub(i, i)
  257. table.insert(decrypted, map[char] or char)
  258. end
  259. return table.concat(decrypted)
  260. end
  261.  
  262. -- Function to save the key to a file
  263. function saveKeyToFile(key, dir)
  264. local randomNumber = math.random(1, 1000)
  265. local fileName = dir .. "/key" .. randomNumber .. ".txt"
  266. local file = fs.open(fileName, "w")
  267. file.write(key)
  268. file.close()
  269. return fileName
  270. end
  271.  
  272. -- Function to load the key from a file
  273. function loadKeyFromFile(fileName)
  274. local file = fs.open(fileName, "r")
  275. local key = file.readAll()
  276. file.close()
  277. return key
  278. end
  279.  
  280. -- Function to find the key file in a directory
  281. function findKeyFile(dir)
  282. local files = fs.list(dir)
  283. for _, file in ipairs(files) do
  284. if file:match("^key%d+%.txt$") then
  285. return dir .. "/" .. file
  286. end
  287. end
  288. return nil
  289. end
  290.  
  291. -- API Functions
  292. function create(username, cardID)
  293. local userDir = "/disk/users/" .. username
  294.  
  295. if not fs.exists(userDir) then
  296. fs.makeDir(userDir)
  297. end
  298.  
  299. local key = generateKey()
  300. local encryptedCardID = encrypt(cardID, key)
  301. local keyFileName = saveKeyToFile(key, userDir)
  302.  
  303. local cardFile = fs.open(userDir .. "/card_" .. cardID .. ".txt", "w")
  304. cardFile.write(encryptedCardID)
  305. cardFile.close()
  306.  
  307. return { status = "success", encryptedCardID = encryptedCardID, keyFileName = keyFileName }
  308. end
  309.  
  310. function delete(username, cardID)
  311. local userDir = "/disk/users/" .. username
  312. local keyFileName = findKeyFile(userDir)
  313.  
  314. if keyFileName then
  315. local key = loadKeyFromFile(keyFileName)
  316. local encryptedCardID = encrypt(cardID, key)
  317. local filePath = userDir .. "/card_" .. encryptedCardID .. ".txt"
  318.  
  319. if fs.exists(filePath) then
  320. fs.delete(filePath)
  321. return { status = "success", message = "Security Card deleted successfully." }
  322. else
  323. return { status = "error", message = "Security Card not found." }
  324. end
  325. else
  326. return { status = "error", message = "Key file not found. Cannot delete card." }
  327. end
  328. end
  329.  
  330. -- Main Program Execution
  331.  
  332. local function main()
  333. term.clear()
  334. term.setCursorPos(1, 1)
  335. print("Enter admin username:")
  336. local adminUsername = read()
  337. print("Enter admin password:")
  338. local adminPassword = read("*")
  339.  
  340. local authenticated = authenticate(adminUsername, adminPassword)
  341.  
  342. if authenticated then
  343. print("Authentication successful.")
  344. print("Enter username to manage security cards:")
  345. local username = read()
  346.  
  347. securityCardLogin(username)
  348. else
  349. print("Authentication failed. Exiting...")
  350. sleep(2)
  351. return
  352. end
  353. end
  354.  
  355. main()
  356.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement