metro80

UTFEncryption

Aug 12th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.91 KB | None | 0 0
  1. charValues = {
  2.     "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
  3.     "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e",
  4.     "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
  5.     "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  6.     "0", " ", ".", ",", ")", "(", "*", "&", "%", "$", "#", "@", "!", "+", "=", ":",
  7.     ";", "\"", "'", "?", "/", "~", "-"
  8. }
  9.  
  10. function convToCV(s, b)
  11.     local nS = ""
  12.     for i=1,string.len(s) do
  13.         o = string.sub(s, i, i)
  14.         for i=1,#charValues do
  15.             if o == charValues[i] then
  16.                 if b then
  17.                     nS = nS..i
  18.                 else
  19.                     nS = nS.."-"..i
  20.                     break
  21.                 end
  22.             end
  23.             if i > #charValues then
  24.             error("Character not present")
  25.             end
  26.         end
  27.     end
  28.     return nS
  29. end
  30.  
  31. function encrypt(s, k)
  32.     if not k then return false end
  33.     local k = tostring(k)
  34.     local nS = ""
  35.     local cS = convToCV(s)
  36.     local kL = #k
  37.     local kC = 1
  38.     local c
  39.     local nK
  40.     local t = false
  41.     local th = false
  42.     local f = false
  43.     local eK
  44.     for i=1,#cS do
  45.         c = string.sub(cS, i, i)
  46.         if c ~= "-" and not t and not th and not f then
  47.             if string.sub(cS, i+1, i+1) ~= "-" then
  48.                 c = string.sub(cS, i, i+1)
  49.                 t = true
  50.                 if string.sub(cS, i+2, i+2) ~= "-" then
  51.                     c = string.sub(cS, i, i+2)
  52.                     th = true
  53.                     if string.sub(cS, i+3, i+3) ~= "-" then
  54.                         c = string.sub(cS, i, i+3)
  55.                         f = true
  56.                     end
  57.                 end
  58.             end
  59.             nK = string.sub(k, kC, kC)
  60.             kC = kC + 1
  61.             if not tonumber(tostring(nK)) then
  62.                 nK = convToCV(nK, true)
  63.             end
  64.             if kC > kL then kC = 1 end
  65.             eK = string.sub(k, 1, 1)
  66.             c = tonumber(c) + tonumber(nK) + tonumber(convToCV(eK, true))
  67.             nS = nS.."-"..c
  68.         else
  69.             if f then
  70.                 f = false
  71.             elseif th then
  72.                 th = false
  73.             elseif t then
  74.                 t = false
  75.             end
  76.         end
  77.     end
  78.     return nS
  79. end
  80.  
  81. function decrypt(s, k)
  82.     if not k then return false end
  83.     local nS = ""
  84.     local k = tostring(k)
  85.     local c
  86.     local t = false
  87.     local th = false
  88.     local f = false
  89.     local kL = #k
  90.     local kC = 1
  91.     local nK
  92.     local eK
  93.     for i=1,#s do
  94.         c = string.sub(s, i, i)
  95.         if c ~= "-" and not t and not th and not f then
  96.             if string.sub(s, i+1, i+1) ~= "-" then
  97.                 c = string.sub(s, i, i+1)
  98.                 t = true
  99.                 if string.sub(s, i+2, i+2) ~= "-" then
  100.                     c = string.sub(s, i, i+2)
  101.                     th = true
  102.                     if string.sub(s, i+3, i+3) ~= "-" then
  103.                         c = string.sub(s, i, i+3)
  104.                         f = true
  105.                     end
  106.                 end
  107.             end
  108.             nK = string.sub(k, kC, kC)
  109.             kC = kC + 1
  110.             if not tonumber(nK) then
  111.                 nK = convToCV(nK, true)
  112.             end
  113.             if kC > kL then kC = 1 end
  114.             eK = string.sub(k, 1, 1)
  115.             c = tonumber(c) - tonumber(nK) - tonumber(convToCV(eK, true))
  116.             if c < 1 then
  117.                 c = 1
  118.             elseif c > #charValues then
  119.                 c = #charValues - 1
  120.             end
  121.             c = charValues[c]
  122.             nS = nS..c
  123.         else
  124.             if f then
  125.                 f = false
  126.             elseif th then
  127.                 th = false
  128.             elseif t then
  129.                 t = false
  130.             end
  131.         end
  132.     end
  133.     return nS
  134. end
Advertisement
Add Comment
Please, Sign In to add comment