Advertisement
tiin57

cipher

Sep 11th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.82 KB | None | 0 0
  1. local function split(self, sep)
  2.     local sep, fields = sep or ":", {}
  3.     local pattern = string.format("([^%s]+)", sep)
  4.     self:gsub(pattern, function(c) fields[#fields+1]=c end)
  5.     return fields
  6. end
  7.  
  8. function encode(text, key)
  9.     local chars = {}
  10.     for i=1, #text do
  11.         chars[i]=text:sub(i, i)
  12.     end
  13.     local rand_num = 1
  14.     for i=1, #key do
  15.         rand_num=rand_num+string.byte(key:sub(i, i))
  16.     end
  17.     local coded = ""
  18.     for _, v in pairs(chars) do
  19.         coded=coded..":"..tostring(string.byte(v)*rand_num)
  20.     end
  21.     return coded:sub(2)
  22. end
  23.  
  24. function decode(text, key)
  25.     local rand_num = 1
  26.     for i=1, #key do
  27.         rand_num=rand_num+string.byte(key:sub(i, i))
  28.     end
  29.     numbers = split(text, ":")
  30.     final = ""
  31.     for _, v in pairs(numbers) do
  32.         if tonumber(v)==nil then break end
  33.         final=final..string.char(tonumber(v)/rand_num)
  34.     end
  35.     return final
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement