Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local alpha = "pqowieurytlaksjdhfgmznxbcvPLOKIJUHYGTFRDESWAQMNBVCXZ|\\/.,?><#';[]~@:{}=+-_0)9(8*7&6^5%4$3£21! ¬"
- alpha = alpha..'"'
- local function getNum(ch)
- local num = 1
- for i=1,string.len(alpha) do
- if string.sub(alpha, i, i) == ch then num = i end
- end
- return num
- end
- local function getCh(num)
- if num > string.len(alpha) then num = 1 end
- return string.sub(alpha, num, num)
- end
- local cur = 1
- local function getRand(pass)
- cur = cur + 1
- if cur > string.len(pass) then cur = 1 end
- local letter = string.sub(pass, cur, cur)
- local num = getNum(letter)
- if (num + cur) <= string.len(alpha) then num = num + cur
- elseif (num - cur) > 1 then num = num - cur else
- if (num + string.len(pass)) <= string.len(alpha) then num = num + string.len(pass)
- elseif (num - string.len(pass)) > 1 then num = num - string.len(pass) end
- end
- return num
- end
- local function getRCh(pass, ch, index)
- local i = index * 2
- while i > string.len(pass) or (string.len(pass) > string.len(alpha) and i > string.len(alpha)) do
- i = i - string.len(pass)
- end
- local mod = getRand(pass)
- local n = getNum(ch)
- local r = n + mod
- local omd = mod
- if r > string.len(alpha) then
- r = r - string.len(alpha)
- omd = mod - string.len(pass)
- while omd < 1 do omd = omd + r end
- if omd == mod then omd = omd - 1 end
- if omd > string.len(alpha) then omd = string.len(alpha)-1 end
- if omd == mod then omd = omd - 1 end
- end
- if omd ~= mod and omd > string.len(alpha) then omd = 1 end
- if omd < 1 and omd ~= mod then omd = string.len(alpha) end
- return getCh(r)..getCh(omd)
- end
- local function revRCh(pass, ch1, ch2, index)
- local i = index * 2
- while i > string.len(pass) or (string.len(pass) > string.len(alpha) and i > string.len(alpha)) do
- i = i - string.len(pass)
- end
- local mod = getRand(pass)
- local omod = getNum(ch2)
- local r = getNum(ch1)
- if omod ~= mod then
- r = r + string.len(alpha)
- end
- local n = r - mod
- local ch = getCh(n)
- return ch
- end
- local function gen(pass)
- local sp = 1
- for i=1,string.len(pass) do
- local ch = string.sub(pass, i, i)
- local num = getNum(ch)
- if num <= string.len(pass) then sp = num end
- end
- local key = ""
- for i=sp,string.len(pass) do
- local ch = string.sub(pass, i, i)
- local num = getRand(pass)
- num = num + sp
- local num2 = getNum(ch)
- num2 = num2 + sp
- key = key..getCh(num)..getCh(num2)
- end
- return key
- end
- local function enc(pass, str)
- local nstr = ""
- for i=1,string.len(str) do
- local ch = string.sub(str, i, i)
- local nch = getRCh(pass, ch, i)
- nstr = nstr..nch
- end
- return nstr
- end
- local function unenc(pass, str)
- local nstr = ""
- for i=1,string.len(str)/2 do
- local i1 = (i*2)-1
- local i2 = (i*2)
- local ch1 = string.sub(str, i1, i1)
- local ch2 = string.sub(str, i2, i2)
- local fch = revRCh(pass, ch1, ch2, i)
- nstr = nstr..fch
- end
- return nstr
- end
- local function unencfile(pass, lines, fname)
- if fs.exists(fname) then fs.delete(fname) end
- local file = fs.open(fname, "a")
- for i=2,#lines do
- local line = lines[i]
- local eline = unenc(pass, line)
- file.writeLine(eline)
- end
- file.close()
- end
- local function encfile(pass, lines, fname, key)
- if fs.exists(fname) then fs.delete(fname) end
- local file = fs.open(fname, "a")
- file.writeLine(key)
- for i=1,#lines do
- local line = lines[i]
- local eline = enc(pass, line)
- file.writeLine(eline)
- end
- file.close()
- end
- local function getInput()
- local input = read()
- while input == nil or input == "" do
- input = read
- end
- return input
- end
- print("Enter file directory:")
- local fname = getInput()
- print("Enter password:")
- local pass = getInput()
- print("Open/Close:")
- local cmd = string.lower(getInput())
- if fs.exists(fname) then
- local file = fs.open(fname, "r")
- local lines = {}
- local line = file.readLine()
- while line ~= nil do
- lines[#lines + 1] = line
- line = file.readLine()
- end
- file.close()
- local key = lines[1]
- local gkey = gen(pass)
- print(gkey)
- if cmd == "open" then
- if gkey == key then
- unencfile(pass, lines, fname)
- else
- print("Incorrect password!")
- end
- elseif cmd == "close" then
- encfile(pass, lines, fname, gkey)
- else
- print("Unkown command!")
- end
- else
- print("Non existant file!")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement