Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Password Authentication System with Centered ASCII Art and Color-Coded Messages 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 = fs.open("key.pass", "r")
- if not file then
- 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: "
- term.setCursorPos((screenWidth - #prompt) / 2, screenHeight / 2)
- write(prompt)
- local newPassword = read("*")
- local encryptedPassword = caesarCipher(newPassword, 3) -- Encrypt with a shift of 3
- -- Write the encrypted password to the file
- local file = fs.open("key.pass", "w")
- 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 = {
- " |\\_/| ",
- " | @ @ Woof! ",
- " | <> _ ",
- " | _/\\------____ ((| |))",
- " | `--' | ",
- " ____|_ ___| |___.' ",
- "/_/_____/____/_______| "
- }
- -- 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()
- -- Get user input just below the prompt text
- local input = read("*")
- -- Centered feedback messages for Access Granted or Denied
- local screenWidth, screenHeight = term.getSize()
- if input == correctPassword then
- term.setTextColor(colors.green)
- local grantedMsg = "Access Granted!"
- term.setCursorPos((screenWidth - #grantedMsg) / 2, screenHeight / 2 + #asciiArt + 2)
- print(grantedMsg)
- term.setTextColor(colors.white) -- Reset color to white after
- sleep(2) -- Pause for 2 seconds before continuing
- return true
- else
- term.setTextColor(colors.red)
- local deniedMsg = "Access Denied! Incorrect Password."
- term.setCursorPos((screenWidth - #deniedMsg) / 2, screenHeight / 2 + #asciiArt + 2)
- print(deniedMsg)
- term.setTextColor(colors.white) -- Reset color to white after
- sleep(2) -- Pause before restarting
- return false
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement