Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function clr() term.clear() end
- function cp(x,y) term.setCursorPos(x,y) end
- function encrypt()
- clr() cp(1,1)
- write("Message: ")
- strn = read()
- write("Key: ")
- key = read()
- write("Save to /")
- save = read()
- print("Encrypting...")
- strn = tostring(strn)
- key = tonumber(key)
- if type(strn) ~= "string" then print("strn error") return end
- if type(key) ~= "number" then print("key error") return end
- local cnt = 1
- local encry = {}
- local s_key = {}
- for character in strn:gmatch(".") do
- if #encry < 1 then
- encry[1] = string.byte(character)
- else
- encry[#encry+1] = string.byte(character)
- end
- end
- for character in tostring(key):gmatch(".") do
- if #s_key < 1 then
- s_key[1] = string.byte(character)
- else
- s_key[#s_key+1] = string.byte(character)
- end
- end
- for i=1, #encry do
- encry[i] = encry[i] + s_key[cnt]
- if cnt == #s_key then
- cnt = 1
- else
- cnt = cnt + 1
- end
- end
- print("Done!")
- if save ~= "" then
- if fs.exists(save) then fs.delete(save) end
- local file = fs.open(save, "w")
- for i=1, #encry do
- file.write(encry[i])
- end
- file.close()
- end
- for i=1, #encry do
- write(string.char(encry[i]))
- end
- end
- function decrypt()
- write("File /")
- save = read()
- if not fs.exists(save) then clr() cp(1,1) print("Invalid") return end
- write("Key: ")
- key = read()
- print("Decrypting...")
- local file = fs.open(save, "r")
- strn = file.readLine()
- file.close()
- strn = tostring(strn)
- local fin = false
- local b_strn = string.len(strn)
- local r_strn = {}
- local s_key = {}
- local cnt = 1
- for character in key:gmatch(".") do
- if #s_key < 1 then
- s_key[1] = string.byte(character)
- else
- s_key[#s_key+1] = string.byte(character)
- end
- end
- local decode = {}
- for character in strn:gmatch(".") do
- if character == "." then
- if #decode < 1 then
- for i=1, #r_strn do
- if decode[1] == nil then
- decode[1] = r_strn[i]
- else
- decode[1] = decode[1]..r_strn[i]
- end
- end
- r_strn = {}
- else
- for i=1, #r_strn do
- if decode[#decode+1] == nil then
- decode[#decode+1] = r_strn[i]
- else
- decode[#decode+1] = decode[#decode+1]..r_strn[i]
- end
- end
- r_strn = {}
- end
- else
- if #r_strn < 1 then
- r_strn[1] = character
- else
- r_strn[#r_strn+1] = character
- end
- end
- end
- for i=1, #decode do
- decode[i] = tonumber(decode[i])
- decode[i] = decode[i] - s_key[cnt]
- if cnt == #s_key then
- cnt = 1
- else
- cnt = cnt + 1
- end
- end
- for i=1, #decode do
- if i == 1 then
- print(string.char(decode[i]))
- else
- print(string.char(decode[i]*-1))
- end
- end
- local file = fs.open(save, "w")
- for i=1, #decode do
- file.write(decode[i])
- end
- file.close()
- print("Done!")
- end
- encrypt()
Advertisement
Add Comment
Please, Sign In to add comment