Advertisement
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
- -- Function to read passwords from a file and decrypt them
- function readPasswords(filename, key)
- local file = fs.open(filename, "r")
- if not file then
- return {}
- end
- local encryptedData = file.readAll()
- file.close()
- local decryptedData = decrypt(encryptedData, key)
- local passwords = {}
- for line in decryptedData:gmatch("[^\r\n]+") do
- table.insert(passwords, line)
- end
- return passwords
- end
- -- Function to write passwords to a file after encrypting them
- function writePasswords(filename, passwords, key)
- local data = table.concat(passwords, "\n")
- local encryptedData = encrypt(data, key)
- local file = fs.open(filename, "w")
- file.write(encryptedData)
- file.close()
- end
- -- Main Program
- local tArgs = {...}
- if #tArgs < 1 then
- print("Usage: jambler <add|show>")
- return
- end
- local mode = tArgs[1]
- local filename = "passwords.txt"
- local key = generateKey() -- In a real scenario, use a securely stored key
- local passwords = readPasswords(filename, key)
- if mode == "add" then
- if #tArgs < 2 then
- print("Usage: jambler add <password>")
- return
- end
- local password = table.concat(tArgs, " ", 2)
- table.insert(passwords, password)
- writePasswords(filename, passwords, key)
- print("Password added.")
- elseif mode == "show" then
- print("Enter the decrypted password to check:")
- local inputPassword = read()
- local inputEncrypted = encrypt(inputPassword, key)
- local found = false
- for _, password in ipairs(passwords) do
- if encrypt(password, key) == inputEncrypted then
- print("logon success")
- found = true
- break
- end
- end
- if not found then
- print("failure")
- end
- else
- print("Invalid mode. Use 'add' or 'show'.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement