def dict2RSA(**kw):
""" Create Crypto.PublicKey.RSA from dict
Required RSA priv. key params (as long)
n, e - modulus and public exponent (public key only)
n, d, e - modulus, private and public exponent
or
p, q, e - primes p, q, and public exponent e
If also dp, dq, qinv present, they are checked to be consistent.
Default value for e is 0x10001
Return Crypto.PublicKey.RSA object
dp = d mod (p-1), dq = d mod (q-1), q*qinv mod p = 1
"""
for par in ('n', 'd', 'p', 'q', 'dp', 'dq', 'qinv'):
if par in kw:
assert isinstance(long(kw[par]), long), \
"RSA parameter %s must be long" % par
e = long(kw.get('e', 0x10001L))
if all([par not in kw for par in ('d', 'p', 'q', 'dp', 'dq', 'qinv')]):
assert 'n' in kw, "At least modulus must be in dict"
return RSA.construct((kw['n'], e))
if 'n' in kw and 'd' in kw:
return RSA.construct((kw['n'], e, kw['d']))
assert 'p' in kw and 'q' in kw, "Either n, d or p, q must be in dict"
p = kw['p']
q = kw['q']
n = p*q
d = number.inverse(e, (p-1)*(q-1))
if 'd' in kw:
assert d == kw['d'], "Inconsinstent private exponent"
if 'dp' in kw:
assert d % (p-1) == kw['dp'], "Inconsistent d mod (p-1)"
if 'dq' in kw:
assert d % (q-1) == kw['dq'], "Inconsistent d mod (q-1)"
u = number.inverse(q, p)
if 'qinv' in kw:
assert u == kw['qinv'], "Inconsistent q inv"
return RSA.construct((n, e, d, q, p, u))
评论列表
文章目录