Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- verze = "23.0401.R1"
- local args = {...}
- if #args < 2 then
- print("Usage: lua encrypt.lua <key> <file> [remove_original]")
- return
- end
- local key = args[1]
- local input_file = args[2]
- local remove_original = args[3] == "true"
- -- check if file is encrypted
- local is_encrypted = input_file:sub(-4) == ".pcr"
- -- read input file
- local file = io.open(input_file, "rb")
- if not file then
- print("Error: Input file not found")
- return
- end
- local input_data = file:read("*all")
- file:close()
- -- encryption function
- local function encrypt(text, key)
- local result = ""
- for i = 1, #text do
- local byte = text:byte(i)
- local key_byte = key:byte((i - 1) % #key + 1)
- result = result .. string.char(bit.bxor(byte, key_byte))
- end
- return result
- end
- -- decryption function
- local function decrypt(ciphertext, key)
- return encrypt(ciphertext, key) -- decryption is just encryption with the same key
- end
- -- encrypt or decrypt data
- local output_data
- local output_file
- if is_encrypted then
- -- decrypt data
- output_data = decrypt(input_data, key)
- output_file = input_file:sub(1, -5)
- else
- -- encrypt data
- output_data = encrypt(input_data, key)
- output_file = input_file .. ".pcr"
- end
- -- write output file
- local file = io.open(output_file, "wb")
- if not file then
- return
- end
- file:write(output_data)
- file:close()
- -- check if output file was created
- if not fs.exists(output_file) then
- return
- end
- -- remove original file if specified
- if remove_original then
- local success, message = fs.delete(input_file)
- if not success then
- return
- end
- -- rename new file
- success, message = fs.move(output_file, input_file)
- if not success then
- return
- end
- else
- -- rename new file
- success, message = fs.move(output_file, input_file .. ".new")
- if not success then
- return
- end
- end
- -- output success message
- if is_encrypted then
- print("File decrypted and saved as " .. output_file)
- else
- print("File encrypted and saved as " .. output_file)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement