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

Viewed 3345

I generated a private key with:

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 load 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, it occurs error below:

>>> 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 anybody tell me what's my mistake?

Thank you!

2 Answers
Related