-- Creates RC5 codec -- key: encryption key -- w: word length in bits, optional -- r: number of rounds, optional function rc5(key, w, r) w, r = w or 32, r or 12 local p = {[8] = 0xB8, [16] = 0xB7E1, [32] = 0xB7E15163} local q = {[8] = 0x9E, [16] = 0x9E37, [32] = 0x9E3779B9} local m = 2 ^ w local ws = w / 8 local function rotl(a, b) b = b % w local c = 2 ^ (w - b) b = 2 ^ b return a % c * b + math.floor(a / c) end local function rotr(a, b) b = b % w local c = 2 ^ (w - b) b = 2 ^ b return a % b * c + math.floor(a / b) end local function xor(a, b) local c, pow = 0, 1 local xorl = {[0] = {[0] = 0, [1] = 1}, [1] = {[0] = 1, [1] = 0}} while a > 0 or b > 0 do c = c + xorl[a % 2][b % 2] * pow a = math.floor(a / 2) b = math.floor(b / 2) pow = pow * 2 end return c end local codec = { s = {}, -- Encodes block w1:w2 encode = function(self, w1, w2) w1 = (w1 + self.s[0]) % m w2 = (w2 + self.s[1]) % m for i = 1, r do w1 = (rotl(xor(w1, w2), w2) + self.s[2 * i]) % m w2 = (rotl(xor(w2, w1), w1) + self.s[2 * i + 1]) % m end return w1, w2 end, -- Decodes block w1:w2 decode = function(self, w1, w2) for i = r, 1, -1 do w2 = xor(rotr((w2 - self.s[2 * i + 1]) % m, w1), w1) w1 = xor(rotr((w1 - self.s[2 * i]) % m, w2), w2) end w2 = (w2 - self.s[1]) % m w1 = (w1 - self.s[0]) % m return w1, w2 end, -- Splits string str to block split = function(self, str) local w1, w2, mul = 0, 0, 1 for i = 1, ws do w1 = w1 + (str:byte(i) or 0) * mul w2 = w2 + (str:byte(i + 4) or 0) * mul mul = 256 * mul end return w1, w2 end, -- Joins block w1:w2 to string join = function(self, w1, w2) local res = "" for i = 1, ws do res = res .. string.char(w1 % 256) w1 = math.floor(w1 / 256) end for i = 1, ws do res = res .. string.char(w2 % 256) w2 = math.floor(w2 / 256) end return res end, -- Encodes/decodes string str in ECB mode -- f: codec.encode or codec.decode ecb = function(self, f, str) local res = "" while #res < #str do res = res .. self:join(f(self, self:split(str:sub(#res + 1, #res + 2 * ws + 1)))) end return res end, -- Encodes string str in CBC mode -- iv1, iv2: initialization vector, optional cbc_encode = function(self, str, iv1, iv2) iv1, iv2 = iv1 or 0, iv2 or 0 local res = "" while #res < #str do local w1, w2 = self:split(str:sub(#res + 1, #res + 2 * ws + 1)) iv1, iv2 = self:encode(xor(iv1, w1), xor(iv2, w2)) res = res .. self:join(iv1, iv2) end return res end, -- Decodes string str in CBC mode -- iv1, iv2: initialization vector, optional cbc_decode = function(self, str, iv1, iv2) iv1, iv2 = iv1 or 0, iv2 or 0 local res = "" while #res < #str do local w1, w2 = self:split(str:sub(#res + 1, #res + 2 * ws + 1)) local dw1, dw2 = self:decode(w1, w2) res = res .. self:join(xor(iv1, dw1), xor(iv2, dw2)) iv1, iv2 = w1, w2 end return res end } local c = math.ceil(8 * math.max(#key, 1) / w) local t = 2 * (r + 1) local l = {} for i = #key - 1, 0, -1 do local j = math.floor(8 * i / w) l[j] = (l[j] or 0) * 256 + key:byte(i + 1) end codec.s[0] = p[w] for i = 1, t do codec.s[i] = (codec.s[i - 1] + q[w]) % m end local a, b, i, j = 0, 0, 0, 0 for k = 1, 3 * math.max(t, c) do codec.s[i] = rotl((codec.s[i] + a + b) % m, 3) a = codec.s[i] l[j] = rotl((l[j] + a + b) % m, (a + b) % m) b = l[j] i = (i + 1) % t j = (j + 1) % c end return codec end