Thomasims

Untitled

Jul 8th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. local chars16 = "0123456789ABCDEF"
  2. local chars64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  3. local sets, bsets = {[4]={},[6]={},[8]={}}, {[4]={},[6]={},[8]={}}
  4. for i=1,16 do sets[4][i]=chars16:sub(i,i) bsets[4][sets[4][i]] = i bsets[4][sets[4][i]:lower()] = i end
  5. for i=1,64 do sets[6][i]=chars64:sub(i,i) bsets[6][sets[6][i]] = i end
  6. for i=1,256 do sets[8][i]=string.char(i-1) bsets[8][sets[8][i]] = i end
  7. local function toString(bi, bits, nb)
  8.     nb = nb or 4
  9.     bits = bits or (#tostring(bi))*3.321928
  10.     local base = 2^nb
  11.     local str = ""
  12.     for i=1,math.ceil(bits/nb) do
  13.         str = sets[nb][tonumber(biginteger.band(bi, base-1)+1)] .. str
  14.         bi = biginteger.shr(bi,nb)
  15.     end
  16.     return str
  17. end
  18. local function fromString(str, nb)
  19.     nb = nb or 4
  20.     local num = biginteger.new(0)
  21.     for i=1,#str do
  22.         num = biginteger.bor(biginteger.shl(num, nb), bsets[nb][str:sub(i,i)]-1)
  23.     end
  24.     return num
  25. end
  26. bigint = {
  27.     serialise = toString,
  28.     unserialise = fromString
  29. }
  30. --RSA
  31. local keyindex = {
  32.     pass = function(self, message)
  33.         return biginteger.modpow(message,self.e,self.m)
  34.     end,
  35.     length = function(self)
  36.         return self.bits
  37.     end,
  38.     serialise = function(self, base) -- base = 16, 64, 256
  39.         if base~=256 and base~=64 and base~=16 then error("Base must be 16, 64 or 256",1) end
  40.         local nb = math.log(base)/math.log(2)
  41.         return (base==256 and "8" or (base==64 and "6" or "4")) ..
  42.                 toString(biginteger.new(math.log(self.bits)/math.log(2)),6,nb) ..
  43.                 toString(self.e,self.bits,nb) ..
  44.                 toString(self.m,self.bits*2,nb)
  45.     end
  46. }
  47. local keymeta = {
  48.     __index = keyindex,
  49.     __tostring = function(self) return self:serialise(64) end
  50. }
  51. local function key(exponent, mod, length)
  52.     return setmetatable({e = exponent, m = mod, bits = length}, keymeta)
  53. end
  54.  
  55. rsa = {
  56.     --[[    pub, pri = generateKeyPair(bits)
  57.         Arguments:
  58.             bits: numbers of bits to use for the keys, must be a power of 2
  59.         Returns:
  60.             public key object
  61.             private key object
  62.     ]]
  63.     generateKeyPair = function(bits)
  64.         if (math.log(bits)/math.log(2))%1 ~= 0 or bits<4 then error("Unsupported length", 1) end
  65.         local p = biginteger.newProbPrime(bits)
  66.         local q = biginteger.newProbPrime(bits)
  67.         local n = p*q
  68.         local t = (p-1)*(q-1)
  69.         local e = biginteger.newProbPrime(bits)
  70.         local d = biginteger.modinv(e,t)
  71.         return key(e,n,bits), key(d,n,bits)
  72.     end,
  73.     unserialise = function(str)
  74.         local nb = tonumber(str:sub(1,1)) str=str:sub(2)
  75.         if nb~=8 and nb~=6 and nb~=4 then error("invalid input",1) end
  76.         local lens = math.ceil(6/nb)
  77.         local bits = 2^tonumber(fromString(str:sub(1,lens), nb)) str=str:sub(lens+1)
  78.         local ln = math.ceil(bits/nb)
  79.         local e = fromString(str:sub(1,ln), nb)
  80.         local n = fromString(str:sub(ln+1), nb)
  81.         return key(e,n,bits)
  82.     end
  83. }
Advertisement
Add Comment
Please, Sign In to add comment