Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local conv = {}
- local lastn = 0
- local function getNumber(key, mx)
- local n = 0
- for i=1,string.len(key) do
- n = n + (string.byte(key:sub(i, i))*key:len())
- end
- n = (n + lastn) % mx
- if n == 0 then n = 1 end
- lastn = n
- return n
- end
- local chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!£$%^&*()-=_+[];'#,./{}:@~<>?\\| "..'"'
- local function getIndex(char)
- local i = 1
- for j=1,chars:len() do if chars:sub(j,j) == char then i = j end end
- --print("i="..i..", char="..char)
- return i
- end
- local function genConv(password)
- local curc = chars
- local n = 1
- --print(string.len(curc)..":"..#conv)
- while string.len(curc) > 0 do
- local i = getNumber(password, string.len(curc))
- --term.write(":"..i..","..curc:len())
- conv[curc:sub(i, i)] = n
- n = n + 1
- local bef = ""
- if i > 1 then bef = curc:sub(1, i-1) end
- local aft = ""
- if i < string.len(curc) then aft = curc:sub(i+1, -1) end
- curc = bef..aft
- end
- --print("")
- --print(string.len(curc)..":"..#conv)
- lastn = 0
- end
- local function getCoord(a, b)
- local cood = {conv[a], conv[b]}
- return cood
- end
- local function getIs(c1, c2)
- local ci1 = {c1[1], c2[2]}
- local ci2 = {c2[1], c1[2]}
- --print("c1: "..type(ci1).." c2: "..type(ci2).." c1; "..type(ci1[1])..","..type(ci1[2]).." c2; "..type(ci2[1])..","..type(ci2[2]))
- return ci1, ci2
- end
- local function getCoordI(i1, i2)
- local p1 = {i1[1], i2[2]}
- local p2 = {i2[1], i1[2]}
- return p1, p2
- end
- local function getLetter(number)
- --print("n: "..number)
- --[[
- for k,v in pairs(conv) do
- if v == number then return k end
- end
- ]]
- --print("no pair found for "..number)
- number = number%chars:len()
- if number == 0 then number = 1 end
- return chars:sub(number, number)
- end
- local function getCLetter(number)
- for k,v in pairs(conv) do
- if v == number then return k end
- end
- end
- local function getAB(c)
- local a = getCLetter(c[1])
- local b = getCLetter(c[2])
- if a ~= nil and b ~= nil then
- return a..b
- else
- error("Something went wrong! Grid match up returned nil value.. letter invalid? WORKING")
- end
- end
- local function encode(str)
- while string.len(str)%4 ~= 0 do
- str = str.." "
- end
- local estr = ""
- for i=1,str:len(),4 do
- local a1 = str:sub(i, i)
- local b1 = str:sub(i+1, i+1)
- local a2 = str:sub(i+2, i+2)
- local b2 = str:sub(i+3, i+3)
- local co1 = getCoord(a1, b1)
- local co2 = getCoord(a2, b2)
- local i1, i2 = getIs(co1, co2)
- if i == 1 then
- --print("c1: "..co1[1]..","..co1[2].." c2: "..co2[1]..","..co2[2])
- --print("i1: "..i1[1]..","..i1[2].." i2: "..i2[1]..","..i2[2])
- end
- local letters = {getLetter(i1[1]), getLetter(i1[2]), getLetter(i2[1]), getLetter(i2[2])}
- local ok = true
- for i=1,#letters do if letters[i] == nil then ok = false end end
- if #letters < 4 then ok = false error("letters have reduced in size? ("..#letters..")") end
- if ok then
- for i=1,#letters do
- estr = estr..letters[i]
- end
- else
- error("Something went wrong! Grid match up returned nil value.. letter invalid? (ENCODING)")
- end
- end
- return estr
- end
- local function decode(str)
- local dstr = ""
- for i=1,str:len(),4 do
- --local i1 = {char[str:sub(i, i)], conv[str:sub(i+1, i+1)]}
- --local i2 = {conv[str:sub(i+2, i+2)], conv[str:sub(i+3, i+3)]}
- local i1 = {getIndex(str:sub(i, i)), getIndex(str:sub(i+1, i+1))}
- local i2 = {getIndex(str:sub(i+2, i+2)), getIndex(str:sub(i+3, i+3))}
- local p1, p2 = getCoordI(i1, i2)
- if i == 1 then
- --print("i1: "..i1[1]..","..i1[2].." i2: "..i2[1]..","..i2[2])
- --print("c1: "..p1[1]..","..p1[2].." c2: "..p2[1]..","..p2[2])
- end
- local ab1 = getAB(p1)
- local ab2 = getAB(p2)
- if ab1 ~= nil and ab2 ~= nil then
- dstr = dstr..ab1..ab2
- else
- error("Something went wrong! No idea what!")
- end
- end
- return dstr
- end
- print("E/D-crypt?")
- local cmd = read()
- if cmd:lower() == "e" then
- print("password?")
- local pass = read()
- print("text?")
- local text = read()
- genConv(pass)
- local estr = encode(text)
- print(estr)
- local file = fs.open("enc", "a")
- file.writeLine("PASS: "..pass.." TEXT: "..text.." ENC: "..estr)
- file.close()
- elseif cmd:lower() == "d" then
- print("password?")
- local pass = read()
- print("text?")
- local text = read()
- genConv(pass)
- local dstr = decode(text)
- print(dstr)
- local file = fs.open("enc", "a")
- file.writeLine("PASS: "..pass.." TEXT: "..text.." DEC: "..dstr)
- file.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement