Alyssa

Better Simple Encoding

May 26th, 2015
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.33 KB | None | 0 0
  1. local function code(msg, key, mode, fileMode, convertNums)
  2.     if convertNums then
  3.         nm = ""
  4.         for i = 1, math.floor(#msg/3), 1 do
  5.             os.queueEvent("NCY")
  6.             os.pullEvent("NCY")
  7.             nm = nm .. string.char(tonumber(string.sub(msg,i*3-2,i*3)))
  8.         end
  9.     end
  10.     key = tostring(key)
  11.     msg = tostring(msg)
  12.     if convertNums then msg = nm end
  13.     local kn = 1
  14.     local nmsg = ""
  15.     for i = 1, #msg do
  16.         if i % 50 == 0 then
  17.             os.queueEvent("NO-CRASH-YIELD")
  18.             os.pullEvent("NO-CRASH-YIELD")
  19.         end
  20.         curChar = string.sub(msg, i, i)
  21.         nCurChar = string.byte(curChar)
  22.         curKey = string.sub(key, kn, kn)
  23.         nCurKey = string.byte(curKey)
  24.         if mode == "encode" then
  25.             newChar = nCurChar + nCurKey
  26.         elseif mode == "decode" then
  27.             newChar = nCurChar - nCurKey
  28.         end
  29.         if newChar >= 256 then
  30.             newChar = newChar - 255
  31.         end
  32.         if newChar <= 0 then
  33.             newChar = newChar + 255
  34.         end
  35.         if not fileMode then
  36.             nmsg = nmsg .. string.char(newChar)
  37.         else
  38.             nc = tostring(newChar)
  39.             while #nc < 3 do
  40.                 nc = "0"..nc
  41.             end
  42.             nmsg = nmsg .. nc
  43.         end
  44.         if kn == #key then
  45.             kn = 1
  46.         else
  47.             kn = kn+1
  48.         end
  49.     end
  50.     return nmsg
  51. end
  52.  
  53. function encode(msg, key, doFile, convertNums)
  54.     return code(msg, key, "encode", doFile, convertNums)
  55. end
  56.  
  57. function decode(msg, key, doFile, convertNums)
  58.     return code(msg, key, "decode", doFile, convertNums)
  59. end
Advertisement
Add Comment
Please, Sign In to add comment