Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Encryption Functions
- -- Function to generate a key for encryption and decryption
- function generateKey()
- local chars = {}
- for i = 32, 126 do -- ASCII range for printable characters
- table.insert(chars, string.char(i))
- end
- local key = {}
- while #chars > 0 do
- local index = math.random(#chars)
- table.insert(key, table.remove(chars, index))
- end
- return table.concat(key)
- end
- -- Function to create a map from a key
- function createMap(key)
- local map = {}
- for i = 32, 126 do
- map[string.char(i)] = key:sub(i - 31, i - 31)
- end
- return map
- end
- -- Function to invert a map
- function invertMap(map)
- local invMap = {}
- for k, v in pairs(map) do
- invMap[v] = k
- end
- return invMap
- end
- -- Function to encrypt text using a key
- function encrypt(text, key)
- local map = createMap(key)
- local encrypted = {}
- for i = 1, #text do
- local char = text:sub(i, i)
- table.insert(encrypted, map[char] or char)
- end
- return table.concat(encrypted)
- end
- -- Function to decrypt text using a key
- function decrypt(text, key)
- local map = invertMap(createMap(key))
- local decrypted = {}
- for i = 1, #text do
- local char = text:sub(i, i)
- table.insert(decrypted, map[char] or char)
- end
- return table.concat(decrypted)
- end
- -- Function to save the key to a file
- function saveKeyToFile(key, dir)
- local randomNumber = math.random(1, 1000)
- local fileName = dir .. "/key" .. randomNumber .. ".txt"
- local file = fs.open(fileName, "w")
- file.write(key)
- file.close()
- return fileName
- end
- -- Function to load the key from a file
- function loadKeyFromFile(fileName)
- local file = fs.open(fileName, "r")
- local key = file.readAll()
- file.close()
- return key
- end
- -- Function to find the key file in a directory
- function findKeyFile(dir)
- local files = fs.list(dir)
- for _, file in ipairs(files) do
- if file:match("^key%d+%.txt$") then
- return dir .. "/" .. file
- end
- end
- return nil
- end
- -- Main Program Execution
- -- Function to select and decrypt username
- function selectAndDecryptUsername()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter encrypted username:")
- local encryptedUsername = read("*")
- -- Load key from file in user's directory
- local keyFile = findKeyFile("/disk/users/<username>") -- Replace <username> with actual username
- if not keyFile then
- print("Encryption key not found.")
- return nil
- end
- local key = loadKeyFromFile(keyFile)
- local decryptedUsername = decrypt(encryptedUsername, key)
- return decryptedUsername
- end
- -- Function to encrypt username and save key
- function encryptAndSaveUsername(username)
- local key = generateKey()
- local encryptedUsername = encrypt(username, key)
- -- Save key to file in user's directory
- local keyFileName = saveKeyToFile(key, "/disk/users/" .. username)
- return { encryptedUsername = encryptedUsername, keyFileName = keyFileName }
- end
- -- Function to encrypt password
- function encryptPassword(password, key)
- return encrypt(password, key)
- end
- -- Function to decrypt password
- function decryptPassword(encryptedPassword, key)
- return decrypt(encryptedPassword, key)
- end
- -- Function to save encrypted password to file in user's directory
- function saveEncryptedPassword(username, encryptedPassword)
- local userDir = "/disk/users/" .. username
- -- Create user directory if it doesn't exist
- if not fs.exists(userDir) then
- fs.makeDir(userDir)
- end
- local passwordFile = fs.open(userDir .. "/password.txt", "w")
- passwordFile.write(encryptedPassword)
- passwordFile.close()
- end
- -- Function to load encrypted password from file in user's directory
- function loadEncryptedPassword(username)
- local userDir = "/disk/users/" .. username
- local fileName = userDir .. "/password.txt"
- if not fs.exists(fileName) then
- print("Password file does not exist for user: " .. username)
- return nil
- end
- local file = fs.open(fileName, "r")
- local encryptedPassword = file.readAll()
- file.close()
- return encryptedPassword
- end
- -- Function for Security Card Login (insert or enter ID)
- function securityCardLogin(username)
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- print("Security Card Manager for User:", username)
- print("1. Add Card")
- print("2. Remove Cards")
- print("3. Show All Security Cards")
- print("4. Disable Security Card Login")
- print("5. Back to Main Menu")
- local choice = read()
- if choice == "1" then
- addCardMenu(username)
- elseif choice == "2" then
- removeCardsMenu(username)
- elseif choice == "3" then
- showAllSecurityCards(username)
- elseif choice == "4" then
- disableSecurityCardLogin(username)
- elseif choice == "5" then
- return -- Back to main menu
- else
- print("Invalid choice. Try again.")
- sleep(2)
- end
- end
- end
- -- Function for Add Card submenu
- function addCardMenu(username)
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- print("Add Card")
- print("1. Insert Security Card")
- print("2. Enter Security Card ID")
- print("3. Back to Security Card Manager")
- local choice = read()
- if choice == "1" then
- grabAndSaveDiskID(username)
- elseif choice == "2" then
- saveDiskID(username)
- elseif choice == "3" then
- return -- Back to Security Card Manager
- else
- print("Invalid choice. Try again.")
- sleep(2)
- end
- end
- end
- -- Function for Remove Cards submenu
- function removeCardsMenu(username)
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- print("Remove Cards")
- print("1. Insert Security Card to delete")
- print("2. Enter Security Card ID to delete")
- print("3. Back to Security Card Manager")
- local choice = read()
- if choice == "1" then
- grabAndDeleteDiskID(username)
- elseif choice == "2" then
- deleteSpecificCard(username)
- elseif choice == "3" then
- return -- Back to Security Card Manager
- else
- print("Invalid choice. Try again.")
- sleep(2)
- end
- end
- end
- -- Function to delete a specific security card by ID
- function deleteSpecificCard(username)
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter Card ID to delete:")
- local cardID = read()
- local filePath = "/disk/users/" .. username .. "/ID/" .. cardID .. ".file"
- if fs.exists(filePath) then
- fs.delete(filePath)
- print("Security Card deleted successfully.")
- else
- print("Security Card not found.")
- end
- sleep(2)
- end
- -- Function to grab disk ID and delete it for a user
- function grabAndDeleteDiskID(username)
- term.clear()
- term.setCursorPos(1, 1)
- print("Insert Security Card to delete ID...")
- while true do
- local peripherals = peripheral.getNames()
- for _, name in ipairs(peripherals) do
- if peripheral.getType(name) == "drive" then
- local diskID = disk.getID(name)
- if diskID then
- local filePath = "/disk/users/" .. username .. "/ID/" .. diskID .. ".file"
- if fs.exists(filePath) then
- fs.delete(filePath)
- print("Security Card ID deleted successfully.")
- else
- print("Security Card ID not found.")
- end
- return -- Exit function after deleting ID
- end
- end
- end
- sleep(1) -- Check every second
- end
- end
- -- Function to save disk ID for a user
- function saveDiskID(username)
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter Card ID:")
- local cardID = read()
- -- Create directory if it doesn't exist
- local idDir = "/disk/users/" .. username .. "/ID"
- if not fs.exists(idDir) then
- fs.makeDir(idDir)
- end
- -- Save disk ID as a file
- local filePath = idDir .. "/" .. cardID .. ".file"
- local file = fs.open(filePath, "w")
- file.writeLine("Disk ID: " .. cardID)
- file.close()
- print("Disk ID saved successfully.")
- sleep(2)
- end
- -- Function to disable Security Card Login (delete all IDs)
- function disableSecurityCardLogin(username)
- term.clear()
- term.setCursorPos(1, 1)
- print("Are you sure you want to disable Security Card Login for user: " .. username .. "? (y/n)")
- local choice = read()
- if choice:lower() == "y" then
- local idDir = "/disk/users/" .. username .. "/ID"
- if fs.exists(idDir) then
- fs.delete(idDir)
- print("Security Card Login disabled successfully.")
- else
- print("No Security Cards found to disable.")
- end
- else
- print("Security Card Login disable operation cancelled.")
- end
- sleep(2)
- end
- -- Function to show all security cards for a user
- function showAllSecurityCards(username)
- term.clear()
- term.setCursorPos(1, 1)
- print("Security Cards for User: " .. username)
- local idDir = "/disk/users/" .. username .. "/ID"
- if fs.exists(idDir) then
- local files = fs.list(idDir)
- for _, file in ipairs(files) do
- if fs.isDir(idDir .. "/" .. file) == false then
- print(file)
- end
- end
- else
- print("No security cards found.")
- end
- print("Press any key to continue...")
- read()
- end
- -- API Functions
- function create(username, text)
- local userDir = "/disk/users/" .. username
- if not fs.exists(userDir) then
- fs.makeDir(userDir)
- end
- local key = generateKey()
- local encryptedText = encrypt(text, key)
- local keyFileName = saveKeyToFile(key, userDir)
- local passwordFile = fs.open(userDir .. "/password.txt", "w")
- passwordFile.write(encryptedText)
- passwordFile.close()
- return { status = "success", encryptedText = encryptedText, keyFileName = keyFileName }
- end
- function delete(username)
- local userDir = "/disk/users/" .. username
- if fs.exists(userDir) then
- fs.delete(userDir)
- return { status = "success", message = "User directory deleted." }
- else
- return { status = "error", message = "User directory not found." }
- end
- end
- function authenticate(adminUsername, adminPassword)
- -- Replace with actual admin credential verification
- if adminUsername == "admin" and adminPassword == "password" then
- return { status = "success", message = "Authenticated successfully." }
- else
- return { status = "error", message = "Authentication failed." }
- end
- end
- -- Example Usage
- local tArgs = {...}
- if #tArgs < 1 then
- print("Usage: api <command> <args>")
- return
- end
- local command = tArgs[1]
- if command == "create" then
- if #tArgs < 3 then
- print("Usage: api create <username> <text>")
- return
- end
- local username = tArgs[2]
- local text = table.concat(tArgs, " ", 3)
- local result = create(username, text)
- print(textutils.serialize(result))
- elseif command == "delete" then
- if #tArgs < 2 then
- print("Usage: api delete <username>")
- return
- end
- local username = tArgs[2]
- local result = delete(username)
- print(textutils.serialize(result))
- elseif command == "authenticate" then
- if #tArgs < 3 then
- print("Usage: api authenticate <adminUsername> <adminPassword>")
- return
- end
- local adminUsername = tArgs[2]
- local adminPassword = tArgs[3]
- local result = authenticate(adminUsername, adminPassword)
- print(textutils.serialize(result))
- else
- print("Invalid command. Use 'create', 'delete', or 'authenticate'.")
- end
- -- Main Menu
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- print("Welcome to Security Card System")
- print("1. Login")
- print("2. Exit")
- local choice = read()
- if choice == "1" then
- local username = selectAndDecryptUsername()
- if username then
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter password for user: " .. username)
- local password = read("*")
- if validateUserCredentials(username, password) then
- securityCardLogin(username)
- else
- print("Invalid password. Access denied.")
- sleep(2)
- end
- else
- print("Invalid username.")
- sleep(2)
- end
- elseif choice == "2" then
- print("Exiting...")
- break
- else
- print("Invalid choice. Try again.")
- sleep(2)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement