Advertisement
DOGGYWOOF

DOGGY OS SECURITY CREDENTIAL ENCRYPTION CODE

Jul 12th, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 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)
  59. local randomNumber = math.random(1, 1000)
  60. local fileName = "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. -- Main Program
  76. local tArgs = {...}
  77. if #tArgs < 2 then
  78. print("Usage: jambler <encrypt|decrypt> <text> [keyfile (for decrypt)]")
  79. return
  80. end
  81.  
  82. local mode = tArgs[1]
  83. local text = table.concat(tArgs, " ", 2, #tArgs - (mode == "decrypt" and 1 or 0))
  84.  
  85. if mode == "encrypt" then
  86. local key = generateKey()
  87. local encryptedText = encrypt(text, key)
  88. local keyFileName = saveKeyToFile(key)
  89. print("Encrypted Text: " .. encryptedText)
  90. print("Key saved to file: " .. keyFileName)
  91. elseif mode == "decrypt" then
  92. if #tArgs < 3 then
  93. print("A key file is required for decryption.")
  94. return
  95. end
  96. local keyFileName = tArgs[#tArgs]
  97. if not fs.exists(keyFileName) then
  98. print("Key file does not exist.")
  99. return
  100. end
  101. local key = loadKeyFromFile(keyFileName)
  102. local decryptedText = decrypt(text, key)
  103. print("Decrypted Text: " .. decryptedText)
  104. else
  105. print("Invalid mode. Use 'encrypt' or 'decrypt'.")
  106. end
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement