billysback

Encer

Nov 13th, 2012
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.26 KB | None | 0 0
  1.  
  2. local alpha = "pqowieurytlaksjdhfgmznxbcvPLOKIJUHYGTFRDESWAQMNBVCXZ|\\/.,?><#';[]~@:{}=+-_0)9(8*7&6^5%4$3£21! ¬"
  3. alpha = alpha..'"'
  4.  
  5. local function getNum(ch)
  6.     local num = 1
  7.     for i=1,string.len(alpha) do
  8.         if string.sub(alpha, i, i) == ch then num = i end
  9.     end
  10.     return num
  11. end
  12.  
  13. local function getCh(num)
  14.     if num > string.len(alpha) then num = 1 end
  15.     return string.sub(alpha, num, num)
  16. end
  17.  
  18. local cur = 1
  19. local function getRand(pass)
  20.     cur = cur + 1
  21.     if cur > string.len(pass) then cur = 1 end
  22.     local letter = string.sub(pass, cur, cur)
  23.     local num = getNum(letter)
  24.     if (num + cur) <= string.len(alpha) then num = num + cur
  25.     elseif (num - cur) > 1 then num = num - cur else
  26.         if (num + string.len(pass)) <= string.len(alpha) then num = num + string.len(pass)
  27.         elseif (num - string.len(pass)) > 1 then num = num - string.len(pass) end
  28.     end
  29.    
  30.     return num
  31. end
  32.  
  33. local function getRCh(pass, ch, index)
  34.     local i = index * 2
  35.     while i > string.len(pass) or (string.len(pass) > string.len(alpha) and i > string.len(alpha)) do
  36.         i = i - string.len(pass)
  37.     end
  38.     local mod = getRand(pass)
  39.     local n = getNum(ch)
  40.     local r = n + mod
  41.     local omd = mod
  42.     if r > string.len(alpha) then
  43.         r = r - string.len(alpha)
  44.         omd = mod - string.len(pass)
  45.         while omd < 1 do omd = omd + r end
  46.         if omd == mod then omd = omd - 1 end
  47.         if omd > string.len(alpha) then omd = string.len(alpha)-1 end
  48.         if omd == mod then omd = omd - 1 end
  49.     end
  50.     if omd ~= mod and omd > string.len(alpha) then omd = 1 end
  51.     if omd < 1 and omd ~= mod then omd = string.len(alpha) end
  52.     return getCh(r)..getCh(omd)
  53. end
  54.  
  55. local function revRCh(pass, ch1, ch2, index)
  56.     local i = index * 2
  57.     while i > string.len(pass) or (string.len(pass) > string.len(alpha) and i > string.len(alpha)) do
  58.         i = i - string.len(pass)
  59.     end
  60.     local mod = getRand(pass)
  61.     local omod = getNum(ch2)
  62.     local r = getNum(ch1)
  63.    
  64.     if omod ~= mod then
  65.         r = r + string.len(alpha)
  66.     end
  67.    
  68.     local n = r - mod
  69.    
  70.     local ch = getCh(n)
  71.     return ch
  72. end
  73.  
  74. local function gen(pass)
  75.     local sp = 1
  76.     for i=1,string.len(pass) do
  77.         local ch = string.sub(pass, i, i)
  78.         local num = getNum(ch)
  79.         if num <= string.len(pass) then sp = num end
  80.     end
  81.     local key = ""
  82.     for i=sp,string.len(pass) do
  83.         local ch = string.sub(pass, i, i)
  84.         local num = getRand(pass)
  85.         num = num + sp
  86.         local num2 = getNum(ch)
  87.         num2 = num2 + sp
  88.        
  89.         key = key..getCh(num)..getCh(num2)
  90.     end
  91.     return key
  92. end
  93.  
  94. local function enc(pass, str)
  95.     local nstr = ""
  96.     for i=1,string.len(str) do
  97.         local ch = string.sub(str, i, i)
  98.         local nch = getRCh(pass, ch, i)
  99.         nstr = nstr..nch
  100.     end
  101.     return nstr
  102. end
  103.  
  104. local function unenc(pass, str)
  105.     local nstr = ""
  106.     for i=1,string.len(str)/2 do
  107.         local i1 = (i*2)-1
  108.         local i2 = (i*2)
  109.        
  110.         local ch1 = string.sub(str, i1, i1)
  111.         local ch2 = string.sub(str, i2, i2)
  112.        
  113.         local fch = revRCh(pass, ch1, ch2, i)
  114.         nstr = nstr..fch
  115.     end
  116.     return nstr
  117. end
  118.  
  119. local function unencfile(pass, lines, fname)
  120.     if fs.exists(fname) then fs.delete(fname) end
  121.     local file = fs.open(fname, "a")
  122.     for i=2,#lines do
  123.         local line = lines[i]
  124.         local eline = unenc(pass, line)
  125.         file.writeLine(eline)
  126.     end
  127.     file.close()
  128. end
  129.  
  130. local function encfile(pass, lines, fname, key)
  131.     if fs.exists(fname) then fs.delete(fname) end
  132.     local file = fs.open(fname, "a")
  133.     file.writeLine(key)
  134.     for i=1,#lines do
  135.         local line = lines[i]
  136.         local eline = enc(pass, line)
  137.         file.writeLine(eline)
  138.     end
  139.     file.close()
  140. end
  141.  
  142.  
  143. local function getInput()
  144.     local input = read()
  145.     while input == nil or input == "" do
  146.         input = read
  147.     end
  148.     return input
  149. end
  150.  
  151. print("Enter file directory:")
  152. local fname = getInput()
  153. print("Enter password:")
  154. local pass = getInput()
  155. print("Open/Close:")
  156. local cmd = string.lower(getInput())
  157.  
  158. if fs.exists(fname) then
  159.     local file = fs.open(fname, "r")
  160.     local lines = {}
  161.     local line = file.readLine()
  162.     while line ~= nil do
  163.         lines[#lines + 1] = line
  164.         line = file.readLine()
  165.     end
  166.     file.close()
  167.    
  168.     local key = lines[1]
  169.     local gkey = gen(pass)
  170.     print(gkey)
  171.     if cmd == "open" then
  172.         if gkey == key then
  173.             unencfile(pass, lines, fname)
  174.         else
  175.             print("Incorrect password!")
  176.         end
  177.     elseif cmd == "close" then
  178.         encfile(pass, lines, fname, gkey)
  179.     else
  180.         print("Unkown command!")
  181.     end
  182. else
  183.     print("Non existant file!")
  184. end
Advertisement
Add Comment
Please, Sign In to add comment