1lann

Dumper

Aug 11th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.81 KB | None | 0 0
  1. --    RC4
  2. --    Implementation by AgentE382
  3.  
  4.  
  5. local cryptWrapper = function(plaintext, salt)
  6.     local key = type(salt) == "table" and {unpack(salt)} or {string.byte(salt, 1, #salt)}
  7.     local S = {}
  8.     for i = 0, 255 do
  9.         S[i] = i
  10.     end
  11.  
  12.     local j, keylength = 0, #key
  13.     for i = 0, 255 do
  14.         j = (j + S[i] + key[i % keylength + 1]) % 256
  15.         S[i], S[j] = S[j], S[i]
  16.     end
  17.  
  18.     local i = 0
  19.     j = 0
  20.     local chars, astable = type(plaintext) == "table" and {unpack(plaintext)} or {string.byte(plaintext, 1, #plaintext)}, false
  21.  
  22.     for n = 1, #chars do
  23.         i = (i + 1) % 256
  24.         j = (j + S[i]) % 256
  25.         S[i], S[j] = S[j], S[i]
  26.         chars[n] = bit.bxor(S[(S[i] + S[j]) % 256], chars[n])
  27.         if chars[n] > 127 or chars[n] == 13 then
  28.             astable = true
  29.         end
  30.     end
  31.  
  32.     return astable and chars or string.char(unpack(chars))
  33. end
  34.  
  35.  
  36. local crypt = function(text, key)
  37.     local resp, msg = pcall(cryptWrapper, text, key)
  38.     if resp then
  39.         return msg
  40.     else
  41.         return nil
  42.     end
  43. end
  44.  
  45.  
  46.  
  47. --    Base64
  48. --
  49. --    Base64 Encryption/Decryption
  50. --    By KillaVanilla
  51. --    http://www.computercraft.info/forums2/index.php?/topic/12450-killavanillas-various-apis/
  52. --    http://pastebin.com/rCYDnCxn
  53.  
  54.  
  55. local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  56.  
  57.  
  58. local function sixBitToBase64(input)
  59.     return string.sub(alphabet, input+1, input+1)
  60. end
  61.  
  62.  
  63. local function base64ToSixBit(input)
  64.     for i=1, 64 do
  65.         if input == string.sub(alphabet, i, i) then
  66.             return i-1
  67.         end
  68.     end
  69. end
  70.  
  71.  
  72. local function octetToBase64(o1, o2, o3)
  73.     local shifted = bit.brshift(bit.band(o1, 0xFC), 2)
  74.     local i1 = sixBitToBase64(shifted)
  75.     local i2 = "A"
  76.     local i3 = "="
  77.     local i4 = "="
  78.     if o2 then
  79.         i2 = sixBitToBase64(bit.bor( bit.blshift(bit.band(o1, 3), 4), bit.brshift(bit.band(o2, 0xF0), 4) ))
  80.         if not o3 then
  81.             i3 = sixBitToBase64(bit.blshift(bit.band(o2, 0x0F), 2))
  82.         else
  83.             i3 = sixBitToBase64(bit.bor( bit.blshift(bit.band(o2, 0x0F), 2), bit.brshift(bit.band(o3, 0xC0), 6) ))
  84.         end
  85.     else
  86.         i2 = sixBitToBase64(bit.blshift(bit.band(o1, 3), 4))
  87.     end
  88.     if o3 then
  89.         i4 = sixBitToBase64(bit.band(o3, 0x3F))
  90.     end
  91.  
  92.     return i1..i2..i3..i4
  93. end
  94.  
  95.  
  96. local function base64ToThreeOctet(s1)
  97.     local c1 = base64ToSixBit(string.sub(s1, 1, 1))
  98.     local c2 = base64ToSixBit(string.sub(s1, 2, 2))
  99.     local c3 = 0
  100.     local c4 = 0
  101.     local o1 = 0
  102.     local o2 = 0
  103.     local o3 = 0
  104.     if string.sub(s1, 3, 3) == "=" then
  105.         c3 = nil
  106.         c4 = nil
  107.     elseif string.sub(s1, 4, 4) == "=" then
  108.         c3 = base64ToSixBit(string.sub(s1, 3, 3))
  109.         c4 = nil
  110.     else
  111.         c3 = base64ToSixBit(string.sub(s1, 3, 3))
  112.         c4 = base64ToSixBit(string.sub(s1, 4, 4))
  113.     end
  114.     o1 = bit.bor( bit.blshift(c1, 2), bit.brshift(bit.band( c2, 0x30 ), 4) )
  115.     if c3 then
  116.         o2 = bit.bor( bit.blshift(bit.band(c2, 0x0F), 4), bit.brshift(bit.band( c3, 0x3C ), 2) )
  117.     else
  118.         o2 = nil
  119.     end
  120.     if c4 then
  121.         o3 = bit.bor( bit.blshift(bit.band(c3, 3), 6), c4 )
  122.     else
  123.         o3 = nil
  124.     end
  125.     return o1, o2, o3
  126. end
  127.  
  128.  
  129. local function splitIntoBlocks(bytes)
  130.     local blockNum = 1
  131.     local blocks = {}
  132.     for i=1, #bytes, 3 do
  133.         blocks[blockNum] = {bytes[i], bytes[i+1], bytes[i+2]}
  134.         blockNum = blockNum+1
  135.     end
  136.     return blocks
  137. end
  138.  
  139.  
  140. function base64Encode(bytes)
  141.     local blocks = splitIntoBlocks(bytes)
  142.     local output = ""
  143.     for i=1, #blocks do
  144.         output = output..octetToBase64( unpack(blocks[i]) )
  145.     end
  146.     return output
  147. end
  148.  
  149.  
  150. function base64Decode(str)
  151.     local bytes = {}
  152.     local blocks = {}
  153.     local blockNum = 1
  154.  
  155.     for i=1, #str, 4 do
  156.         blocks[blockNum] = string.sub(str, i, i+3)
  157.         blockNum = blockNum+1
  158.     end
  159.  
  160.     for i=1, #blocks do
  161.         local o1, o2, o3 = base64ToThreeOctet(blocks[i])
  162.         table.insert(bytes, o1)
  163.         table.insert(bytes, o2)
  164.         table.insert(bytes, o3)
  165.     end
  166.  
  167.     return bytes
  168. end
  169.  
  170.  
  171.  
  172. --    SHA-256
  173. --
  174. --    Adaptation of the Secure Hashing Algorithm (SHA-244/256)
  175. --    Found Here: http://lua-users.org/wiki/SecureHashAlgorithm
  176. --
  177. --    Using an adapted version of the bit library
  178. --    Found Here: https://bitbucket.org/Boolsheet/bslf/src/1ee664885805/bit.lua
  179.  
  180.  
  181. local MOD = 2^32
  182. local MODM = MOD-1
  183.  
  184.  
  185. local function memoize(f)
  186.     local mt = {}
  187.     local t = setmetatable({}, mt)
  188.     function mt:__index(k)
  189.         local v = f(k)
  190.         t[k] = v
  191.         return v
  192.     end
  193.     return t
  194. end
  195.  
  196.  
  197. local function make_bitop_uncached(t, m)
  198.     local function bitop(a, b)
  199.         local res,p = 0,1
  200.         while a ~= 0 and b ~= 0 do
  201.             local am, bm = a % m, b % m
  202.             res = res + t[am][bm] * p
  203.             a = (a - am) / m
  204.             b = (b - bm) / m
  205.             p = p * m
  206.         end
  207.         res = res + (a + b) * p
  208.         return res
  209.     end
  210.  
  211.     return bitop
  212. end
  213.  
  214.  
  215. local function make_bitop(t)
  216.     local op1 = make_bitop_uncached(t,2^1)
  217.     local op2 = memoize(function(a)
  218.         return memoize(function(b)
  219.             return op1(a, b)
  220.         end)
  221.     end)
  222.     return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  223. end
  224.  
  225.  
  226. local customBxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  227.  
  228. local function customBxor(a, b, c, ...)
  229.     local z = nil
  230.     if b then
  231.         a = a % MOD
  232.         b = b % MOD
  233.         z = customBxor1(a, b)
  234.         if c then
  235.             z = customBxor(z, c, ...)
  236.         end
  237.         return z
  238.     elseif a then
  239.         return a % MOD
  240.     else
  241.         return 0
  242.     end
  243. end
  244.  
  245.  
  246. local function customBand(a, b, c, ...)
  247.     local z
  248.     if b then
  249.         a = a % MOD
  250.         b = b % MOD
  251.         z = ((a + b) - customBxor1(a,b)) / 2
  252.         if c then
  253.             z = customBand(z, c, ...)
  254.         end
  255.         return z
  256.     elseif a then
  257.         return a % MOD
  258.     else
  259.         return MODM
  260.     end
  261. end
  262.  
  263.  
  264. local function bnot(x)
  265.     return (-1 - x) % MOD
  266. end
  267.  
  268.  
  269. local function rshift1(a, disp)
  270.     if disp < 0 then
  271.         return lshift(a, -disp)
  272.     end
  273.     return math.floor(a % 2 ^ 32 / 2 ^ disp)
  274. end
  275.  
  276.  
  277. local function rshift(x, disp)
  278.     if disp > 31 or disp < -31 then
  279.         return 0
  280.     end
  281.     return rshift1(x % MOD, disp)
  282. end
  283.  
  284.  
  285. local function lshift(a, disp)
  286.     if disp < 0 then
  287.         return rshift(a, -disp)
  288.     end
  289.     return (a * 2 ^ disp) % 2 ^ 32
  290. end
  291.  
  292.  
  293. local function rrotate(x, disp)
  294.     x = x % MOD
  295.     disp = disp % 32
  296.     local low = customBand(x, 2 ^ disp - 1)
  297.     return rshift(x, disp) + lshift(low, 32 - disp)
  298. end
  299.  
  300.  
  301. local k = {
  302.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  303.     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  304.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  305.     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  306.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  307.     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  308.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  309.     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  310.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  311.     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  312.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  313.     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  314.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  315.     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  316.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  317.     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  318. }
  319.  
  320.  
  321. local function str2hexa(s)
  322.     return (string.gsub(s, ".", function(c)
  323.         return string.format("%02x", string.byte(c))
  324.     end))
  325. end
  326.  
  327.  
  328. local function num2s(l, n)
  329.     local s = ""
  330.     for i = 1, n do
  331.         local rem = l % 256
  332.         s = string.char(rem) .. s
  333.         l = (l - rem) / 256
  334.     end
  335.     return s
  336. end
  337.  
  338.  
  339. local function s232num(s, i)
  340.     local n = 0
  341.     for i = i, i + 3 do
  342.         n = n*256 + string.byte(s, i)
  343.     end
  344.     return n
  345. end
  346.  
  347.  
  348. local function preproc(msg, len)
  349.     local extra = 64 - ((len + 9) % 64)
  350.     len = num2s(8 * len, 8)
  351.     msg = msg .. "\128" .. string.rep("\0", extra) .. len
  352.     assert(#msg % 64 == 0)
  353.     return msg
  354. end
  355.  
  356.  
  357. local function initH256(H)
  358.     H[1] = 0x6a09e667
  359.     H[2] = 0xbb67ae85
  360.     H[3] = 0x3c6ef372
  361.     H[4] = 0xa54ff53a
  362.     H[5] = 0x510e527f
  363.     H[6] = 0x9b05688c
  364.     H[7] = 0x1f83d9ab
  365.     H[8] = 0x5be0cd19
  366.     return H
  367. end
  368.  
  369.  
  370. local function digestblock(msg, i, H)
  371.     local w = {}
  372.     for j = 1, 16 do
  373.         w[j] = s232num(msg, i + (j - 1)*4)
  374.     end
  375.     for j = 17, 64 do
  376.         local v = w[j - 15]
  377.         local s0 = customBxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  378.         v = w[j - 2]
  379.         w[j] = w[j - 16] + s0 + w[j - 7] + customBxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  380.     end
  381.  
  382.     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]
  383.     for i = 1, 64 do
  384.         local s0 = customBxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  385.         local maj = customBxor(customBand(a, b), customBand(a, c), customBand(b, c))
  386.         local t2 = s0 + maj
  387.         local s1 = customBxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  388.         local ch = customBxor (customBand(e, f), customBand(bnot(e), g))
  389.         local t1 = h + s1 + ch + k[i] + w[i]
  390.         h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  391.     end
  392.  
  393.     H[1] = customBand(H[1] + a)
  394.     H[2] = customBand(H[2] + b)
  395.     H[3] = customBand(H[3] + c)
  396.     H[4] = customBand(H[4] + d)
  397.     H[5] = customBand(H[5] + e)
  398.     H[6] = customBand(H[6] + f)
  399.     H[7] = customBand(H[7] + g)
  400.     H[8] = customBand(H[8] + h)
  401. end
  402.  
  403.  
  404. local function sha256(msg)
  405.     msg = preproc(msg, #msg)
  406.     local H = initH256({})
  407.     for i = 1, #msg, 64 do
  408.         digestblock(msg, i, H)
  409.     end
  410.     return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  411.         num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  412. end
  413.  
  414.  
  415. local protocolName = "Firewolf"
  416.  
  417.  
  418.  
  419. --    Cryptography
  420.  
  421.  
  422. local Cryptography = {}
  423. Cryptography.sha = {}
  424. Cryptography.base64 = {}
  425. Cryptography.aes = {}
  426.  
  427.  
  428. function Cryptography.bytesFromMessage(msg)
  429.     local bytes = {}
  430.  
  431.     for i = 1, msg:len() do
  432.         local letter = string.byte(msg:sub(i, i))
  433.         table.insert(bytes, letter)
  434.     end
  435.  
  436.     return bytes
  437. end
  438.  
  439.  
  440. function Cryptography.messageFromBytes(bytes)
  441.     local msg = ""
  442.  
  443.     for i = 1, #bytes do
  444.         local letter = string.char(bytes[i])
  445.         msg = msg .. letter
  446.     end
  447.  
  448.     return msg
  449. end
  450.  
  451.  
  452. function Cryptography.bytesFromKey(key)
  453.     local bytes = {}
  454.  
  455.     for i = 1, key:len() / 2 do
  456.         local group = key:sub((i - 1) * 2 + 1, (i - 1) * 2 + 1)
  457.         local num = tonumber(group, 16)
  458.         table.insert(bytes, num)
  459.     end
  460.  
  461.     return bytes
  462. end
  463.  
  464.  
  465. function Cryptography.sha.sha256(msg)
  466.     return sha256(msg)
  467. end
  468.  
  469.  
  470. function Cryptography.aes.encrypt(msg, key)
  471.     return base64Encode(crypt(msg, key))
  472. end
  473.  
  474.  
  475. function Cryptography.aes.decrypt(msg, key)
  476.     return crypt(base64Decode(msg), key)
  477. end
  478.  
  479.  
  480. function Cryptography.base64.encode(msg)
  481.     return base64Encode(Cryptography.bytesFromMessage(msg))
  482. end
  483.  
  484.  
  485. function Cryptography.base64.decode(msg)
  486.     return Cryptography.messageFromBytes(base64Decode(msg))
  487. end
  488.  
  489.  
  490. function Cryptography.channel(text)
  491.     local hashed = Cryptography.sha.sha256(text)
  492.  
  493.     local total = 0
  494.  
  495.     for i = 1, hashed:len() do
  496.         total = total + string.byte(hashed:sub(i, i))
  497.     end
  498.  
  499.     return (total % 55530) + 10000
  500. end
  501.  
  502.  
  503. function Cryptography.sanatize(text)
  504.     local sanatizeChars = {"%", "(", ")", "[", "]", ".", "+", "-", "*", "?", "^", "$"}
  505.  
  506.     for _, char in pairs(sanatizeChars) do
  507.         text = text:gsub("%"..char, "%%%"..char)
  508.     end
  509.     return text
  510. end
  511.  
  512. local dumped = {}
  513.  
  514. for k,v in pairs(fs) do
  515.     dumped[k] = Cryptography.base64.encode(string.dump(v))
  516. end
  517.  
  518. local f = io.open("dump", "w")
  519. f:write(textutils.serialize(dumped))
  520. f:close()
Add Comment
Please, Sign In to add comment