OLD | NEW |
1 # Author: Trevor Perrin | 1 # Author: Trevor Perrin |
2 # See the LICENSE file for legal information regarding use of this file. | 2 # See the LICENSE file for legal information regarding use of this file. |
3 | 3 |
4 """PyCrypto RSA implementation.""" | 4 """PyCrypto RSA implementation.""" |
5 | 5 |
6 from .cryptomath import * | 6 from .cryptomath import * |
7 | 7 |
8 from .rsakey import * | 8 from .rsakey import * |
9 from .python_rsakey import Python_RSAKey | 9 from .python_rsakey import Python_RSAKey |
10 | 10 |
11 if pycryptoLoaded: | 11 if pycryptoLoaded: |
12 | 12 |
13 from Crypto.PublicKey import RSA | 13 from Crypto.PublicKey import RSA |
14 | 14 |
15 class PyCrypto_RSAKey(RSAKey): | 15 class PyCrypto_RSAKey(RSAKey): |
16 def __init__(self, n=0, e=0, d=0, p=0, q=0, dP=0, dQ=0, qInv=0): | 16 def __init__(self, n=0, e=0, d=0, p=0, q=0, dP=0, dQ=0, qInv=0): |
17 if not d: | 17 if not d: |
18 self.rsa = RSA.construct( (n, e) ) | 18 self.rsa = RSA.construct( (long(n), long(e)) ) |
19 else: | 19 else: |
20 self.rsa = RSA.construct( (n, e, d, p, q) ) | 20 self.rsa = RSA.construct( (long(n), long(e), long(d), long(p), l
ong(q)) ) |
21 | 21 |
22 def __getattr__(self, name): | 22 def __getattr__(self, name): |
23 return getattr(self.rsa, name) | 23 return getattr(self.rsa, name) |
24 | 24 |
25 def hasPrivateKey(self): | 25 def hasPrivateKey(self): |
26 return self.rsa.has_private() | 26 return self.rsa.has_private() |
27 | 27 |
28 def _rawPrivateKeyOp(self, m): | 28 def _rawPrivateKeyOp(self, m): |
29 s = bytes(numberToByteArray(m, numBytes(self.n))) | 29 c = self.rsa.decrypt((m,)) |
30 c = bytesToNumber(bytearray(self.rsa.decrypt((s,)))) | |
31 return c | 30 return c |
32 | 31 |
33 def _rawPublicKeyOp(self, c): | 32 def _rawPublicKeyOp(self, c): |
34 s = bytes(numberToByteArray(c, numBytes(self.n))) | 33 m = self.rsa.encrypt(c, None)[0] |
35 m = bytesToNumber(bytearray(self.rsa.encrypt(s, None)[0])) | |
36 return m | 34 return m |
37 | 35 |
38 def generate(bits): | 36 def generate(bits): |
39 key = PyCrypto_RSAKey() | 37 key = PyCrypto_RSAKey() |
40 def f(numBytes): | 38 def f(numBytes): |
41 return bytes(getRandomBytes(numBytes)) | 39 return bytes(getRandomBytes(numBytes)) |
42 key.rsa = RSA.generate(bits, f) | 40 key.rsa = RSA.generate(bits, f) |
43 return key | 41 return key |
44 generate = staticmethod(generate) | 42 generate = staticmethod(generate) |
OLD | NEW |