Advertisement
DOGGYWOOF

Create Doggy OS encryped user accounts

Jul 13th, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. -- Function to generate a key for encryption and decryption
  2. function generateKey()
  3. local chars = {}
  4. for i = 32, 126 do -- ASCII range for printable characters
  5. table.insert(chars, string.char(i))
  6. end
  7.  
  8. local key = {}
  9. while #chars > 0 do
  10. local index = math.random(#chars)
  11. table.insert(key, table.remove(chars, index))
  12. end
  13.  
  14. return table.concat(key)
  15. end
  16.  
  17. -- Function to create a map from a key
  18. function createMap(key)
  19. local map = {}
  20. for i = 32, 126 do
  21. map[string.char(i)] = key:sub(i - 31, i - 31)
  22. end
  23. return map
  24. end
  25.  
  26. -- Function to invert a map
  27. function invertMap(map)
  28. local invMap = {}
  29. for k, v in pairs(map) do
  30. invMap[v] = k
  31. end
  32. return invMap
  33. end
  34.  
  35. -- Function to encrypt text using a key
  36. function encrypt(text, key)
  37. local map = createMap(key)
  38. local encrypted = {}
  39. for i = 1, #text do
  40. local char = text:sub(i, i)
  41. table.insert(encrypted, map[char] or char)
  42. end
  43. return table.concat(encrypted)
  44. end
  45.  
  46. -- Function to decrypt text using a key
  47. function decrypt(text, key)
  48. local map = invertMap(createMap(key))
  49. local decrypted = {}
  50. for i = 1, #text do
  51. local char = text:sub(i, i)
  52. table.insert(decrypted, map[char] or char)
  53. end
  54. return table.concat(decrypted)
  55. end
  56.  
  57. -- Function to save the key to a file
  58. function saveKeyToFile(key, dir)
  59. local randomNumber = math.random(1, 1000)
  60. local fileName = dir .. "/key" .. randomNumber .. ".txt"
  61. local file = fs.open(fileName, "w")
  62. file.write(key)
  63. file.close()
  64. return fileName
  65. end
  66.  
  67. -- Function to load the key from a file
  68. function loadKeyFromFile(fileName)
  69. local file = fs.open(fileName, "r")
  70. local key = file.readAll()
  71. file.close()
  72. return key
  73. end
  74.  
  75. -- Function to find the key file in a directory
  76. function findKeyFile(dir)
  77. local files = fs.list(dir)
  78. for _, file in ipairs(files) do
  79. if file:match("^key%d+%.txt$") then
  80. return dir .. "/" .. file
  81. end
  82. end
  83. return nil
  84. end
  85.  
  86. -- Function to load the encrypted text from the password file
  87. function loadEncryptedText(userDir)
  88. local fileName = userDir .. "/password.txt"
  89. if not fs.exists(fileName) then
  90. print("Password file does not exist.")
  91. return nil
  92. end
  93. local file = fs.open(fileName, "r")
  94. local encryptedText = file.readAll()
  95. file.close()
  96. return encryptedText
  97. end
  98.  
  99. -- Main Program
  100. local tArgs = {...}
  101. if #tArgs < 2 then
  102. print("Usage: jambler <encrypt|decrypt> <username> <text> [keyfile (for decrypt)]")
  103. return
  104. end
  105.  
  106. local mode = tArgs[1]
  107. local username = tArgs[2]
  108. local userDir = "/disk/users/" .. username
  109.  
  110. if not fs.exists(userDir) then
  111. fs.makeDir(userDir)
  112. end
  113.  
  114. if mode == "encrypt" then
  115. local text = table.concat(tArgs, " ", 3)
  116. local key = generateKey()
  117. local encryptedText = encrypt(text, key)
  118. local keyFileName = saveKeyToFile(key, userDir)
  119.  
  120. local passwordFile = fs.open(userDir .. "/password.txt", "w")
  121. passwordFile.write(encryptedText)
  122. passwordFile.close()
  123.  
  124. print("Encrypted Text: " .. encryptedText)
  125. print("Key saved to file: " .. keyFileName)
  126. elseif mode == "decrypt" then
  127. local encryptedText = loadEncryptedText(userDir)
  128. if not encryptedText then return end
  129.  
  130. local keyFileName = findKeyFile(userDir)
  131. if not keyFileName then
  132. print("No key file found in the user's directory.")
  133. return
  134. end
  135.  
  136. local key = loadKeyFromFile(keyFileName)
  137. local decryptedText = decrypt(encryptedText, key)
  138.  
  139. print("Decrypted Text: " .. decryptedText)
  140. else
  141. print("Invalid mode. Use 'encrypt' or 'decrypt'.")
  142. end
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement