Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to generate a key for encryption and decryption
- function generateKey()
- local chars = {}
- for i = 32, 126 do -- ASCII range for printable characters
- table.insert(chars, string.char(i))
- end
- local key = {}
- while #chars > 0 do
- local index = math.random(#chars)
- table.insert(key, table.remove(chars, index))
- end
- return table.concat(key)
- end
- -- Function to create a map from a key
- function createMap(key)
- local map = {}
- for i = 32, 126 do
- map[string.char(i)] = key:sub(i - 31, i - 31)
- end
- return map
- end
- -- Function to invert a map
- function invertMap(map)
- local invMap = {}
- for k, v in pairs(map) do
- invMap[v] = k
- end
- return invMap
- end
- -- Function to encrypt text using a key
- function encrypt(text, key)
- local map = createMap(key)
- local encrypted = {}
- for i = 1, #text do
- local char = text:sub(i, i)
- table.insert(encrypted, map[char] or char)
- end
- return table.concat(encrypted)
- end
- -- Function to decrypt text using a key
- function decrypt(text, key)
- local map = invertMap(createMap(key))
- local decrypted = {}
- for i = 1, #text do
- local char = text:sub(i, i)
- table.insert(decrypted, map[char] or char)
- end
- return table.concat(decrypted)
- end
- -- Function to save the key to a file
- function saveKeyToFile(key, dir)
- local randomNumber = math.random(1, 1000)
- local fileName = dir .. "/key" .. randomNumber .. ".txt"
- local file = fs.open(fileName, "w")
- file.write(key)
- file.close()
- return fileName
- end
- -- Function to load the key from a file
- function loadKeyFromFile(fileName)
- local file = fs.open(fileName, "r")
- local key = file.readAll()
- file.close()
- return key
- end
- -- Function to find the key file in a directory
- function findKeyFile(dir)
- local files = fs.list(dir)
- for _, file in ipairs(files) do
- if file:match("^key%d+%.txt$") then
- return dir .. "/" .. file
- end
- end
- return nil
- end
- -- Function to load the encrypted text from the password file
- function loadEncryptedText(userDir)
- local fileName = userDir .. "/password.txt"
- if not fs.exists(fileName) then
- print("Password file does not exist.")
- return nil
- end
- local file = fs.open(fileName, "r")
- local encryptedText = file.readAll()
- file.close()
- return encryptedText
- end
- -- Main Program
- local tArgs = {...}
- if #tArgs < 2 then
- print("Usage: jambler <encrypt|decrypt> <username> <text> [keyfile (for decrypt)]")
- return
- end
- local mode = tArgs[1]
- local username = tArgs[2]
- local userDir = "/disk/users/" .. username
- if not fs.exists(userDir) then
- fs.makeDir(userDir)
- end
- if mode == "encrypt" then
- local text = table.concat(tArgs, " ", 3)
- local key = generateKey()
- local encryptedText = encrypt(text, key)
- local keyFileName = saveKeyToFile(key, userDir)
- local passwordFile = fs.open(userDir .. "/password.txt", "w")
- passwordFile.write(encryptedText)
- passwordFile.close()
- print("Encrypted Text: " .. encryptedText)
- print("Key saved to file: " .. keyFileName)
- elseif mode == "decrypt" then
- local encryptedText = loadEncryptedText(userDir)
- if not encryptedText then return end
- local keyFileName = findKeyFile(userDir)
- if not keyFileName then
- print("No key file found in the user's directory.")
- return
- end
- local key = loadKeyFromFile(keyFileName)
- local decryptedText = decrypt(encryptedText, key)
- print("Decrypted Text: " .. decryptedText)
- else
- print("Invalid mode. Use 'encrypt' or 'decrypt'.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement