Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- set_password.lua
- -- 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 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 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 save the key to a file
- function saveKeyToFile(key)
- local randomNumber = math.random(1, 1000)
- local fileName = "key" .. randomNumber .. ".txt"
- local file = fs.open(fileName, "w")
- file.write(key)
- file.close()
- return fileName
- end
- -- Main program
- print("Enter a password to set:")
- local password = read("*")
- -- Save the password to hpass.txt
- local file = fs.open("hpass.txt", "w")
- file.write(password)
- file.close()
- -- Generate a new key
- local key = generateKey()
- -- Encrypt the password and save the key
- local encryptedPassword = encrypt(password, key)
- file = fs.open("password.txt", "w")
- file.write(encryptedPassword)
- file.close()
- local keyFileName = saveKeyToFile(key)
- print("Password set and system secured.")
- print("Key saved to file: " .. keyFileName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement