Advertisement
DOGGYWOOF

Untitled

Jul 17th, 2024 (edited)
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. -- Doggy OS Encryption Password Setup
  2.  
  3. -- Function to generate a key for encryption and decryption
  4. local 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 create a map from a key
  20. local function createMap(key)
  21. local map = {}
  22. for i = 32, 126 do
  23. map[string.char(i)] = key:sub(i - 31, i - 31)
  24. end
  25. return map
  26. end
  27.  
  28. -- Function to encrypt text using a key
  29. local function encrypt(text, key)
  30. local map = createMap(key)
  31. local encrypted = {}
  32. for i = 1, #text do
  33. local char = text:sub(i, i)
  34. table.insert(encrypted, map[char] or char)
  35. end
  36. return table.concat(encrypted)
  37. end
  38.  
  39. -- Function to save the key to a file
  40. local function saveKeyToFile(key)
  41. local randomNumber = math.random(1, 1000)
  42. local fileName = "key" .. randomNumber .. ".txt"
  43. local file = io.open(fileName, "w")
  44. file:write(key)
  45. file:close()
  46. return fileName
  47. end
  48.  
  49. -- Function to set or reset the encryption password
  50. local function setPassword()
  51. term.clear()
  52. term.setCursorPos(1, 1)
  53. print("Doggy OS Encryption Password Setup")
  54. print()
  55. print("Enter new password:")
  56. local newPassword = read("*")
  57.  
  58. -- Generate a new encryption key
  59. local key = generateKey()
  60. local encryptedPassword = encrypt(newPassword, key)
  61.  
  62. -- Save the encrypted password to a file
  63. local file = io.open("password.txt", "w")
  64. file:write(encryptedPassword)
  65. file:close()
  66.  
  67. -- Save the encryption key to a file
  68. local keyFileName = saveKeyToFile(key)
  69. print("Password set successfully.")
  70. print("Encryption key saved to file: " .. keyFileName)
  71.  
  72. print("Press ENTER to continue.")
  73. io.read() -- Wait for user to press enter
  74. end
  75.  
  76. -- Main program
  77. term.clear()
  78. term.setCursorPos(1, 1)
  79. print("Doggy OS Encryption Password Setup")
  80. print()
  81.  
  82. -- Check if password file already exists
  83. if fs.exists("password.txt") then
  84. print("Password is already set.")
  85. print("Press ENTER to exit.")
  86. io.read() -- Wait for user to press enter
  87. else
  88. setPassword()
  89. end
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement