Advertisement
DOGGYWOOF

Untitled

Jul 16th, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 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. -- Function to traverse and process files in a directory
  76. function processDirectory(path, processFunction, key)
  77. local files = fs.list(path)
  78. for _, file in ipairs(files) do
  79. local filePath = fs.combine(path, file)
  80. if fs.isDir(filePath) then
  81. processDirectory(filePath, processFunction, key)
  82. else
  83. local file = fs.open(filePath, "r")
  84. local content = file.readAll()
  85. file.close()
  86.  
  87. local processedContent = processFunction(content, key)
  88.  
  89. file = fs.open(filePath, "w")
  90. file.write(processedContent)
  91. file.close()
  92. end
  93. end
  94. end
  95.  
  96. -- Main Program
  97. local tArgs = {...}
  98. if #tArgs < 1 then
  99. print("Usage: jambler <encrypt|decrypt> [keyfile (for decrypt)]")
  100. return
  101. end
  102.  
  103. local mode = tArgs[1]
  104.  
  105. if mode == "encrypt" then
  106. local key = generateKey()
  107. processDirectory("/disk", encrypt, key)
  108. local keyFileName = saveKeyToFile(key)
  109. print("All files in /disk/ encrypted.")
  110. print("Key saved to file: " .. keyFileName)
  111. elseif mode == "decrypt" then
  112. if #tArgs < 2 then
  113. print("A key file is required for decryption.")
  114. return
  115. end
  116. local keyFileName = tArgs[2]
  117. if not fs.exists(keyFileName) then
  118. print("Key file does not exist.")
  119. return
  120. end
  121. local key = loadKeyFromFile(keyFileName)
  122. processDirectory("/disk", decrypt, key)
  123. print("All files in /disk/ decrypted.")
  124. else
  125. print("Invalid mode. Use 'encrypt' or 'decrypt'.")
  126. end
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement