Advertisement
programcreator

API: encrypt

Jan 27th, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.79 KB | None | 0 0
  1. -- I, didn't create the api so I don't take any merit for it. I post it because I use it in my programms I think it is extremely well written.
  2. local function enc(str, key)
  3.     if not key then return false end
  4.     local f = ""
  5.     local kb = {}
  6.     for i = 1, #key do
  7.         kb[i] = string.byte(key:sub(i,i)) - 31
  8.     end
  9.     local c = 1
  10.     for i = 1, #str do
  11.         local t1 = string.byte(str:sub(i,i))
  12.         if t1 == 10 then
  13.             f = f .. string.char(10)
  14.         else
  15.             local t2 = t1 + kb[c]
  16.             if t2 > 126 then
  17.                 local t3 = t2 - 126
  18.                 t2 = 31 + t3
  19.             end
  20.         f = f .. string.char(t2)
  21.         end
  22.         c = c + 1
  23.         if c > #key then
  24.             c = 1
  25.         end
  26.     end
  27.     return f
  28. end
  29.  
  30. local function dec(str, key)
  31.     if not key then return false end
  32.     local f = ""
  33.     local kb = {}
  34.     for i = 1, #key do
  35.         kb[i] = string.byte(key:sub(i,i)) - 31
  36.     end
  37.     local c = 1
  38.     for i = 1, #str do
  39.         local t2 = string.byte(str:sub(i,i)) - 31
  40.         if t2 == -21 then
  41.             f = f .. string.char(10)
  42.         else
  43.             local t3 = t2 - kb[c] + 31
  44.             if t3 < 32 then
  45.                 t3 = t3 + 126 - 31
  46.             end
  47.             if t3 < 32 then return false, t3 end
  48.             if t3 > 126 then return false, t3 end
  49.             f = f .. string.char(t3)
  50.         end
  51.         c = c + 1
  52.         if c > #key then
  53.             c = 1
  54.         end
  55.     end
  56.     return f
  57. end
  58.  
  59. function encrypt(str, key)
  60.     if not key then return false end
  61.     if #key < 3 then return false end
  62.     local f = str
  63.     key = key .. key .. "#me1"
  64.     for i = 0, #key - 1 do
  65.         f = enc(f, key:sub(i,i + 1))
  66.     end
  67.     f = enc(f, key:sub(1,2) .. key:sub(#key - 1, #key) .. enc(key, key .. key))
  68.     return f
  69. end
  70.  
  71. function decrypt(str, key)
  72.     if not key then return false end
  73.     if #key < 3 then return false end
  74.     local f = str
  75.     key = key .. key .. "#me1"
  76.     for i = 0, #key - 1 do
  77.         f = dec(f, key:sub(i,i + 1))
  78.     end
  79.     f = dec(f, key:sub(1,2) .. key:sub(#key - 1, #key) .. enc(key, key .. key))
  80.     return f
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement