Advertisement
DOGGYWOOF

Untitled

Oct 29th, 2024 (edited)
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | None | 0 0
  1. -- Password Authentication System with Event API 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, err = fs.open("key.pass", "r")
  24. if not file then
  25. print("Error reading password file: " .. err)
  26. return nil -- Return nil if the file doesn't exist
  27. end
  28. local encryptedPassword = file.readAll()
  29. file.close()
  30. return encryptedPassword
  31. end
  32.  
  33. -- Function to create a new password and save it to the file
  34. local function createNewPassword()
  35. term.clear()
  36. local screenWidth, screenHeight = term.getSize()
  37.  
  38. -- Centered message for new password creation
  39. local noPasswordMsg = "No password found."
  40. local newPasswordMsg = "Create a new password."
  41. term.setCursorPos((screenWidth - #noPasswordMsg) / 2, screenHeight / 2 - 2)
  42. print(noPasswordMsg)
  43. term.setCursorPos((screenWidth - #newPasswordMsg) / 2, screenHeight / 2 - 1)
  44. print(newPasswordMsg)
  45.  
  46. -- Prompt to enter new password
  47. local prompt = "Enter new password (min 6 chars): "
  48. term.setCursorPos((screenWidth - #prompt) / 2, screenHeight / 2)
  49. write(prompt)
  50.  
  51. local newPassword = ""
  52. -- Use event API to gather input character by character
  53. while true do
  54. local event, key = os.pullEvent("key")
  55. if key == keys.enter then
  56. if #newPassword >= 6 then
  57. break
  58. else
  59. term.setCursorPos((screenWidth - #prompt) / 2, screenHeight / 2 + 1)
  60. print("Password too short. Try again.")
  61. term.setCursorPos((screenWidth - #prompt) / 2, screenHeight / 2)
  62. write(prompt)
  63. newPassword = ""
  64. end
  65. elseif key == keys.backspace then
  66. newPassword = newPassword:sub(1, -2)
  67. term.setCursorPos((screenWidth - #prompt) / 2 + #prompt, screenHeight / 2)
  68. write(string.rep(" ", 20)) -- Clear previous input
  69. term.setCursorPos((screenWidth - #prompt) / 2 + #prompt, screenHeight / 2)
  70. write(newPassword) -- Display updated password
  71. else
  72. newPassword = newPassword .. string.char(key)
  73. term.setCursorPos((screenWidth - #prompt) / 2 + #prompt, screenHeight / 2)
  74. write(newPassword)
  75. end
  76. end
  77.  
  78. local encryptedPassword = caesarCipher(newPassword, 3) -- Encrypt with a shift of 3
  79.  
  80. -- Write the encrypted password to the file
  81. local file, err = fs.open("key.pass", "w")
  82. if not file then
  83. print("Error writing password file: " .. err)
  84. return nil
  85. end
  86. file.write(encryptedPassword)
  87. file.close()
  88.  
  89. local successMsg = "Password created successfully!"
  90. term.setCursorPos((screenWidth - #successMsg) / 2, screenHeight / 2 + 1)
  91. print(successMsg)
  92. sleep(2) -- Pause before proceeding
  93. return newPassword
  94. end
  95.  
  96. -- Decrypt the password read from the file
  97. local function getDecryptedPassword()
  98. local encryptedPassword = readEncryptedPassword()
  99. if encryptedPassword then
  100. return caesarCipher(encryptedPassword, -3) -- Decrypting with a shift of -3
  101. end
  102. return nil
  103. end
  104.  
  105. -- ASCII Art to be displayed
  106. local asciiArt = {
  107. " |\\_/| ",
  108. " | @ @ ",
  109. " | <> _ ",
  110. " | _/\\------____ ((| |))",
  111. " | --' | ",
  112. " ____|_ ___| |___.' ",
  113. "/_/_____/____/_______| "
  114. }
  115.  
  116. -- Function to display ASCII art and centered password prompt
  117. local function displayAsciiArt()
  118. term.clear()
  119. local screenWidth, screenHeight = term.getSize()
  120. local artHeight = #asciiArt
  121.  
  122. -- Display each line of the ASCII art centered
  123. for i, line in ipairs(asciiArt) do
  124. local lineWidth = #line
  125. local startX = math.floor((screenWidth - lineWidth) / 2)
  126. local startY = math.floor((screenHeight - artHeight) / 2) + i - 1
  127. term.setCursorPos(startX, startY)
  128. term.write(line)
  129. end
  130.  
  131. -- Center the "Enter Password:" prompt below the ASCII art
  132. local prompt = "Enter Password: "
  133. local promptX = math.floor((screenWidth - #prompt) / 2)
  134. local promptY = math.floor((screenHeight - artHeight) / 2) + artHeight + 1
  135. term.setCursorPos(promptX, promptY)
  136. write(prompt)
  137. end
  138.  
  139. -- Function to prompt for password
  140. local function promptForPassword(correctPassword)
  141. displayAsciiArt()
  142.  
  143. local input = ""
  144. -- Use event API to gather input character by character
  145. while true do
  146. local event, key = os.pullEvent("key")
  147. if key == keys.enter then
  148. if input == correctPassword then
  149. return true
  150. else
  151. term.setTextColor(colors.red)
  152. local deniedMsg = "Access Denied! Incorrect Password."
  153. term.setCursorPos((term.getSize() - #deniedMsg) / 2, term.getSize() / 2 + #asciiArt + 2)
  154. print(deniedMsg)
  155. term.setTextColor(colors.white) -- Reset color to white after
  156. sleep(2) -- Pause before restarting
  157. return false
  158. end
  159. elseif key == keys.backspace then
  160. input = input:sub(1, -2)
  161. term.setCursorPos((term.getSize() - #prompt) / 2 + #prompt, term.getSize() / 2 + #asciiArt)
  162. write(string.rep(" ", 20)) -- Clear previous input
  163. term.setCursorPos((term.getSize() - #prompt) / 2 + #prompt, term.getSize() / 2 + #asciiArt)
  164. write(input) -- Display updated input
  165. else
  166. input = input .. string.char(key)
  167. term.setCursorPos((term.getSize() - #prompt) / 2 + #prompt, term.getSize() / 2 + #asciiArt)
  168. write(input)
  169. end
  170. end
  171. end
  172.  
  173. -- Main loop to handle authentication
  174. local decryptedPassword = getDecryptedPassword()
  175.  
  176. if not decryptedPassword then
  177. decryptedPassword = createNewPassword() -- Prompt to create a new password
  178. end
  179.  
  180. local authenticated = false
  181.  
  182. while not authenticated do
  183. authenticated = promptForPassword(decryptedPassword)
  184. end
  185.  
  186. -- Code for your main boot process can go here
  187. term.clear()
  188. local welcomeMsg = "Welcome to your system!"
  189. term.setCursorPos((term.getSize() - #welcomeMsg) / 2, term.getSize() / 2)
  190. print(welcomeMsg)
  191. -- Additional initialization code can be added here
  192.  
  193. shell.run("bootloader")
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement