How can I read the standard openssl rsa private key with PyCrypto and decrypt it

I generated a private key using

openssl req -x509 -out anytime-pub.der -outform der -new -newkey rsa:2048 -keyout anytime.pem -days 3650

In my old code, I use M2Crypto to download the key file to decrypt something, and it works.

from M2Crypto import RSA 

ServerRSA = RSA.load_key('keys/anytime.pem', passwd)
key = ServerRSA.private_decrypt(b64decode(cipher),1)

but when I use pycrypto to do the same thing, the error below occurs:

>>> from Crypto.PublicKey import RSA
>>> key = RSA.importKey(open('keys/anytime.pem', 'r'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/xyzkizer/Projects/AnytimeBackend/env/lib/python2.7/site-packages/Crypto/PublicKey/RSA.py", line 641, in importKey
    raise ValueError("PEM encryption format not supported.")
ValueError: PEM encryption format not supported.

Can someone tell me what my mistake is?

Thanks!

+4
source share
2 answers

There is no mistake. The private key is encoded in the password-protected PKCS # 8 structure (inside the PEM envelope) and is not understood by the current version of PyCrypto (2.6).

PKCS # 8 support is available on the current library development thread .

EDIT: PKCS # 8, PKCS # 7

+3

, ASCII- (PEM) -.

, -keyform DER openssl req.

base64 PEM, openssl base64 -d python.

+1

All Articles