Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Text Encryption Jambler in ComputerCraft
- -- This script will encrypt and decrypt text using a simple substitution cipher
- -- 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 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[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
- -- Main Program
- local tArgs = {...}
- if #tArgs < 1 then
- print("Usage: jambler <encrypt|decrypt> <text>")
- return
- end
- local mode = tArgs[1]
- local text = table.concat(tArgs, " ", 2)
- -- Set a fixed key for simplicity. In a real scenario, this should be securely shared or dynamically generated.
- local key = generateKey()
- if mode == "encrypt" then
- local encryptedText = encrypt(text, key)
- print("Encrypted Text: " .. encryptedText)
- elseif mode == "decrypt" then
- local decryptedText = decrypt(text, key)
- print("Decrypted Text: " .. decryptedText)
- else
- print("Invalid mode. Use 'encrypt' or 'decrypt'.")
- end
Add Comment
Please, Sign In to add comment