Advertisement
DOGGYWOOF

Untitled

Oct 27th, 2024 (edited)
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. -- Password Authentication System with Centered ASCII Art and Color-Coded Messages for CC:Tweaked
  2.  
  3. -- Function to encrypt/decrypt password using a simple Caesar cipher
  4. local function caesarCipher(text, shift)
  5. local result = {}
  6. for i = 1, #text do
  7. local char = text:sub(i, i)
  8. local ascii = string.byte(char)
  9. -- Encrypt/Decrypt uppercase letters
  10. if ascii >= 65 and ascii <= 90 then
  11. char = string.char(((ascii - 65 + shift) % 26) + 65)
  12. -- Encrypt/Decrypt lowercase letters
  13. elseif ascii >= 97 and ascii <= 122 then
  14. char = string.char(((ascii - 97 + shift) % 26) + 97)
  15. end
  16. table.insert(result, char)
  17. end
  18. return table.concat(result)
  19. end
  20.  
  21. -- Function to read the encrypted password from the file
  22. local function readEncryptedPassword()
  23. local file = fs.open("key.pass", "r")
  24. if not file then
  25. return nil -- Return nil if the file doesn't exist
  26. end
  27. local encryptedPassword = file.readAll()
  28. file.close()
  29. return encryptedPassword
  30. end
  31.  
  32. -- Function to create a new password and save it to the file
  33. local function createNewPassword()
  34. term.clear()
  35. local screenWidth, screenHeight = term.getSize()
  36.  
  37. -- Centered message for new password creation
  38. local noPasswordMsg = "No password found."
  39. local newPasswordMsg = "Create a new password."
  40. term.setCursorPos((screenWidth - #noPasswordMsg) / 2, screenHeight / 2 - 2)
  41. print(noPasswordMsg)
  42. term.setCursorPos((screenWidth - #newPasswordMsg) / 2, screenHeight / 2 - 1)
  43. print(newPasswordMsg)
  44.  
  45. -- Prompt to enter new password
  46. local prompt = "Enter new password: "
  47. term.setCursorPos((screenWidth - #prompt) / 2, screenHeight / 2)
  48. write(prompt)
  49.  
  50. local newPassword = read("*")
  51. local encryptedPassword = caesarCipher(newPassword, 3) -- Encrypt with a shift of 3
  52.  
  53. -- Write the encrypted password to the file
  54. local file = fs.open("key.pass", "w")
  55. file.write(encryptedPassword)
  56. file.close()
  57.  
  58. local successMsg = "Password created successfully!"
  59. term.setCursorPos((screenWidth - #successMsg) / 2, screenHeight / 2 + 1)
  60. print(successMsg)
  61. sleep(2) -- Pause before proceeding
  62. return newPassword
  63. end
  64.  
  65. -- Decrypt the password read from the file
  66. local function getDecryptedPassword()
  67. local encryptedPassword = readEncryptedPassword()
  68. if encryptedPassword then
  69. return caesarCipher(encryptedPassword, -3) -- Decrypting with a shift of -3
  70. end
  71. return nil
  72. end
  73.  
  74. -- ASCII Art to be displayed
  75. local asciiArt = {
  76. " |\\_/| ",
  77. " | @ @ Woof! ",
  78. " | <> _ ",
  79. " | _/\\------____ ((| |))",
  80. " | `--' | ",
  81. " ____|_ ___| |___.' ",
  82. "/_/_____/____/_______| "
  83. }
  84.  
  85. -- Function to display ASCII art and centered password prompt
  86. local function displayAsciiArt()
  87. term.clear()
  88. local screenWidth, screenHeight = term.getSize()
  89. local artHeight = #asciiArt
  90.  
  91. -- Display each line of the ASCII art centered
  92. for i, line in ipairs(asciiArt) do
  93. local lineWidth = #line
  94. local startX = math.floor((screenWidth - lineWidth) / 2)
  95. local startY = math.floor((screenHeight - artHeight) / 2) + i - 1
  96. term.setCursorPos(startX, startY)
  97. term.write(line)
  98. end
  99.  
  100. -- Center the "Enter Password:" prompt below the ASCII art
  101. local prompt = "Enter Password: "
  102. local promptX = math.floor((screenWidth - #prompt) / 2)
  103. local promptY = math.floor((screenHeight - artHeight) / 2) + artHeight + 1
  104. term.setCursorPos(promptX, promptY)
  105. write(prompt)
  106. end
  107.  
  108. -- Function to prompt for password
  109. local function promptForPassword(correctPassword)
  110. displayAsciiArt()
  111.  
  112. -- Get user input just below the prompt text
  113. local input = read("*")
  114.  
  115. -- Centered feedback messages for Access Granted or Denied
  116. local screenWidth, screenHeight = term.getSize()
  117. if input == correctPassword then
  118. term.setTextColor(colors.green)
  119. local grantedMsg = "Access Granted!"
  120. term.setCursorPos((screenWidth - #grantedMsg) / 2, screenHeight / 2 + #asciiArt + 2)
  121. print(grantedMsg)
  122. term.setTextColor(colors.white) -- Reset color to white after
  123. sleep(2) -- Pause for 2 seconds before continuing
  124. return true
  125. else
  126. term.setTextColor(colors.red)
  127. local deniedMsg = "Access Denied! Incorrect Password."
  128. term.setCursorPos((screenWidth - #deniedMsg) / 2, screenHeight / 2 + #asciiArt + 2)
  129. print(deniedMsg)
  130. term.setTextColor(colors.white) -- Reset color to white after
  131. sleep(2) -- Pause before restarting
  132. return false
  133. end
  134. end
  135.  
  136. -- Main loop to handle authentication
  137. local decryptedPassword = getDecryptedPassword()
  138.  
  139. if not decryptedPassword then
  140. decryptedPassword = createNewPassword() -- Prompt to create a new password
  141. end
  142.  
  143. local authenticated = false
  144.  
  145. while not authenticated do
  146. authenticated = promptForPassword(decryptedPassword)
  147. end
  148.  
  149. -- Code for your main boot process can go here
  150. term.clear()
  151. local welcomeMsg = "Welcome to your system!"
  152. term.setCursorPos((term.getSize() - #welcomeMsg) / 2, term.getSize() / 2)
  153. print(welcomeMsg)
  154. -- Additional initialization code can be added here
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement