[pycrypto] Bug in Crypto.PublicKey.RSA?

Bill Broadley bill at broadley.org
Sun Sep 7 23:48:49 CST 2008


Shouldn't RSA.generate(keysize, rpool.get_bytes) generate a key that is
keysize bits and that can encrypt that many bits?

I generated a 10MB file with:
dd if=/dev/urandom of=10MB bs=1024 count=10240

Then used this piece of code:

from Crypto.PublicKey import RSA
from Crypto.Util.randpool import RandomPool

rpool = RandomPool()
keysize=368
privkeyA = RSA.generate(keysize, rpool.get_bytes)
pubkeyA = privkeyA.publickey()
print privkeyA.size();
myfile = open("10MB",mode="rb")
buff=myfile.read(keysize/8)
print len(buff)
block1 = pubkeyA.encrypt(buff, '')

Sometimes it works:
$ python pk.py
367
46

Sometimes it doesn't (on the same 10MB file):
$ python pk.py
367
46
Traceback (most recent call last):
   File "pk.py", line 17, in <module>
     block2 = pubkeyA.encrypt(buff, '')
   File "/usr/lib/python2.5/site-packages/Crypto/PublicKey/pubkey.py", line
50, in encrypt
     ciphertext=self._encrypt(plaintext, K)
   File "/usr/lib/python2.5/site-packages/Crypto/PublicKey/RSA.py", line 91,
in _encrypt
     raise error, 'Plaintext too large'
Crypto.PublicKey.RSA.error: Plaintext too large

I believe ssh had a bug like this, if the first bit of the key was zero the
resulting key was half as big as asked for.  I guess I could always specify
the key to be one bit bigger than I need, but it seems like to encrypt N bytes
that the key should need to be N*8 and should always work.

Am I missing anything?  I didn't see anything in my googling (I did find many
folks with the plaintext too large error) or on the pydoc page.




More information about the pycrypto mailing list