MudkipTheEpic

SecureRednet V1

Mar 29th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 42.59 KB | None | 0 0
  1. --SecureRednet V1
  2. --Made for ComputerCraft 1.6
  3.  
  4. local debug=true
  5.  
  6. local salt="xMeQ7FBqJ2w5CGNgWUW6PA24F4xwZq6zGdGLEvvtNPG6U5NzvyD3XeAsRcmqhU46Z6UhphCpjJ4RYvEr23QyB6Wt39vwhAD7J6NaDCwDygp7Xaydj4puBmTmMEeaFRwVuZEqfBPzX5RY9NVFEaMcfYqsZNUg4sxMrYujWgX9tTxTgwJE5xYjqBSDMEpDs2XCr9cvfyhPesfFP6ABkmh7hbmSpLnjBhE2hVwShGyTDTcTDEgz"--),%HVE-2#,-c;Rbfp/'wz'G[Rjr2`P.4~}a(X>R?_gsUsa\"+xE'sp^!&qmw`ebw&rg777UT/T~KK[%$wGbz2)9+a,E'FBcFM~wA+-WNE,:,MHU=$X.m)WJ4](H{zqw[>" --Long enough, right? :P
  7.  
  8. local salt="hey"
  9.  
  10. local protocol="SecureRednet"
  11.  
  12. local rednet=rednet
  13.  
  14. local function debugPrint(...)
  15.     if debug then
  16.         local tArgs={...}
  17.         for i=1,#tArgs do
  18.             if type(tArgs[i])=="table" then
  19.                 local ok,ser=pcall(textutils.serialize,tArgs[i])
  20.                 if ok then
  21.                     print(ser)
  22.                 end
  23.             else
  24.                 print(tostring(tArgs[i]))
  25.             end
  26.             sleep(0.25)
  27.         end
  28.     end
  29. end
  30.  
  31.  
  32.  
  33. --START LIBRARY LOADING
  34.  
  35. --SHA256 BY GRAVITYSCORE
  36.  
  37. local sha256=select(2,pcall(loadstring([====[
  38.  
  39. --  
  40. --  Adaptation of the Secure Hashing Algorithm (SHA-244/256)
  41. --  Found Here: http://lua-users.org/wiki/SecureHashAlgorithm
  42. --  
  43. --  Using an adapted version of the bit library
  44. --  Found Here: https://bitbucket.org/Boolsheet/bslf/src/1ee664885805/bit.lua
  45. --  
  46.  
  47. local MOD = 2^32
  48. local MODM = MOD-1
  49.  
  50. local function memoize(f)
  51.     local mt = {}
  52.     local t = setmetatable({}, mt)
  53.     function mt:__index(k)
  54.         local v = f(k)
  55.         t[k] = v
  56.         return v
  57.     end
  58.     return t
  59. end
  60.  
  61. local function make_bitop_uncached(t, m)
  62.     local function bitop(a, b)
  63.         local res,p = 0,1
  64.         while a ~= 0 and b ~= 0 do
  65.             local am, bm = a % m, b % m
  66.             res = res + t[am][bm] * p
  67.             a = (a - am) / m
  68.             b = (b - bm) / m
  69.             p = p*m
  70.         end
  71.         res = res + (a + b) * p
  72.         return res
  73.     end
  74.     return bitop
  75. end
  76.  
  77. local function make_bitop(t)
  78.     local op1 = make_bitop_uncached(t,2^1)
  79.     local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  80.     return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  81. end
  82.  
  83. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  84.  
  85. local function bxor(a, b, c, ...)
  86.     local z = nil
  87.     if b then
  88.         a = a % MOD
  89.         b = b % MOD
  90.         z = bxor1(a, b)
  91.         if c then z = bxor(z, c, ...) end
  92.         return z
  93.     elseif a then return a % MOD
  94.     else return 0 end
  95. end
  96.  
  97. local function band(a, b, c, ...)
  98.     local z
  99.     if b then
  100.         a = a % MOD
  101.         b = b % MOD
  102.         z = ((a + b) - bxor1(a,b)) / 2
  103.         if c then z = bit32_band(z, c, ...) end
  104.         return z
  105.     elseif a then return a % MOD
  106.     else return MODM end
  107. end
  108.  
  109. local function bnot(x) return (-1 - x) % MOD end
  110.  
  111. local function rshift1(a, disp)
  112.     if disp < 0 then return lshift(a,-disp) end
  113.     return math.floor(a % 2 ^ 32 / 2 ^ disp)
  114. end
  115.  
  116. local function rshift(x, disp)
  117.     if disp > 31 or disp < -31 then return 0 end
  118.     return rshift1(x % MOD, disp)
  119. end
  120.  
  121. local function lshift(a, disp)
  122.     if disp < 0 then return rshift(a,-disp) end
  123.     return (a * 2 ^ disp) % 2 ^ 32
  124. end
  125.  
  126. local function rrotate(x, disp)
  127.     x = x % MOD
  128.     disp = disp % 32
  129.     local low = band(x, 2 ^ disp - 1)
  130.     return rshift(x, disp) + lshift(low, 32 - disp)
  131. end
  132.  
  133. local k = {
  134.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  135.     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  136.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  137.     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  138.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  139.     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  140.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  141.     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  142.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  143.     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  144.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  145.     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  146.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  147.     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  148.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  149.     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  150. }
  151.  
  152. local function str2hexa(s)
  153.     return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  154. end
  155.  
  156. local function num2s(l, n)
  157.     local s = ""
  158.     for i = 1, n do
  159.         local rem = l % 256
  160.         s = string.char(rem) .. s
  161.         l = (l - rem) / 256
  162.     end
  163.     return s
  164. end
  165.  
  166. local function s232num(s, i)
  167.     local n = 0
  168.     for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  169.     return n
  170. end
  171.  
  172. local function preproc(msg, len)
  173.     local extra = 64 - ((len + 9) % 64)
  174.     len = num2s(8 * len, 8)
  175.     msg = msg .. "\128" .. string.rep("\0", extra) .. len
  176.     assert(#msg % 64 == 0)
  177.     return msg
  178. end
  179.  
  180. local function initH256(H)
  181.     H[1] = 0x6a09e667
  182.     H[2] = 0xbb67ae85
  183.     H[3] = 0x3c6ef372
  184.     H[4] = 0xa54ff53a
  185.     H[5] = 0x510e527f
  186.     H[6] = 0x9b05688c
  187.     H[7] = 0x1f83d9ab
  188.     H[8] = 0x5be0cd19
  189.     return H
  190. end
  191.  
  192. local function digestblock(msg, i, H)
  193.     local w = {}
  194.     for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  195.     for j = 17, 64 do
  196.         local v = w[j - 15]
  197.         local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  198.         v = w[j - 2]
  199.         w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  200.     end
  201.  
  202.     local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  203.     for i = 1, 64 do
  204.         local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  205.         local maj = bxor(band(a, b), band(a, c), band(b, c))
  206.         local t2 = s0 + maj
  207.         local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  208.         local ch = bxor (band(e, f), band(bnot(e), g))
  209.         local t1 = h + s1 + ch + k[i] + w[i]
  210.         h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  211.     end
  212.  
  213.     H[1] = band(H[1] + a)
  214.     H[2] = band(H[2] + b)
  215.     H[3] = band(H[3] + c)
  216.     H[4] = band(H[4] + d)
  217.     H[5] = band(H[5] + e)
  218.     H[6] = band(H[6] + f)
  219.     H[7] = band(H[7] + g)
  220.     H[8] = band(H[8] + h)
  221. end
  222.  
  223. local function sha256(msg)
  224.     msg = preproc(msg, #msg)
  225.     local H = initH256({})
  226.     for i = 1, #msg, 64 do digestblock(msg, i, H) end
  227.     return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  228.         num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  229. end
  230. ]====].."\n return sha256","SHA256")))
  231.  
  232. if type(sha256) ~= "function" then
  233.     error("SHA256 error: "..tostring(sha256))
  234. end
  235.  
  236. --AES ENCRYPTION BY KILLAVANILLA
  237.  
  238. local aes=loadstring([===[
  239. -- AES implementation
  240. -- By KillaVanilla
  241.  
  242. local sbox = {
  243. [0]=0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
  244. 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
  245. 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
  246. 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
  247. 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
  248. 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
  249. 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
  250. 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
  251. 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
  252. 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
  253. 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
  254. 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
  255. 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
  256. 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
  257. 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
  258. 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16}
  259.  
  260. local inv_sbox = {
  261. [0]=0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
  262. 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
  263. 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
  264. 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
  265. 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
  266. 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
  267. 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
  268. 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
  269. 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
  270. 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
  271. 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
  272. 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
  273. 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
  274. 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
  275. 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
  276. 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D}
  277.  
  278. local Rcon = {
  279. [0]=0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
  280. 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39,
  281. 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
  282. 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
  283. 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef,
  284. 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
  285. 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b,
  286. 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3,
  287. 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
  288. 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
  289. 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
  290. 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
  291. 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
  292. 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
  293. 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
  294. 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d}
  295.  
  296. -- Finite-field multiplication lookup tables:
  297.  
  298. local mul_2 = {
  299. [0]=0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e,0x10,0x12,0x14,0x16,0x18,0x1a,0x1c,0x1e,
  300. 0x20,0x22,0x24,0x26,0x28,0x2a,0x2c,0x2e,0x30,0x32,0x34,0x36,0x38,0x3a,0x3c,0x3e,
  301. 0x40,0x42,0x44,0x46,0x48,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,0x5e,
  302. 0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6e,0x70,0x72,0x74,0x76,0x78,0x7a,0x7c,0x7e,
  303. 0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e,0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e,
  304. 0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xae,0xb0,0xb2,0xb4,0xb6,0xb8,0xba,0xbc,0xbe,
  305. 0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc,0xce,0xd0,0xd2,0xd4,0xd6,0xd8,0xda,0xdc,0xde,
  306. 0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xee,0xf0,0xf2,0xf4,0xf6,0xf8,0xfa,0xfc,0xfe,
  307. 0x1b,0x19,0x1f,0x1d,0x13,0x11,0x17,0x15,0x0b,0x09,0x0f,0x0d,0x03,0x01,0x07,0x05,
  308. 0x3b,0x39,0x3f,0x3d,0x33,0x31,0x37,0x35,0x2b,0x29,0x2f,0x2d,0x23,0x21,0x27,0x25,
  309. 0x5b,0x59,0x5f,0x5d,0x53,0x51,0x57,0x55,0x4b,0x49,0x4f,0x4d,0x43,0x41,0x47,0x45,
  310. 0x7b,0x79,0x7f,0x7d,0x73,0x71,0x77,0x75,0x6b,0x69,0x6f,0x6d,0x63,0x61,0x67,0x65,
  311. 0x9b,0x99,0x9f,0x9d,0x93,0x91,0x97,0x95,0x8b,0x89,0x8f,0x8d,0x83,0x81,0x87,0x85,
  312. 0xbb,0xb9,0xbf,0xbd,0xb3,0xb1,0xb7,0xb5,0xab,0xa9,0xaf,0xad,0xa3,0xa1,0xa7,0xa5,
  313. 0xdb,0xd9,0xdf,0xdd,0xd3,0xd1,0xd7,0xd5,0xcb,0xc9,0xcf,0xcd,0xc3,0xc1,0xc7,0xc5,
  314. 0xfb,0xf9,0xff,0xfd,0xf3,0xf1,0xf7,0xf5,0xeb,0xe9,0xef,0xed,0xe3,0xe1,0xe7,0xe5,
  315. }
  316.  
  317. local mul_3 = {
  318. [0]=0x00,0x03,0x06,0x05,0x0c,0x0f,0x0a,0x09,0x18,0x1b,0x1e,0x1d,0x14,0x17,0x12,0x11,
  319. 0x30,0x33,0x36,0x35,0x3c,0x3f,0x3a,0x39,0x28,0x2b,0x2e,0x2d,0x24,0x27,0x22,0x21,
  320. 0x60,0x63,0x66,0x65,0x6c,0x6f,0x6a,0x69,0x78,0x7b,0x7e,0x7d,0x74,0x77,0x72,0x71,
  321. 0x50,0x53,0x56,0x55,0x5c,0x5f,0x5a,0x59,0x48,0x4b,0x4e,0x4d,0x44,0x47,0x42,0x41,
  322. 0xc0,0xc3,0xc6,0xc5,0xcc,0xcf,0xca,0xc9,0xd8,0xdb,0xde,0xdd,0xd4,0xd7,0xd2,0xd1,
  323. 0xf0,0xf3,0xf6,0xf5,0xfc,0xff,0xfa,0xf9,0xe8,0xeb,0xee,0xed,0xe4,0xe7,0xe2,0xe1,
  324. 0xa0,0xa3,0xa6,0xa5,0xac,0xaf,0xaa,0xa9,0xb8,0xbb,0xbe,0xbd,0xb4,0xb7,0xb2,0xb1,
  325. 0x90,0x93,0x96,0x95,0x9c,0x9f,0x9a,0x99,0x88,0x8b,0x8e,0x8d,0x84,0x87,0x82,0x81,
  326. 0x9b,0x98,0x9d,0x9e,0x97,0x94,0x91,0x92,0x83,0x80,0x85,0x86,0x8f,0x8c,0x89,0x8a,
  327. 0xab,0xa8,0xad,0xae,0xa7,0xa4,0xa1,0xa2,0xb3,0xb0,0xb5,0xb6,0xbf,0xbc,0xb9,0xba,
  328. 0xfb,0xf8,0xfd,0xfe,0xf7,0xf4,0xf1,0xf2,0xe3,0xe0,0xe5,0xe6,0xef,0xec,0xe9,0xea,
  329. 0xcb,0xc8,0xcd,0xce,0xc7,0xc4,0xc1,0xc2,0xd3,0xd0,0xd5,0xd6,0xdf,0xdc,0xd9,0xda,
  330. 0x5b,0x58,0x5d,0x5e,0x57,0x54,0x51,0x52,0x43,0x40,0x45,0x46,0x4f,0x4c,0x49,0x4a,
  331. 0x6b,0x68,0x6d,0x6e,0x67,0x64,0x61,0x62,0x73,0x70,0x75,0x76,0x7f,0x7c,0x79,0x7a,
  332. 0x3b,0x38,0x3d,0x3e,0x37,0x34,0x31,0x32,0x23,0x20,0x25,0x26,0x2f,0x2c,0x29,0x2a,
  333. 0x0b,0x08,0x0d,0x0e,0x07,0x04,0x01,0x02,0x13,0x10,0x15,0x16,0x1f,0x1c,0x19,0x1a,
  334. }
  335.  
  336. local mul_9 = {
  337. [0]=0x00,0x09,0x12,0x1b,0x24,0x2d,0x36,0x3f,0x48,0x41,0x5a,0x53,0x6c,0x65,0x7e,0x77,
  338. 0x90,0x99,0x82,0x8b,0xb4,0xbd,0xa6,0xaf,0xd8,0xd1,0xca,0xc3,0xfc,0xf5,0xee,0xe7,
  339. 0x3b,0x32,0x29,0x20,0x1f,0x16,0x0d,0x04,0x73,0x7a,0x61,0x68,0x57,0x5e,0x45,0x4c,
  340. 0xab,0xa2,0xb9,0xb0,0x8f,0x86,0x9d,0x94,0xe3,0xea,0xf1,0xf8,0xc7,0xce,0xd5,0xdc,
  341. 0x76,0x7f,0x64,0x6d,0x52,0x5b,0x40,0x49,0x3e,0x37,0x2c,0x25,0x1a,0x13,0x08,0x01,
  342. 0xe6,0xef,0xf4,0xfd,0xc2,0xcb,0xd0,0xd9,0xae,0xa7,0xbc,0xb5,0x8a,0x83,0x98,0x91,
  343. 0x4d,0x44,0x5f,0x56,0x69,0x60,0x7b,0x72,0x05,0x0c,0x17,0x1e,0x21,0x28,0x33,0x3a,
  344. 0xdd,0xd4,0xcf,0xc6,0xf9,0xf0,0xeb,0xe2,0x95,0x9c,0x87,0x8e,0xb1,0xb8,0xa3,0xaa,
  345. 0xec,0xe5,0xfe,0xf7,0xc8,0xc1,0xda,0xd3,0xa4,0xad,0xb6,0xbf,0x80,0x89,0x92,0x9b,
  346. 0x7c,0x75,0x6e,0x67,0x58,0x51,0x4a,0x43,0x34,0x3d,0x26,0x2f,0x10,0x19,0x02,0x0b,
  347. 0xd7,0xde,0xc5,0xcc,0xf3,0xfa,0xe1,0xe8,0x9f,0x96,0x8d,0x84,0xbb,0xb2,0xa9,0xa0,
  348. 0x47,0x4e,0x55,0x5c,0x63,0x6a,0x71,0x78,0x0f,0x06,0x1d,0x14,0x2b,0x22,0x39,0x30,
  349. 0x9a,0x93,0x88,0x81,0xbe,0xb7,0xac,0xa5,0xd2,0xdb,0xc0,0xc9,0xf6,0xff,0xe4,0xed,
  350. 0x0a,0x03,0x18,0x11,0x2e,0x27,0x3c,0x35,0x42,0x4b,0x50,0x59,0x66,0x6f,0x74,0x7d,
  351. 0xa1,0xa8,0xb3,0xba,0x85,0x8c,0x97,0x9e,0xe9,0xe0,0xfb,0xf2,0xcd,0xc4,0xdf,0xd6,
  352. 0x31,0x38,0x23,0x2a,0x15,0x1c,0x07,0x0e,0x79,0x70,0x6b,0x62,0x5d,0x54,0x4f,0x46,
  353. }
  354.  
  355. local mul_11 = {
  356. [0]=0x00,0x0b,0x16,0x1d,0x2c,0x27,0x3a,0x31,0x58,0x53,0x4e,0x45,0x74,0x7f,0x62,0x69,
  357. 0xb0,0xbb,0xa6,0xad,0x9c,0x97,0x8a,0x81,0xe8,0xe3,0xfe,0xf5,0xc4,0xcf,0xd2,0xd9,
  358. 0x7b,0x70,0x6d,0x66,0x57,0x5c,0x41,0x4a,0x23,0x28,0x35,0x3e,0x0f,0x04,0x19,0x12,
  359. 0xcb,0xc0,0xdd,0xd6,0xe7,0xec,0xf1,0xfa,0x93,0x98,0x85,0x8e,0xbf,0xb4,0xa9,0xa2,
  360. 0xf6,0xfd,0xe0,0xeb,0xda,0xd1,0xcc,0xc7,0xae,0xa5,0xb8,0xb3,0x82,0x89,0x94,0x9f,
  361. 0x46,0x4d,0x50,0x5b,0x6a,0x61,0x7c,0x77,0x1e,0x15,0x08,0x03,0x32,0x39,0x24,0x2f,
  362. 0x8d,0x86,0x9b,0x90,0xa1,0xaa,0xb7,0xbc,0xd5,0xde,0xc3,0xc8,0xf9,0xf2,0xef,0xe4,
  363. 0x3d,0x36,0x2b,0x20,0x11,0x1a,0x07,0x0c,0x65,0x6e,0x73,0x78,0x49,0x42,0x5f,0x54,
  364. 0xf7,0xfc,0xe1,0xea,0xdb,0xd0,0xcd,0xc6,0xaf,0xa4,0xb9,0xb2,0x83,0x88,0x95,0x9e,
  365. 0x47,0x4c,0x51,0x5a,0x6b,0x60,0x7d,0x76,0x1f,0x14,0x09,0x02,0x33,0x38,0x25,0x2e,
  366. 0x8c,0x87,0x9a,0x91,0xa0,0xab,0xb6,0xbd,0xd4,0xdf,0xc2,0xc9,0xf8,0xf3,0xee,0xe5,
  367. 0x3c,0x37,0x2a,0x21,0x10,0x1b,0x06,0x0d,0x64,0x6f,0x72,0x79,0x48,0x43,0x5e,0x55,
  368. 0x01,0x0a,0x17,0x1c,0x2d,0x26,0x3b,0x30,0x59,0x52,0x4f,0x44,0x75,0x7e,0x63,0x68,
  369. 0xb1,0xba,0xa7,0xac,0x9d,0x96,0x8b,0x80,0xe9,0xe2,0xff,0xf4,0xc5,0xce,0xd3,0xd8,
  370. 0x7a,0x71,0x6c,0x67,0x56,0x5d,0x40,0x4b,0x22,0x29,0x34,0x3f,0x0e,0x05,0x18,0x13,
  371. 0xca,0xc1,0xdc,0xd7,0xe6,0xed,0xf0,0xfb,0x92,0x99,0x84,0x8f,0xbe,0xb5,0xa8,0xa3,
  372. }
  373.  
  374. local mul_13 = {
  375. [0]=0x00,0x0d,0x1a,0x17,0x34,0x39,0x2e,0x23,0x68,0x65,0x72,0x7f,0x5c,0x51,0x46,0x4b,
  376. 0xd0,0xdd,0xca,0xc7,0xe4,0xe9,0xfe,0xf3,0xb8,0xb5,0xa2,0xaf,0x8c,0x81,0x96,0x9b,
  377. 0xbb,0xb6,0xa1,0xac,0x8f,0x82,0x95,0x98,0xd3,0xde,0xc9,0xc4,0xe7,0xea,0xfd,0xf0,
  378. 0x6b,0x66,0x71,0x7c,0x5f,0x52,0x45,0x48,0x03,0x0e,0x19,0x14,0x37,0x3a,0x2d,0x20,
  379. 0x6d,0x60,0x77,0x7a,0x59,0x54,0x43,0x4e,0x05,0x08,0x1f,0x12,0x31,0x3c,0x2b,0x26,
  380. 0xbd,0xb0,0xa7,0xaa,0x89,0x84,0x93,0x9e,0xd5,0xd8,0xcf,0xc2,0xe1,0xec,0xfb,0xf6,
  381. 0xd6,0xdb,0xcc,0xc1,0xe2,0xef,0xf8,0xf5,0xbe,0xb3,0xa4,0xa9,0x8a,0x87,0x90,0x9d,
  382. 0x06,0x0b,0x1c,0x11,0x32,0x3f,0x28,0x25,0x6e,0x63,0x74,0x79,0x5a,0x57,0x40,0x4d,
  383. 0xda,0xd7,0xc0,0xcd,0xee,0xe3,0xf4,0xf9,0xb2,0xbf,0xa8,0xa5,0x86,0x8b,0x9c,0x91,
  384. 0x0a,0x07,0x10,0x1d,0x3e,0x33,0x24,0x29,0x62,0x6f,0x78,0x75,0x56,0x5b,0x4c,0x41,
  385. 0x61,0x6c,0x7b,0x76,0x55,0x58,0x4f,0x42,0x09,0x04,0x13,0x1e,0x3d,0x30,0x27,0x2a,
  386. 0xb1,0xbc,0xab,0xa6,0x85,0x88,0x9f,0x92,0xd9,0xd4,0xc3,0xce,0xed,0xe0,0xf7,0xfa,
  387. 0xb7,0xba,0xad,0xa0,0x83,0x8e,0x99,0x94,0xdf,0xd2,0xc5,0xc8,0xeb,0xe6,0xf1,0xfc,
  388. 0x67,0x6a,0x7d,0x70,0x53,0x5e,0x49,0x44,0x0f,0x02,0x15,0x18,0x3b,0x36,0x21,0x2c,
  389. 0x0c,0x01,0x16,0x1b,0x38,0x35,0x22,0x2f,0x64,0x69,0x7e,0x73,0x50,0x5d,0x4a,0x47,
  390. 0xdc,0xd1,0xc6,0xcb,0xe8,0xe5,0xf2,0xff,0xb4,0xb9,0xae,0xa3,0x80,0x8d,0x9a,0x97,
  391. }
  392.  
  393. local mul_14 = {
  394. [0]=0x00,0x0e,0x1c,0x12,0x38,0x36,0x24,0x2a,0x70,0x7e,0x6c,0x62,0x48,0x46,0x54,0x5a,
  395. 0xe0,0xee,0xfc,0xf2,0xd8,0xd6,0xc4,0xca,0x90,0x9e,0x8c,0x82,0xa8,0xa6,0xb4,0xba,
  396. 0xdb,0xd5,0xc7,0xc9,0xe3,0xed,0xff,0xf1,0xab,0xa5,0xb7,0xb9,0x93,0x9d,0x8f,0x81,
  397. 0x3b,0x35,0x27,0x29,0x03,0x0d,0x1f,0x11,0x4b,0x45,0x57,0x59,0x73,0x7d,0x6f,0x61,
  398. 0xad,0xa3,0xb1,0xbf,0x95,0x9b,0x89,0x87,0xdd,0xd3,0xc1,0xcf,0xe5,0xeb,0xf9,0xf7,
  399. 0x4d,0x43,0x51,0x5f,0x75,0x7b,0x69,0x67,0x3d,0x33,0x21,0x2f,0x05,0x0b,0x19,0x17,
  400. 0x76,0x78,0x6a,0x64,0x4e,0x40,0x52,0x5c,0x06,0x08,0x1a,0x14,0x3e,0x30,0x22,0x2c,
  401. 0x96,0x98,0x8a,0x84,0xae,0xa0,0xb2,0xbc,0xe6,0xe8,0xfa,0xf4,0xde,0xd0,0xc2,0xcc,
  402. 0x41,0x4f,0x5d,0x53,0x79,0x77,0x65,0x6b,0x31,0x3f,0x2d,0x23,0x09,0x07,0x15,0x1b,
  403. 0xa1,0xaf,0xbd,0xb3,0x99,0x97,0x85,0x8b,0xd1,0xdf,0xcd,0xc3,0xe9,0xe7,0xf5,0xfb,
  404. 0x9a,0x94,0x86,0x88,0xa2,0xac,0xbe,0xb0,0xea,0xe4,0xf6,0xf8,0xd2,0xdc,0xce,0xc0,
  405. 0x7a,0x74,0x66,0x68,0x42,0x4c,0x5e,0x50,0x0a,0x04,0x16,0x18,0x32,0x3c,0x2e,0x20,
  406. 0xec,0xe2,0xf0,0xfe,0xd4,0xda,0xc8,0xc6,0x9c,0x92,0x80,0x8e,0xa4,0xaa,0xb8,0xb6,
  407. 0x0c,0x02,0x10,0x1e,0x34,0x3a,0x28,0x26,0x7c,0x72,0x60,0x6e,0x44,0x4a,0x58,0x56,
  408. 0x37,0x39,0x2b,0x25,0x0f,0x01,0x13,0x1d,0x47,0x49,0x5b,0x55,0x7f,0x71,0x63,0x6d,
  409. 0xd7,0xd9,0xcb,0xc5,0xef,0xe1,0xf3,0xfd,0xa7,0xa9,0xbb,0xb5,0x9f,0x91,0x83,0x8d,
  410. }
  411.  
  412. local bxor = bit.bxor
  413. local insert = table.insert
  414.  
  415. local function copy(input)
  416.     local c = {}
  417.     for i, v in pairs(input) do
  418.         c[i] = v
  419.     end
  420.     return c
  421. end
  422.  
  423. local function subBytes(input, invert)
  424.     for i=1, #input do
  425.         if not (sbox[input[i]] and inv_sbox[input[i]]) then
  426.             error("subBytes: input["..i.."] > 0xFF")
  427.         end
  428.         if invert then
  429.             input[i] = inv_sbox[input[i]]
  430.         else
  431.             input[i] = sbox[input[i]]
  432.         end
  433.     end
  434.     return input
  435. end
  436.  
  437. local function shiftRows(input)
  438.     local copy = {}
  439.     -- Row 1: No change
  440.     copy[1] = input[1]
  441.     copy[2] = input[2]
  442.     copy[3] = input[3]
  443.     copy[4] = input[4]
  444.     -- Row 2: Offset 1
  445.     copy[5] = input[6]
  446.     copy[6] = input[7]
  447.     copy[7] = input[8]
  448.     copy[8] = input[5]
  449.     -- Row 3: Offset 2
  450.     copy[9] = input[11]
  451.     copy[10] = input[12]
  452.     copy[11] = input[9]
  453.     copy[12] = input[10]
  454.     -- Row 4: Offset 3
  455.     copy[13] = input[16]
  456.     copy[14] = input[13]
  457.     copy[15] = input[14]
  458.     copy[16] = input[15]
  459.     return copy
  460. end
  461.  
  462. local function invShiftRows(input)
  463.     local copy = {}
  464.     -- Row 1: No change
  465.     copy[1] = input[1]
  466.     copy[2] = input[2]
  467.     copy[3] = input[3]
  468.     copy[4] = input[4]
  469.     -- Row 2: Offset 1
  470.     copy[5] = input[8]
  471.     copy[6] = input[5]
  472.     copy[7] = input[6]
  473.     copy[8] = input[7]
  474.     -- Row 3: Offset 2
  475.     copy[9] = input[11]
  476.     copy[10] = input[12]
  477.     copy[11] = input[9]
  478.     copy[12] = input[10]
  479.     -- Row 4: Offset 3
  480.     copy[13] = input[14]
  481.     copy[14] = input[15]
  482.     copy[15] = input[16]
  483.     copy[16] = input[13]
  484.     return copy
  485. end
  486.  
  487. local function finite_field_mul(a,b) -- Multiply two numbers in GF(256), assuming that polynomials are 8 bits wide
  488.     local product = 0
  489.     local mulA, mulB = a,b
  490.     for i=1, 8 do
  491.         --print("FFMul: MulA: "..mulA.." MulB: "..mulB)
  492.         if mulA == 0 or mulB == 0 then
  493.             break
  494.         end
  495.         if bit.band(1, mulB) > 0 then
  496.             product = bxor(product, mulA)
  497.         end
  498.         mulB = bit.brshift(mulB, 1)
  499.         local carry = bit.band(0x80, mulA)
  500.         mulA = bit.band(0xFF, bit.blshift(mulA, 1))
  501.         if carry > 0 then
  502.             mulA = bxor( mulA, 0x1B )
  503.         end
  504.     end
  505.     return product
  506. end
  507.  
  508. local function mixColumn(column)
  509.     local output = {}
  510.     --print("MixColumn: #column: "..#column)
  511.     output[1] = bxor( mul_2[column[1]], bxor( mul_3[column[2]], bxor( column[3], column[4] ) ) )
  512.     output[2] = bxor( column[1], bxor( mul_2[column[2]], bxor( mul_3[column[3]], column[4] ) ) )
  513.     output[3] = bxor( column[1], bxor( column[2], bxor( mul_2[column[3]], mul_3[column[4]] ) ) )
  514.     output[4] = bxor( mul_3[column[1]], bxor( column[2], bxor( column[3], mul_2[column[4]] ) ) )
  515.     return output
  516. end
  517.  
  518. local function invMixColumn(column)
  519.     local output = {}
  520.     --print("InvMixColumn: #column: "..#column)
  521.     output[1] = bxor( mul_14[column[1]], bxor( mul_11[column[2]], bxor( mul_13[column[3]], mul_9[column[4]] ) ) )
  522.     output[2] = bxor( mul_9[column[1]], bxor( mul_14[column[2]], bxor( mul_11[column[3]], mul_13[column[4]] ) ) )
  523.     output[3] = bxor( mul_13[column[1]], bxor( mul_9[column[2]], bxor( mul_14[column[3]], mul_11[column[4]] ) ) )
  524.     output[4] = bxor( mul_11[column[1]], bxor( mul_13[column[2]], bxor( mul_9[column[3]], mul_14[column[4]] ) ) )
  525.     return output
  526. end
  527.  
  528. local function mixColumns(input, invert)
  529.     --print("MixColumns: #input: "..#input)
  530.     -- Ooops. I mixed the ROWS instead of the COLUMNS on accident.
  531.     local output = {}
  532.     --[[
  533.     local c1 = { input[1], input[2], input[3], input[4] }
  534.     local c2 = { input[5], input[6], input[7], input[8] }
  535.     local c3 = { input[9], input[10], input[11], input[12] }
  536.     local c4 = { input[13], input[14], input[15], input[16] }
  537.     ]]
  538.     local c1 = { input[1], input[5], input[9], input[13] }
  539.     local c2 = { input[2], input[6], input[10], input[14] }
  540.     local c3 = { input[3], input[7], input[11], input[15] }
  541.     local c4 = { input[4], input[8], input[12], input[16] }
  542.     if invert then
  543.         c1 = invMixColumn(c1)
  544.         c2 = invMixColumn(c2)
  545.         c3 = invMixColumn(c3)
  546.         c4 = invMixColumn(c4)
  547.     else
  548.         c1 = mixColumn(c1)
  549.         c2 = mixColumn(c2)
  550.         c3 = mixColumn(c3)
  551.         c4 = mixColumn(c4)
  552.     end
  553.     --[[
  554.     output[1] = c1[1]
  555.     output[2] = c1[2]
  556.     output[3] = c1[3]
  557.     output[4] = c1[4]
  558.    
  559.     output[5] = c2[1]
  560.     output[6] = c2[2]
  561.     output[7] = c2[3]
  562.     output[8] = c2[4]
  563.    
  564.     output[9] = c3[1]
  565.     output[10] = c3[2]
  566.     output[11] = c3[3]
  567.     output[12] = c3[4]
  568.    
  569.     output[13] = c4[1]
  570.     output[14] = c4[2]
  571.     output[15] = c4[3]
  572.     output[16] = c4[4]
  573.     ]]
  574.    
  575.     output[1] = c1[1]
  576.     output[5] = c1[2]
  577.     output[9] = c1[3]
  578.     output[13] = c1[4]
  579.    
  580.     output[2] = c2[1]
  581.     output[6] = c2[2]
  582.     output[10] = c2[3]
  583.     output[14] = c2[4]
  584.    
  585.     output[3] = c3[1]
  586.     output[7] = c3[2]
  587.     output[11] = c3[3]
  588.     output[15] = c3[4]
  589.    
  590.     output[4] = c4[1]
  591.     output[8] = c4[2]
  592.     output[12] = c4[3]
  593.     output[16] = c4[4]
  594.    
  595.     return output
  596. end
  597.  
  598. local function addRoundKey(input, exp_key, round)
  599.     local output = {}
  600.     for i=1, 16 do
  601.         assert(input[i], "input["..i.."]=nil!")
  602.         assert(exp_key[ ((round-1)*16)+i ], "round_key["..(((round-1)*16)+i).."]=nil!")
  603.         output[i] = bxor( input[i], exp_key[ ((round-1)*16)+i ] )
  604.     end
  605.     return output
  606. end
  607.  
  608. function key_schedule(enc_key)
  609.     local function core(in1, in2, in3, in4, i)
  610.         local s1 = in2
  611.         local s2 = in3
  612.         local s3 = in4
  613.         local s4 = in1
  614.         s1 = bxor(sbox[s1], Rcon[i])
  615.         s2 = sbox[s2]
  616.         s3 = sbox[s3]
  617.         s4 = sbox[s4]
  618.         return s1, s2, s3, s4
  619.     end
  620.    
  621.     local n, b, key_type = 0, 0, 0
  622.    
  623.     -- Len | n | b |
  624.     -- 128 |16 |176|
  625.     -- 192 |24 |208|
  626.     -- 256 |32 |240|
  627.    
  628.     -- Determine keysize:
  629.    
  630.     if #enc_key < 16 then
  631.         error("Encryption key is too small; key size must be more than 16 bytes.")
  632.     elseif #enc_key >= 16 and #enc_key < 24 then
  633.         n = 16
  634.         b = 176
  635.         --key_type = 1
  636.     elseif #enc_key >= 24 and #enc_key < 32 then
  637.         n = 24
  638.         b = 208
  639.         --key_type = 2
  640.     else
  641.         n = 32
  642.         b = 240
  643.         --key_type = 3
  644.     end
  645.    
  646.     local exp_key = {}
  647.     local rcon_iter = 1
  648.     for i=1, n do
  649.         exp_key[i] = enc_key[i]
  650.     end
  651.     while #exp_key < b do
  652.         local t1 = exp_key[#exp_key]
  653.         local t2 = exp_key[#exp_key-1]
  654.         local t3 = exp_key[#exp_key-2]
  655.         local t4 = exp_key[#exp_key-3]
  656.         t1, t2, t3, t4 = core(t1, t2, t3, t4, rcon_iter)
  657.         rcon_iter = rcon_iter+1
  658.         t1 = bxor(t1, exp_key[#exp_key-(n-1)])
  659.         t2 = bxor(t2, exp_key[#exp_key-(n-2)])
  660.         t3 = bxor(t3, exp_key[#exp_key-(n-3)])
  661.         t4 = bxor(t4, exp_key[#exp_key-(n-4)])
  662.         insert(exp_key, t1)
  663.         insert(exp_key, t2)
  664.         insert(exp_key, t3)
  665.         insert(exp_key, t4)
  666.         for i=1, 3 do
  667.             t1 = bxor(exp_key[#exp_key], exp_key[#exp_key-(n-1)])
  668.             t2 = bxor(exp_key[#exp_key-1], exp_key[#exp_key-(n-2)])
  669.             t3 = bxor(exp_key[#exp_key-2], exp_key[#exp_key-(n-3)])
  670.             t4 = bxor(exp_key[#exp_key-3], exp_key[#exp_key-(n-4)])
  671.             insert(exp_key, t1)
  672.             insert(exp_key, t2)
  673.             insert(exp_key, t3)
  674.             insert(exp_key, t4)
  675.         end
  676.         if key_type == 3 then -- If we're processing a 256 bit key...
  677.             -- Take the previous 4 bytes of the expanded key, run them through the sbox,
  678.             -- then XOR them with the previous n bytes of the expanded key, then output them
  679.             -- as the next 4 bytes of expanded key.
  680.             t1 = bxor(sbox[exp_key[#exp_key]], exp_key[#exp_key-(n-1)])
  681.             t2 = bxor(sbox[exp_key[#exp_key-1]], exp_key[#exp_key-(n-2)])
  682.             t3 = bxor(sbox[exp_key[#exp_key-2]], exp_key[#exp_key-(n-3)])
  683.             t4 = bxor(sbox[exp_key[#exp_key-3]], exp_key[#exp_key-(n-4)])
  684.             insert(exp_key, t1)
  685.             insert(exp_key, t2)
  686.             insert(exp_key, t3)
  687.             insert(exp_key, t4)
  688.         end
  689.         if key_type == 2 or key_type == 3 then -- If we're processing a 192-bit or 256-bit key..
  690.             local i = 2
  691.             if key_type == 3 then
  692.                 i = 3
  693.             end
  694.             for j=1, i do
  695.                 t1 = bxor(exp_key[#exp_key], exp_key[#exp_key-(n-1)])
  696.                 t2 = bxor(exp_key[#exp_key-1], exp_key[#exp_key-(n-2)])
  697.                 t3 = bxor(exp_key[#exp_key-2], exp_key[#exp_key-(n-3)])
  698.                 t4 = bxor(exp_key[#exp_key-3], exp_key[#exp_key-(n-4)])
  699.                 insert(exp_key, t1)
  700.                 insert(exp_key, t2)
  701.                 insert(exp_key, t3)
  702.                 insert(exp_key, t4)
  703.             end
  704.         end
  705.     end
  706.     return exp_key
  707. end
  708.  
  709. -- Transform a string of bytes into 16 byte blocks, adding padding to ensure that each block contains 16 bytes.
  710. -- For example:
  711. -- "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" (contains 28 0xFF bytes)
  712. -- Is transformed into this:
  713. -- {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}, {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0,0,0,0} (16 0xFF bytes, followed by 12 0xFF bytes and 4 0x00 bytes for padding)
  714.  
  715. local function breakIntoBlocks(data)
  716.     if type(data) ~= "string" then
  717.         error("breakIntoBlocks: data is not a string", 2)
  718.     end
  719.     while (#data % 16) ~= 0 do
  720.         data = data.."\0"
  721.     end
  722.     local blocks = {}
  723.     local blockNum = 1
  724.     local output = {}
  725.     for i=1, #data, 16 do
  726.         blocks[blockNum] = {}
  727.         for j=1, 16 do
  728.             blocks[blockNum][j] = string.byte(data, ((blockNum-1)*16)+j, ((blockNum-1)*16)+j)
  729.         end
  730.         blockNum = blockNum+1
  731.     end
  732.     return blocks
  733. end
  734.  
  735. -- Transform a string into a series of blocks.
  736.  
  737. -- For example, to get a key from a string:
  738. -- local key = strToBlocks(keyStr)
  739. -- key = key[1]
  740.  
  741. function strToBlocks(str)
  742.     local rawBytestream = {}
  743.     local blocks = {}
  744.     for i=1, #str do
  745.         rawBytestream[i] = string.byte(str, i, i)
  746.     end
  747.     for i=1, math.ceil(#rawBytestream / 16) do
  748.         blocks[i] = {}
  749.         for j=1, 16 do
  750.             blocks[i][j] = rawBytestream[ ((i-1)*16)+j ] or 0
  751.         end
  752.     end
  753.     return blocks
  754. end
  755.  
  756. -- Encrypt / Decrypt individual blocks:
  757.  
  758. function encrypt_block(data, key)
  759.     local exp_key = key_schedule(key)
  760.     local state = data
  761.     local nr = 0
  762.    
  763.     if #exp_key == 176 then -- Key type 1 (128-bits)
  764.         nr = 10
  765.     elseif #exp_key == 208 then -- Key type 2 (192-bits)
  766.         nr = 12
  767.     elseif #exp_key == 240 then -- Key type 3 (256-bits)
  768.         nr = 14
  769.     else
  770.         error("encrypt_block: Unknown key size?", 2)
  771.     end
  772.    
  773.     -- Inital round:
  774.     state = addRoundKey(state, exp_key, 1)
  775.    
  776.     -- Repeat (Nr-1) times:
  777.     for round_num = 2, nr-1 do 
  778.         state = subBytes(state)
  779.         state = shiftRows(state)
  780.         state = mixColumns(state)
  781.         state = addRoundKey(state, exp_key, round_num)
  782.     end
  783.    
  784.     -- Final round (No mixColumns()):
  785.     state = subBytes(state)
  786.     state = shiftRows(state)
  787.     state = addRoundKey(state, exp_key, nr)
  788.     return state
  789. end
  790.  
  791. function decrypt_block(data, key)
  792.     local exp_key = key_schedule(key)
  793.     local state = data
  794.     local nr = 0
  795.    
  796.     if #exp_key == 176 then -- Key type 1 (128-bits)
  797.         nr = 10
  798.     elseif #exp_key == 208 then -- Key type 2 (192-bits)
  799.         nr = 12
  800.     elseif #exp_key == 240 then -- Key type 3 (256-bits)
  801.         nr = 14
  802.     else
  803.         error("decrypt_block: Unknown key size?", 2)
  804.     end
  805.    
  806.     -- Inital round:
  807.     state = addRoundKey(state, exp_key, nr)
  808.    
  809.     -- Repeat (Nr-1) times:
  810.     for round_num = nr-1, 2, -1 do
  811.         state = invShiftRows(state)
  812.         state = subBytes(state, true)
  813.         state = addRoundKey(state, exp_key, round_num)
  814.         state = mixColumns(state, true)
  815.     end
  816.    
  817.     -- Final round (No mixColumns()):
  818.     state = invShiftRows(state)
  819.     state = subBytes(state, true)
  820.     state = addRoundKey(state, exp_key, 1)
  821.     return state
  822. end
  823.  
  824. function encrypt_block_customExpKey(data, exp_key--[[, key_type]]) -- Encrypt blocks, but using a precalculated expanded key instead of performing the key expansion on every step like with the normal encrypt_block(2) call
  825.     local state = data
  826.     local nr = 0
  827.     if #exp_key == 176 then -- Key type 1 (128-bits)
  828.         nr = 10
  829.     elseif #exp_key == 208 then -- Key type 2 (192-bits)
  830.         nr = 12
  831.     elseif #exp_key == 240 then -- Key type 3 (256-bits)
  832.         nr = 14
  833.     else
  834.         error("encrypt_block: Unknown key size?", 2)
  835.     end
  836.    
  837.     -- Inital round:
  838.     state = addRoundKey(state, exp_key, 1)
  839.    
  840.     -- Repeat (Nr-1) times:
  841.     for round_num = 2, nr-1 do 
  842.         state = subBytes(state)
  843.         state = shiftRows(state)
  844.         state = mixColumns(state)
  845.         state = addRoundKey(state, exp_key, round_num)
  846.     end
  847.    
  848.     -- Final round (No mixColumns()):
  849.     state = subBytes(state)
  850.     state = shiftRows(state)
  851.     state = addRoundKey(state, exp_key, nr)
  852.     return state
  853. end
  854.  
  855. function decrypt_block_customExpKey(data, exp_key--[[, key_type]])
  856.     local state = data
  857.     local nr = 0
  858.     if #exp_key == 176 then -- Key type 1 (128-bits)
  859.         nr = 10
  860.     elseif #exp_key == 208 then -- Key type 2 (192-bits)
  861.         nr = 12
  862.     elseif #exp_key == 240 then -- Key type 3 (256-bits)
  863.         nr = 14
  864.     else
  865.         error("decrypt_block: Unknown key size?", 2)
  866.     end
  867.    
  868.     -- Inital round:
  869.     state = addRoundKey(state, exp_key, nr)
  870.    
  871.     -- Repeat (Nr-1) times:
  872.     for round_num = nr-1, 2, -1 do
  873.         state = invShiftRows(state)
  874.         state = subBytes(state, true)
  875.         state = addRoundKey(state, exp_key, round_num)
  876.         state = mixColumns(state, true)
  877.     end
  878.    
  879.     -- Final round (No mixColumns()):
  880.     state = invShiftRows(state)
  881.     state = subBytes(state, true)
  882.     state = addRoundKey(state, exp_key, 1)
  883.     return state
  884. end
  885.  
  886. -- Encrypt / Decrypt bytestreams (tables of bytes):
  887.  
  888. -- ECB (electronic codebook) Mode (not secure, do not use):
  889.  
  890. function encrypt_bytestream_ecb(data, key)
  891.     local blocks = {}
  892.     local outputBytestream = {}
  893.     local exp_key = key_schedule(key)
  894.     for i=1, #data, 16 do
  895.         local block = {}
  896.         for j=1, 16 do
  897.             block[j] = data[i+(j-1)] or 0
  898.         end
  899.         block = encrypt_block_customExpKey(block, exp_key)
  900.         for j=1, 16 do
  901.             table.insert(outputBytestream, block[j])
  902.         end
  903.         os.queueEvent("")
  904.         os.pullEvent("")
  905.     end
  906.     return outputBytestream
  907. end
  908.  
  909. function decrypt_bytestream_ecb(data, key)
  910.     local outputBytestream = {}
  911.     local exp_key = key_schedule(key)
  912.     for i=1, #data, 16 do
  913.         local block = {}
  914.         for j=1, 16 do
  915.             block[j] = data[i+(j-1)] or 0
  916.         end
  917.         block = decrypt_block_customExpKey(block, exp_key)
  918.         for j=1, 16 do
  919.             table.insert(outputBytestream, block[j])
  920.         end
  921.         os.queueEvent("")
  922.         os.pullEvent("")
  923.     end
  924.     for i=#outputBytestream, 1, -1 do
  925.         if outputBytestream[i] ~= 0 then
  926.             break
  927.         else
  928.             outputBytestream[i] = nil
  929.         end
  930.     end
  931.     return outputBytestream
  932. end
  933.  
  934. -- CBC (cipher-block chaining) mode:
  935.  
  936. function encrypt_bytestream(data, key, init_vector)
  937.     local blocks = { init_vector }
  938.     local outputBytestream = {}
  939.     local exp_key = key_schedule(key)
  940.     if not init_vector then
  941.         error("encrypt_bytestream: No initalization vector was passed.", 2)
  942.     end
  943.     for i=1, #data do
  944.         if data[i] == nil or data[i] >= 256 then
  945.             if type(data[i]) == "number" then
  946.                 error("encrypt_bytestream: Invalid data at i="..i.." data[i]="..data[i], 2)
  947.             else
  948.                 error("encrypt_bytestream: Invalid data at i="..i.." data[i]="..type(data[i]), 2)
  949.             end
  950.         end
  951.     end
  952.     local s = os.clock()
  953.     for i=1, math.ceil(#data/16) do
  954.         local block = {}
  955.         if not blocks[i] then
  956.             error("encrypt_bytestream: blocks["..i.."] is nil! Input size: "..#data, 2)
  957.         end
  958.         for j=1, 16 do
  959.             block[j] = data[((i-1)*16)+j] or 0
  960.             block[j] = bxor(block[j], blocks[i][j]) -- XOR this block with the previous one
  961.         end
  962.         --print("#bytes: "..#block)
  963.         block = encrypt_block_customExpKey(block, exp_key)
  964.         table.insert(blocks, block)
  965.         for j=1, 16 do
  966.             insert(outputBytestream, block[j])
  967.         end
  968.         if os.clock() - s >= 2.5 then
  969.             os.queueEvent("")
  970.             os.pullEvent("")
  971.             s = os.clock()
  972.         end
  973.     end
  974.     return outputBytestream
  975. end
  976.  
  977. function decrypt_bytestream(data, key, init_vector)
  978.     local blocks = { init_vector }
  979.     local outputBytestream = {}
  980.     local exp_key = key_schedule(key)
  981.     if not init_vector then
  982.         error("decrypt_bytestream: No initalization vector was passed.", 2)
  983.     end
  984.     local s = os.clock()
  985.     for i=1, math.ceil(#data/16) do
  986.         local block = {}
  987.         if not blocks[i] then
  988.             error("decrypt_bytestream: blocks["..i.."] is nil! Input size: "..#data, 2)
  989.         end
  990.         for j=1, 16 do
  991.             block[j] = data[((i-1)*16)+j] or 0
  992.         end
  993.         table.insert(blocks, block)
  994.         local dec_block = decrypt_block_customExpKey(block, exp_key)
  995.         for j=1, 16 do
  996.             dec_block[j] = bxor(dec_block[j], blocks[i][j]) -- We use XOR on the plaintext, not the ciphertext
  997.             table.insert(outputBytestream, dec_block[j])
  998.         end
  999.         if os.clock() - s >= 2.5 then
  1000.             os.queueEvent("")
  1001.             os.pullEvent("")
  1002.             s = os.clock()
  1003.         end
  1004.     end
  1005.     -- Remove padding:
  1006.     for i=#outputBytestream, #outputBytestream-15, -1 do
  1007.         if outputBytestream[i] ~= 0 then
  1008.             break
  1009.         else
  1010.             outputBytestream[i] = nil
  1011.         end
  1012.     end
  1013.     return outputBytestream
  1014. end
  1015.  
  1016. -- Encrypt / Decrypt strings:
  1017.  
  1018. function encrypt_str(data, key, iv)
  1019.     local byteStream = {}
  1020.     for i=1, #data do
  1021.         table.insert(byteStream, string.byte(data, i, i))
  1022.     end
  1023.     local output_bytestream = {}
  1024.     if iv then
  1025.         output_bytestream = encrypt_bytestream(byteStream, key, iv)
  1026.     else
  1027.         output_bytestream = encrypt_bytestream_ecb(byteStream, key)
  1028.     end
  1029.     local output = ""
  1030.     for i=1, #output_bytestream do
  1031.         output = output..string.char(output_bytestream[i])
  1032.     end
  1033.     return output
  1034. end
  1035.  
  1036. function decrypt_str(data, key, iv)
  1037.     local byteStream = {}
  1038.     for i=1, #data do
  1039.         table.insert(byteStream, string.byte(data, i, i))
  1040.     end
  1041.     local output_bytestream = {}
  1042.     if iv then
  1043.         output_bytestream = decrypt_bytestream(byteStream, key, iv)
  1044.     else
  1045.         output_bytestream = decrypt_bytestream_ecb(byteStream, key)
  1046.     end
  1047.     local output = ""
  1048.     for i=1, #output_bytestream do
  1049.         output = output..string.char(output_bytestream[i])
  1050.     end
  1051.     return output
  1052. end
  1053.  
  1054. function davies_meyer(data, h0)
  1055.     local last_h = h0
  1056.     for dm_iter=1, 16 do
  1057.         for i=1, math.ceil(#data/16) do
  1058.             local block = {}
  1059.             for j=1, 16 do
  1060.                 block[j] = data[((i-1)*16)+j] or 0
  1061.             end
  1062.             local block = encrypt_block(last_h, block)
  1063.             for j=1, 16 do
  1064.                 block[j] = bxor(block[j], last_h[j]) -- XOR h[i-1] with h[i].
  1065.             end
  1066.             last_h = block
  1067.             os.queueEvent("")
  1068.             os.pullEvent("")
  1069.         end
  1070.     end
  1071.     return last_h
  1072. end
  1073.  
  1074. local function increment_ctr(blk)
  1075.     local cpy = {}
  1076.     for i=1, 16 do
  1077.         cpy[i] = blk[i] or 0
  1078.     end
  1079.     cpy[1] = cpy[1] + incAmt
  1080.     for i=2, 16 do
  1081.         if cpy[i-1] <= 255 then
  1082.             break
  1083.         end
  1084.         local carry = cpy[i-1] - 255
  1085.         cpy[i] = cpy[i]+carry
  1086.     end
  1087.     return cpy
  1088. end
  1089.  
  1090. local counter_mode_context = {
  1091.     key = {},
  1092.     ctr = {},
  1093.     stream_cache = {}, -- Use "leftover" bytes from generate() here.
  1094.     set_key = function(self, key)
  1095.         if type(key) == "string" then
  1096.             if #key < 16 then
  1097.                 error("set_key: Key length ("..#key..") must be at least 16 characters!", 2)
  1098.             end
  1099.             for i=1, 16 do
  1100.                 self.key[i] = string.byte(key, i, i)
  1101.             end
  1102.         elseif type(key) == "table" then
  1103.             if #key < 16 then
  1104.                 error("set_key: Key length ("..#key..") must be at least 16 bytes!", 2)
  1105.             end
  1106.             for i=1, 16 do
  1107.                 if type(key[i]) ~= "number" or key[i] > 255 or key[i] < 0 then
  1108.                     if type(key[i]) == "nil" then
  1109.                         error("set_key: Value key["..i.."] is invalid: nil", 2)
  1110.                     else
  1111.                         error("set_key: Value key["..i.."] is invalid: "..key[i], 2)
  1112.                     end
  1113.                 end
  1114.                 self.key[i] = key[i]
  1115.             end
  1116.         else
  1117.             error("set_key: Key type is not supported: "..type(key), 2)
  1118.         end
  1119.     end,
  1120.     set_ctr = function(self, ctr)
  1121.         if type(ctr) == "string" then
  1122.             if #ctr < 16 then
  1123.                 error("set_ctr: Counter length ("..#ctr..") must be at least 16 characters!", 2)
  1124.             end
  1125.             for i=1, 16 do
  1126.                 self.ctr[i] = string.byte(ctr, i, i)
  1127.             end
  1128.         elseif type(ctr) == "table" then
  1129.             if #ctr < 16 then
  1130.                 error("set_ctr: Counter length ("..#ctr..") must be at least 16 bytes!", 2)
  1131.             end
  1132.             for i=1, 16 do
  1133.                 if type(ctr[i]) ~= "number" or ctr[i] > 255 or ctr[i] < 0 then
  1134.                     if type(ctr[i]) == "nil" then
  1135.                         error("set_ctr: Value ctr["..i.."] is invalid: nil", 2)
  1136.                     else
  1137.                         error("set_ctr: Value ctr["..i.."] is invalid: "..ctr[i], 2)
  1138.                     end
  1139.                 end
  1140.                 self.ctr[i] = ctr[i]
  1141.             end
  1142.         elseif type(ctr) == "number" then
  1143.             local b1 = bit.band( ctr, 0xFF )
  1144.             local b2 = bit.band( bit.brshift(bit.band( ctr, 0xFF00 ), 8), 0xFF )
  1145.             local b3 = bit.band( bit.brshift(bit.band( ctr, 0xFF0000 ), 16), 0xFF )
  1146.             local b4 = bit.band( bit.brshift(bit.band( ctr, 0xFF000000 ), 24), 0xFF )
  1147.             self.ctr = {}
  1148.             for i=1, 16 do
  1149.                 self.ctr[i] = 0
  1150.             end
  1151.             self.ctr[1] = b1
  1152.             self.ctr[2] = b2
  1153.             self.ctr[3] = b3
  1154.             self.ctr[4] = b4
  1155.         else
  1156.             error("set_ctr: Counter type is not supported: "..type(ctr), 2)
  1157.         end
  1158.     end,
  1159.     generate = function(self, bytes)
  1160.         local genBytes = {}
  1161.         if #self.stream_cache >= bytes then
  1162.             for i=1, bytes do
  1163.                 table.insert(genBytes, table.remove(self.stream_cache))
  1164.             end
  1165.         else
  1166.             for i=1, #self.stream_cache do
  1167.                 table.insert(genBytes, table.remove(self.stream_cache))
  1168.             end
  1169.             local blocksToGenerate = math.ceil((bytes - #genBytes) / 16)
  1170.             for i=1, blocksToGenerate-1 do
  1171.                 self.ctr = increment_ctr(self.ctr)
  1172.                 local block = encrypt_block(self.ctr, self.key)
  1173.                 for i=1, 16 do
  1174.                     table.insert(genBytes, block[i])
  1175.                 end
  1176.             end
  1177.             self.ctr = increment_ctr(self.ctr)
  1178.             local block = encrypt_block(self.ctr, self.key)
  1179.             for i=1, (bytes - #genBytes) do
  1180.                 table.insert(genBytes, table.remove(block))
  1181.             end
  1182.             for i=1, #block do
  1183.                 table.insert(self.stream_cache, table.remove(block))
  1184.             end
  1185.         end
  1186.         return genBytes
  1187.     end,
  1188. }
  1189.  
  1190. function new_ctrMode(key, iv)
  1191.     local context = {
  1192.         stream_cache = {},
  1193.         key = {},
  1194.         iv = {},
  1195.         __index = counter_mode_context,
  1196.     }
  1197.     setmetatable(context, context)
  1198.     context:set_key(key)
  1199.     context:set_ctr(iv)
  1200.     return context
  1201. end
  1202. ]===],"AES")
  1203. if not aes then return false end
  1204. pcall(aes)
  1205. aes=getfenv(aes)
  1206.  
  1207. --END LIBRARY LOADING
  1208.  
  1209. --START MAIN FUNCTIONS
  1210.  
  1211. local key=tostring(os.getComputerID())
  1212.  
  1213. function setKey(_key)
  1214.         if type(_key)=="string" then
  1215.                 if #_key<16 then
  1216.                         key=_key..("-"):rep(16-#_key)
  1217.                 else
  1218.                         key=_key
  1219.                 end
  1220.         end
  1221. end
  1222.  
  1223. local function toByteStream(...)
  1224.     local key=table.concat({...},"")
  1225.     return {string.byte(key,1,#key)}
  1226. end
  1227.  
  1228. local function fromByteStream(bytestream)
  1229.     if type(bytestream)~="table" then error("EXPECTED TABLE NOT"..type(bytestream),2) end
  1230.     return string.char(unpack(bytestream))
  1231. end
  1232.  
  1233. local function genAltKey()
  1234.     return aes.encrypt_str("ALTKEY:"..math.random(),toByteStream(key..salt))
  1235. end
  1236.  
  1237. local function getResponseTable(altkey,content)
  1238.     return {toByteStream(sha256("CNFRMREG:"..key..altkey..salt)),aes.encrypt_str(toByteStream(tostring(content)),toByteStream(key..altkey..salt))}
  1239. end
  1240.  
  1241. local function getAltKey(str)
  1242.     if not type(str)=="string" then return false end
  1243.     local ok,err=pcall(aes.decrypt_str,str,toByteStream(key..salt))
  1244.     return ok and type(err)=="string" and err:sub(1,7)=="ALTKEY:" and tonumber(str:sub(8,#str))
  1245. end
  1246.  
  1247.  
  1248.  
  1249.  
  1250. function send(content, id, _protocol) --boolean send(String content, String key, [int id]) --*Securely* (sends content to id if id is present, else broadcasts content)
  1251.     local protocol=_protocol or protocol
  1252.     if type(content)=="table" or type(content)=="function" then return false, "Cannot send type: "..type(content) end
  1253.     if not (type(id)=="number" or id==nil) then return false end
  1254.     content=tostring(content)
  1255.     local transmit=function(m) debugPrint("Sending ",m) if id then debugPrint("Sending ID ",id,m) return rednet.send(id,toByteStream(m),protocol) else debugPrint("Broadcasting",m) return rednet.broadcast(toByteStream(m),protocol) end end
  1256.     transmit("REG:")
  1257.     local altKey
  1258.     while not altKey do
  1259.         local id,response=rednet.receive(protocol,2)
  1260.         debugPrint("Response",response)
  1261.         debugPrint("DECODED",fromByteStream(response))
  1262.         altKey=getAltKey(fromByteStream(response))
  1263.         debugPrint("Altkey",altKey)
  1264.     end
  1265.     return transmit(getResponseTable(altKey,content))
  1266. end
  1267.  
  1268. function receive(_protocol,timeout)
  1269.     local protocol=_protocol or protocol
  1270.     local id,response
  1271.     repeat
  1272.         id,response=rednet.receive(protocol,timeout)
  1273.         if response==nil then
  1274.             return false
  1275.         end
  1276.         response=fromByteStream(response)
  1277.         debugPrint("Response:",response)
  1278.     until response=="REG:"
  1279.     local altKey=genAltKey()
  1280.     debugPrint("Altkey",altKey)
  1281.     rednet.send(id,toByteStream(altKey),protocol)
  1282.     local newId,response,content
  1283.     repeat
  1284.         id,response=rednet.receive(protocol,timeout)
  1285.         if response==nil then return false end
  1286.         response=fromByteStream(response)
  1287.         debugPrint("Second response:",response)
  1288.     until id==newId and type(response)=="table" and fromByteStream(response[1])==sha256("CNFMREG:"..key..altKey:sub(8,#altKey)..salt) and pcall(function() content=aes.decrypt_str(fromByteStream(response[2]),toByteStream(key..altKey:sub(8,#altKey)..salt)) return assert(type(content)=="string") end)
  1289.     return content
  1290. end
Advertisement
Add Comment
Please, Sign In to add comment