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 load the encrypted text from the password file
- function loadEncryptedText(userDir)
- 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 encryptedText = file.readAll()
- file.close()
- return encryptedText
- 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
- -- Function to create a new user
- function createUser()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter new username:")
- local username = read()
- -- Check if the username already exists
- if fs.exists("/disk/users/" .. username) then
- print("Username already exists. Try again.")
- sleep(2)
- return
- end
- -- Create a directory for the new user
- fs.makeDir("/disk/users/" .. username)
- -- Prompt for password
- local password
- repeat
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter password:")
- password = read("*")
- print("Confirm password:")
- local confirmPassword = read("*")
- if password ~= confirmPassword then
- print("Passwords do not match. Try again.")
- sleep(2)
- end
- until password == confirmPassword
- -- Encrypt the password before storing
- local key = generateKey()
- local encryptedPassword = encrypt(password, key)
- -- Store the encrypted password in a text file
- local file = fs.open("/disk/users/" .. username .. "/password.txt", "w")
- file.write(encryptedPassword)
- file.close()
- -- Save the encryption key to a file
- saveKeyToFile(key, "/disk/users/" .. username)
- print("User created successfully.")
- sleep(2)
- end
- -- Function to delete a user
- function deleteUser()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter username to delete:")
- local username = read()
- -- Check if the username exists
- if not fs.exists("/disk/users/" .. username) then
- print("User not found. Try again.")
- sleep(2)
- return
- end
- -- Prompt for password to confirm deletion
- print("Enter password to confirm deletion:")
- local inputPassword = read("*")
- -- Read encryption key from file
- local keyFileName = findKeyFile("/disk/users/" .. username)
- if not keyFileName then
- print("Encryption key not found. Deletion failed.")
- sleep(2)
- return
- end
- local key = loadKeyFromFile(keyFileName)
- -- Read stored encrypted password from the file
- local file = fs.open("/disk/users/" .. username .. "/password.txt", "r")
- local encryptedPassword = file.readAll()
- file.close()
- -- Decrypt stored password and compare with input password
- local storedPassword = decrypt(encryptedPassword, key)
- if inputPassword == storedPassword then
- -- Delete the user directory
- fs.delete("/disk/users/" .. username)
- print("User deleted successfully.")
- else
- print("Incorrect password. Deletion failed.")
- end
- sleep(2)
- end
- -- Function for password recovery by admin
- function passwordRecovery()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter admin username:")
- local adminUsername = read()
- -- Check if the entered user has admin privileges
- if not fs.exists("/disk/users/" .. adminUsername .. "/admin.txt") then
- print("Permission denied. Admin access required.")
- sleep(2)
- return
- end
- -- Prompt for admin password
- print("Enter admin password:")
- local adminPassword = read("*")
- -- Read stored admin password from the file
- local adminFile = fs.open("/disk/users/" .. adminUsername .. "/password.txt", "r")
- local storedAdminPassword = adminFile.readAll()
- adminFile.close()
- -- Decrypt stored admin password and compare with input admin password
- local keyFileName = findKeyFile("/disk/users/" .. adminUsername)
- if not keyFileName then
- print("Encryption key not found. Password reset failed.")
- sleep(2)
- return
- end
- local key = loadKeyFromFile(keyFileName)
- local decryptedAdminPassword = decrypt(storedAdminPassword, key)
- if adminPassword == decryptedAdminPassword then
- print("Enter username for password reset:")
- local username = read()
- -- Check if the username exists
- if not fs.exists("/disk/users/" .. username) then
- print("User not found. Try again.")
- sleep(2)
- return
- end
- -- Confirm password reset
- print("Are you sure you want to reset the password for " .. username .. "? (y/n)")
- local confirm = read()
- if confirm:lower() ~= "y" then
- print("Password reset canceled.")
- sleep(2)
- return
- end
- -- Prompt for new password
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter new password for " .. username .. ":")
- local newPassword = read("*")
- -- Encrypt the new password before storing
- local newKey = generateKey()
- local encryptedNewPassword = encrypt(newPassword, newKey)
- -- Store the encrypted new password in the text file
- local file = fs.open("/disk/users/" .. username .. "/password.txt", "w")
- file.write(encryptedNewPassword)
- file.close()
- -- Save the encryption key for the new password
- saveKeyToFile(newKey, "/disk/users/" .. username)
- print("Password reset successfully.")
- else
- print("Incorrect admin password. Password reset failed.")
- end
- sleep(2)
- end
- -- Function to exit the program
- function exitProgram()
- term.clear()
- term.setCursorPos(1, 1)
- print("Exiting...")
- sleep(2)
- term.clear()
- term.setCursorPos(1, 1)
- os.shutdown()
- end
- -- Function to list all users (directories) in /disk/users
- function viewAllUsers()
- term.clear()
- term.setCursorPos(1, 1)
- print("List of Users:")
- local users = fs.list("/disk/users")
- for _, user in ipairs(users) do
- print("- " .. user)
- end
- print("\nPress any key to return to the main menu.")
- os.pullEvent("key")
- end
- -- Function to run Security Card Setup
- function runSecurityCardSetup()
- term.clear()
- term.setCursorPos(1, 1)
- shell.run("/disk/security/CardSetup")
- print("Security Card Setup completed. Press any key to continue.")
- os.pullEvent("key")
- end
- -- Function to display the main menu
- function mainMenu()
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- print("Main Menu:")
- print("1. Create User")
- print("2. Delete User")
- print("3. Password Recovery")
- print("4. View All Users")
- print("5. Security Card Login")
- print("6. Exit")
- local choice = read()
- if choice == "1" then
- createUser()
- elseif choice == "2" then
- deleteUser()
- elseif choice == "3" then
- passwordRecovery()
- elseif choice == "4" then
- viewAllUsers()
- elseif choice == "5" then
- runSecurityCardSetup()
- elseif choice == "6" then
- exitProgram()
- else
- print("Invalid choice. Try again.")
- sleep(2)
- end
- end
- end
- -- Main program entry point
- mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement