1 """PyCrypto RSA implementation."""
3 from cryptomath import *
6 from Python_RSAKey import Python_RSAKey
10 from Crypto.PublicKey import RSA
12 class PyCrypto_RSAKey(RSAKey):
13 def __init__(self, n=0, e=0, d=0, p=0, q=0, dP=0, dQ=0, qInv=0):
15 self.rsa = RSA.construct( (n, e) )
17 self.rsa = RSA.construct( (n, e, d, p, q) )
19 def __getattr__(self, name):
20 return getattr(self.rsa, name)
22 def hasPrivateKey(self):
23 return self.rsa.has_private()
26 return Python_RSAKey(self.n, self.e).hash()
28 def _rawPrivateKeyOp(self, m):
30 byteLength = numBytes(self.n)
31 if len(s)== byteLength:
33 elif len(s) == byteLength-1:
36 raise AssertionError()
37 c = stringToNumber(self.rsa.decrypt((s,)))
40 def _rawPublicKeyOp(self, c):
42 byteLength = numBytes(self.n)
43 if len(s)== byteLength:
45 elif len(s) == byteLength-1:
48 raise AssertionError()
49 m = stringToNumber(self.rsa.encrypt(s, None)[0])
52 def writeXMLPublicKey(self, indent=''):
53 return Python_RSAKey(self.n, self.e).write(indent)
56 key = PyCrypto_RSAKey()
58 return bytesToString(getRandomBytes(numBytes))
59 key.rsa = RSA.generate(bits, f)
61 generate = staticmethod(generate)