Skip to content Skip to sidebar Skip to footer

Python Pycrypto Rsa Encrypt Method Gives Same Results Using Private Or Public Key

I'm trying to understand the pyCrypto encrypt and decrypt methods for public and private keys, and I'm seeing something strange. Suppose I have a set of private and public keys, st

Solution 1:

When you encrypt with a private key, pycrypto is actually using the public key (which can be generated from the private key).

Source: PyCrypto: Decrypt only with public key in file (no private+public key)

You'll find that pycrypto doesn't allow you to decrypt using the public key for good reason:

>>> publicKey.decrypt(enc2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/RSA.py", line 174, in decrypt
    return pubkey.pubkey.decrypt(self, ciphertext)
  File "/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/pubkey.py", line 93, in decrypt
    plaintext=self._decrypt(ciphertext)
  File "/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/RSA.py", line 239, in _decrypt
    mp = self.key._decrypt(cp)
  File "/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/_slowmath.py", line 52, in _decrypt
    raise TypeError("No private key")
TypeError: No private key

Mathematically, RSA makes it possible to encrypt with the private key and decrypt with the public key, but you're not supposed to do that. The public key is PUBLIC - it's something you would readily share and thus would be easily disseminated. There's no added value in that case compared to using a symmetric cipher and a shared key (see: https://crypto.stackexchange.com/questions/2123/rsa-encryption-with-private-key-and-decryption-with-a-public-key)

Conceptually, "encrypting" with the private key is more useful for signing a message whereas the "decryption" using the public key is used for verifying the message.

More background: exchange public/private key in PKCS#1 OAEP encryption/decryption

Post a Comment for "Python Pycrypto Rsa Encrypt Method Gives Same Results Using Private Or Public Key"