Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 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
- -- Function to save encrypted password to a file
- function saveEncryptedPassword(username, encryptedPassword)
- local userDir = "/disk/users/" .. username
- local passwordFile = fs.open(userDir .. "/password.txt", "w")
- passwordFile.write(encryptedPassword)
- passwordFile.close()
- end
- -- Function to load encrypted password from a file
- 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.")
- return nil
- end
- local file = fs.open(fileName, "r")
- local encryptedPassword = file.readAll()
- file.close()
- return encryptedPassword
- end
- -- Function to encrypt a password using a key
- function encryptPassword(password, key)
- return encrypt(password, key)
- end
- -- Function to decrypt a password using a key
- function decryptPassword(encryptedPassword, key)
- return decrypt(encryptedPassword, key)
- end
- -- Function to select and encrypt username
- function selectAndEncryptUsername()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter username:")
- local username = read()
- local key = generateKey()
- local encryptedUsername = encrypt(username, key)
- local keyFileName = saveKeyToFile(key, "/disk/users/" .. username)
- return {
- encryptedUsername = encryptedUsername,
- keyFileName = keyFileName
- }
- end
- -- Function to decrypt username
- function decryptUsername(encryptedUsername, keyFileName)
- local key = loadKeyFromFile(keyFileName)
- if not key then
- print("Encryption key not found.")
- return nil
- end
- return decrypt(encryptedUsername, key)
- 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 grab disk ID and save it for a user
- function grabAndSaveDiskID(username)
- term.clear()
- term.setCursorPos(1, 1)
- print("Insert Security Card to grab 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
- -- 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 .. "/" .. diskID .. ".file"
- local file = fs.open(filePath, "w")
- file.writeLine("Disk ID: " .. diskID)
- file.close()
- print("Disk ID grabbed and saved successfully.")
- return -- Exit function after saving
- end
- end
- end
- sleep(1) -- Check every second
- end
- 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? (y/n)")
- local confirm = read()
- if confirm: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 delete.")
- end
- else
- print("Operation canceled.")
- 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
- -- Main function to manage user selection and login
- function main()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter encrypted username:")
- local encryptedUsername = read()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter encrypted password:")
- local encryptedPassword = read()
- -- Assume key for decrypting username and password is stored in the user directory
- local keyFileName = "/disk/users/" .. decryptedUsername .. "/key.txt"
- local username = decryptUsername(encryptedUsername, keyFileName)
- local password = decryptPassword(encryptedPassword, keyFileName)
- if username and password then
- print("Authenticated as:", username)
- -- Proceed with security card manager or other functionalities
- securityCardLogin(username)
- else
- print("Authentication failed.")
- end
- end
- -- Run the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement