Advertisement
DOGGYWOOF

Untitled

Sep 3rd, 2024 (edited)
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. -- set_password.lua
  2.  
  3. -- Function to generate a key for encryption and decryption
  4. function generateKey()
  5. local chars = {}
  6. for i = 32, 126 do -- ASCII range for printable characters
  7. table.insert(chars, string.char(i))
  8. end
  9.  
  10. local key = {}
  11. while #chars > 0 do
  12. local index = math.random(#chars)
  13. table.insert(key, table.remove(chars, index))
  14. end
  15.  
  16. return table.concat(key)
  17. end
  18.  
  19. -- Function to encrypt text using a key
  20. function encrypt(text, key)
  21. local map = createMap(key)
  22. local encrypted = {}
  23. for i = 1, #text do
  24. local char = text:sub(i, i)
  25. table.insert(encrypted, map[char] or char)
  26. end
  27. return table.concat(encrypted)
  28. end
  29.  
  30. -- Function to create a map from a key
  31. function createMap(key)
  32. local map = {}
  33. for i = 32, 126 do
  34. map[string.char(i)] = key:sub(i - 31, i - 31)
  35. end
  36. return map
  37. end
  38.  
  39. -- Function to save the key to a file
  40. function saveKeyToFile(key)
  41. local randomNumber = math.random(1, 1000)
  42. local fileName = "key" .. randomNumber .. ".txt"
  43. local file = fs.open(fileName, "w")
  44. file.write(key)
  45. file.close()
  46. return fileName
  47. end
  48.  
  49. -- Main program
  50. print("Enter a password to set:")
  51. local password = read("*")
  52.  
  53. -- Save the password to hpass.txt
  54. local file = fs.open("hpass.txt", "w")
  55. file.write(password)
  56. file.close()
  57.  
  58. -- Generate a new key
  59. local key = generateKey()
  60.  
  61. -- Encrypt the password and save the key
  62. local encryptedPassword = encrypt(password, key)
  63. file = fs.open("password.txt", "w")
  64. file.write(encryptedPassword)
  65. file.close()
  66.  
  67. local keyFileName = saveKeyToFile(key)
  68. print("Password set and system secured.")
  69. print("Key saved to file: " .. keyFileName)
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement