Advertisement
billysback

GridLock

Oct 15th, 2013
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.41 KB | None | 0 0
  1. local conv = {}
  2.  
  3. local lastn = 0
  4. local function getNumber(key, mx)
  5.     local n = 0
  6.     for i=1,string.len(key) do
  7.         n = n + (string.byte(key:sub(i, i))*key:len())
  8.     end
  9.     n = (n + lastn) % mx
  10.     if n == 0 then n = 1 end
  11.     lastn = n
  12.     return n
  13. end
  14.  
  15. local chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!£$%^&*()-=_+[];'#,./{}:@~<>?\\| "..'"'
  16. local function getIndex(char)
  17.     local i = 1
  18.     for j=1,chars:len() do if chars:sub(j,j) == char then i = j end end
  19.     --print("i="..i..", char="..char)
  20.     return i
  21. end
  22.  
  23. local function genConv(password)
  24.     local curc = chars
  25.     local n = 1
  26.     --print(string.len(curc)..":"..#conv)
  27.     while string.len(curc) > 0 do
  28.         local i = getNumber(password, string.len(curc))
  29.         --term.write(":"..i..","..curc:len())
  30.         conv[curc:sub(i, i)] = n
  31.         n = n + 1
  32.         local bef = ""
  33.         if i > 1 then bef = curc:sub(1, i-1) end
  34.         local aft = ""
  35.         if i < string.len(curc) then aft = curc:sub(i+1, -1) end
  36.         curc = bef..aft
  37.     end
  38.     --print("")
  39.     --print(string.len(curc)..":"..#conv)
  40.     lastn = 0
  41. end
  42.  
  43. local function getCoord(a, b)
  44.     local cood = {conv[a], conv[b]}
  45.     return cood
  46. end
  47.  
  48. local function getIs(c1, c2)
  49.     local ci1 = {c1[1], c2[2]}
  50.     local ci2 = {c2[1], c1[2]}
  51.     --print("c1: "..type(ci1).." c2: "..type(ci2).." c1; "..type(ci1[1])..","..type(ci1[2]).." c2; "..type(ci2[1])..","..type(ci2[2]))
  52.     return ci1, ci2
  53. end
  54.  
  55. local function getCoordI(i1, i2)
  56.     local p1 = {i1[1], i2[2]}
  57.     local p2 = {i2[1], i1[2]}
  58.     return p1, p2
  59. end
  60.  
  61. local function getLetter(number)
  62.     --print("n: "..number)
  63.     --[[
  64.     for k,v in pairs(conv) do
  65.         if v == number then return k end
  66.     end
  67.     ]]
  68.     --print("no pair found for "..number)
  69.     number = number%chars:len()
  70.     if number == 0 then number = 1 end
  71.     return chars:sub(number, number)
  72. end
  73.  
  74. local function getCLetter(number)
  75.     for k,v in pairs(conv) do
  76.         if v == number then return k end
  77.     end
  78. end
  79.  
  80. local function getAB(c)
  81.     local a = getCLetter(c[1])
  82.     local b = getCLetter(c[2])
  83.     if a ~= nil and b ~= nil then
  84.         return a..b
  85.     else
  86.         error("Something went wrong! Grid match up returned nil value.. letter invalid? WORKING")
  87.     end
  88. end
  89.  
  90. local function encode(str)
  91.     while string.len(str)%4 ~= 0 do
  92.         str = str.." "
  93.     end
  94.     local estr = ""
  95.     for i=1,str:len(),4 do
  96.         local a1 = str:sub(i, i)
  97.         local b1 = str:sub(i+1, i+1)
  98.         local a2 = str:sub(i+2, i+2)
  99.         local b2 = str:sub(i+3, i+3)
  100.         local co1 = getCoord(a1, b1)
  101.         local co2 = getCoord(a2, b2)
  102.         local i1, i2 = getIs(co1, co2)
  103.         if i == 1 then
  104.             --print("c1: "..co1[1]..","..co1[2].." c2: "..co2[1]..","..co2[2])
  105.             --print("i1: "..i1[1]..","..i1[2].." i2: "..i2[1]..","..i2[2])
  106.         end
  107.         local letters = {getLetter(i1[1]), getLetter(i1[2]), getLetter(i2[1]), getLetter(i2[2])}
  108.         local ok = true
  109.         for i=1,#letters do if letters[i] == nil then ok = false end end
  110.         if #letters < 4 then ok = false error("letters have reduced in size? ("..#letters..")") end
  111.         if ok then
  112.             for i=1,#letters do
  113.                 estr = estr..letters[i]
  114.             end
  115.         else
  116.             error("Something went wrong! Grid match up returned nil value.. letter invalid? (ENCODING)")
  117.         end
  118.     end
  119.     return estr
  120. end
  121.  
  122. local function decode(str)
  123.     local dstr = ""
  124.     for i=1,str:len(),4 do
  125.         --local i1 = {char[str:sub(i, i)], conv[str:sub(i+1, i+1)]}
  126.         --local i2 = {conv[str:sub(i+2, i+2)], conv[str:sub(i+3, i+3)]}
  127.         local i1 = {getIndex(str:sub(i, i)), getIndex(str:sub(i+1, i+1))}
  128.         local i2 = {getIndex(str:sub(i+2, i+2)), getIndex(str:sub(i+3, i+3))}
  129.        
  130.         local p1, p2 = getCoordI(i1, i2)
  131.         if i == 1 then
  132.             --print("i1: "..i1[1]..","..i1[2].." i2: "..i2[1]..","..i2[2])
  133.             --print("c1: "..p1[1]..","..p1[2].." c2: "..p2[1]..","..p2[2])
  134.         end
  135.         local ab1 = getAB(p1)
  136.         local ab2 = getAB(p2)
  137.         if ab1 ~= nil and ab2 ~= nil then
  138.             dstr = dstr..ab1..ab2
  139.         else
  140.             error("Something went wrong! No idea what!")
  141.         end
  142.     end
  143.     return dstr
  144. end
  145.  
  146.  
  147. print("E/D-crypt?")
  148. local cmd = read()
  149. if cmd:lower() == "e" then
  150.     print("password?")
  151.     local pass = read()
  152.     print("text?")
  153.     local text = read()
  154.     genConv(pass)
  155.     local estr = encode(text)
  156.     print(estr)
  157.    
  158.     local file = fs.open("enc", "a")
  159.     file.writeLine("PASS: "..pass.." TEXT: "..text.." ENC: "..estr)
  160.     file.close()
  161.    
  162. elseif cmd:lower() == "d" then
  163.     print("password?")
  164.     local pass = read()
  165.     print("text?")
  166.     local text = read()
  167.     genConv(pass)
  168.     local dstr = decode(text)
  169.     print(dstr)
  170.    
  171.     local file = fs.open("enc", "a")
  172.     file.writeLine("PASS: "..pass.." TEXT: "..text.." DEC: "..dstr)
  173.     file.close()
  174. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement