[pycrypto] RSA output on long inputs can sometimes be gibberish

Sebastian Ramacher sebastian+lists at ramacher.at
Fri Jul 27 03:15:58 EDT 2012

Running the tests fails most of the time with:

$ python -m ejtp.rsatest
key1 >> key2
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/tmp/EJTP-lib-python/ejtp/rsatest.py", line 33, in <module>
    test_run(key1, key2)
  File "/tmp/EJTP-lib-python/ejtp/rsatest.py", line 27, in test_run
    ciphertext = encode(plaintext, key1, key2)
  File "/tmp/EJTP-lib-python/ejtp/rsatest.py", line 19, in encode
    return reciever.encrypt(sender.decrypt(msg))
  File "ejtp/util/crypto/rsa.py", line 44, in encrypt
    value[marker:marker+self.blocksize], "")[0])
  File "/usr/lib/python2.7/dist-packages/Crypto/PublicKey/RSA.py", line 150, in
    return pubkey.pubkey.encrypt(self, plaintext, K)
  File "/usr/lib/python2.7/dist-packages/Crypto/PublicKey/pubkey.py", line 75,
in encrypt
    ciphertext=self._encrypt(plaintext, K)
  File "/usr/lib/python2.7/dist-packages/Crypto/PublicKey/RSA.py", line 224, in
    return (self.key._encrypt(c),)
ValueError: Plaintext too large

The code in ejtp/util/crypto/rsa.py looks like you're not doing proper padding
and using fixed sized blocks that don't depend on the key parameters. That won't

You might want to have a look at PKCS#1 OAEP and pycrypto's
Crypto.Cipher.PKCS1_OAEP module [1].

Kind regards


More information about the pycrypto mailing list