Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Password Authentication System with Event API for CC:Tweaked
- -- Function to encrypt/decrypt password using a simple Caesar cipher
- local function caesarCipher(text, shift)
- local result = {}
- for i = 1, #text do
- local char = text:sub(i, i)
- local ascii = string.byte(char)
- -- Encrypt/Decrypt uppercase letters
- if ascii >= 65 and ascii <= 90 then
- char = string.char(((ascii - 65 + shift) % 26) + 65)
- -- Encrypt/Decrypt lowercase letters
- elseif ascii >= 97 and ascii <= 122 then
- char = string.char(((ascii - 97 + shift) % 26) + 97)
- end
- table.insert(result, char)
- end
- return table.concat(result)
- end
- -- Function to read the encrypted password from the file
- local function readEncryptedPassword()
- local file, err = fs.open("key.pass", "r")
- if not file then
- print("Error reading password file: " .. err)
- return nil -- Return nil if the file doesn't exist
- end
- local encryptedPassword = file.readAll()
- file.close()
- return encryptedPassword
- end
- -- Function to create a new password and save it to the file
- local function createNewPassword()
- term.clear()
- local screenWidth, screenHeight = term.getSize()
- -- Centered message for new password creation
- local noPasswordMsg = "No password found."
- local newPasswordMsg = "Create a new password."
- term.setCursorPos((screenWidth - #noPasswordMsg) / 2, screenHeight / 2 - 2)
- print(noPasswordMsg)
- term.setCursorPos((screenWidth - #newPasswordMsg) / 2, screenHeight / 2 - 1)
- print(newPasswordMsg)
- -- Prompt to enter new password
- local prompt = "Enter new password (min 6 chars): "
- term.setCursorPos((screenWidth - #prompt) / 2, screenHeight / 2)
- write(prompt)
- local newPassword = ""
- -- Use event API to gather input character by character
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.enter then
- if #newPassword >= 6 then
- break
- else
- term.setCursorPos((screenWidth - #prompt) / 2, screenHeight / 2 + 1)
- print("Password too short. Try again.")
- term.setCursorPos((screenWidth - #prompt) / 2, screenHeight / 2)
- write(prompt)
- newPassword = ""
- end
- elseif key == keys.backspace then
- newPassword = newPassword:sub(1, -2)
- term.setCursorPos((screenWidth - #prompt) / 2 + #prompt, screenHeight / 2)
- write(string.rep(" ", 20)) -- Clear previous input
- term.setCursorPos((screenWidth - #prompt) / 2 + #prompt, screenHeight / 2)
- write(newPassword) -- Display updated password
- else
- newPassword = newPassword .. string.char(key)
- term.setCursorPos((screenWidth - #prompt) / 2 + #prompt, screenHeight / 2)
- write(newPassword)
- end
- end
- local encryptedPassword = caesarCipher(newPassword, 3) -- Encrypt with a shift of 3
- -- Write the encrypted password to the file
- local file, err = fs.open("key.pass", "w")
- if not file then
- print("Error writing password file: " .. err)
- return nil
- end
- file.write(encryptedPassword)
- file.close()
- local successMsg = "Password created successfully!"
- term.setCursorPos((screenWidth - #successMsg) / 2, screenHeight / 2 + 1)
- print(successMsg)
- sleep(2) -- Pause before proceeding
- return newPassword
- end
- -- Decrypt the password read from the file
- local function getDecryptedPassword()
- local encryptedPassword = readEncryptedPassword()
- if encryptedPassword then
- return caesarCipher(encryptedPassword, -3) -- Decrypting with a shift of -3
- end
- return nil
- end
- -- ASCII Art to be displayed
- local asciiArt = {
- " |\\_/| ",
- " | @ @ ",
- " | <> _ ",
- " | _/\\------____ ((| |))",
- " | --' | ",
- " ____|_ ___| |___.' ",
- "/_/_____/____/_______| "
- }
- -- Function to display ASCII art and centered password prompt
- local function displayAsciiArt()
- term.clear()
- local screenWidth, screenHeight = term.getSize()
- local artHeight = #asciiArt
- -- Display each line of the ASCII art centered
- for i, line in ipairs(asciiArt) do
- local lineWidth = #line
- local startX = math.floor((screenWidth - lineWidth) / 2)
- local startY = math.floor((screenHeight - artHeight) / 2) + i - 1
- term.setCursorPos(startX, startY)
- term.write(line)
- end
- -- Center the "Enter Password:" prompt below the ASCII art
- local prompt = "Enter Password: "
- local promptX = math.floor((screenWidth - #prompt) / 2)
- local promptY = math.floor((screenHeight - artHeight) / 2) + artHeight + 1
- term.setCursorPos(promptX, promptY)
- write(prompt)
- end
- -- Function to prompt for password
- local function promptForPassword(correctPassword)
- displayAsciiArt()
- local input = ""
- -- Use event API to gather input character by character
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.enter then
- if input == correctPassword then
- return true
- else
- term.setTextColor(colors.red)
- local deniedMsg = "Access Denied! Incorrect Password."
- term.setCursorPos((term.getSize() - #deniedMsg) / 2, term.getSize() / 2 + #asciiArt + 2)
- print(deniedMsg)
- term.setTextColor(colors.white) -- Reset color to white after
- sleep(2) -- Pause before restarting
- return false
- end
- elseif key == keys.backspace then
- input = input:sub(1, -2)
- term.setCursorPos((term.getSize() - #prompt) / 2 + #prompt, term.getSize() / 2 + #asciiArt)
- write(string.rep(" ", 20)) -- Clear previous input
- term.setCursorPos((term.getSize() - #prompt) / 2 + #prompt, term.getSize() / 2 + #asciiArt)
- write(input) -- Display updated input
- else
- input = input .. string.char(key)
- term.setCursorPos((term.getSize() - #prompt) / 2 + #prompt, term.getSize() / 2 + #asciiArt)
- write(input)
- end
- end
- end
- -- Main loop to handle authentication
- local decryptedPassword = getDecryptedPassword()
- if not decryptedPassword then
- decryptedPassword = createNewPassword() -- Prompt to create a new password
- end
- local authenticated = false
- while not authenticated do
- authenticated = promptForPassword(decryptedPassword)
- end
- -- Code for your main boot process can go here
- term.clear()
- local welcomeMsg = "Welcome to your system!"
- term.setCursorPos((term.getSize() - #welcomeMsg) / 2, term.getSize() / 2)
- print(welcomeMsg)
- -- Additional initialization code can be added here
- shell.run("bootloader")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement