我生成了一个私钥:
openssl req -x509 -out anytime-pub.der -outform der -new -newkey rsa:2048 -keyout anytime.pem -days 3650
在我的旧代码中,我使用 M2Crypto 加载 key 文件来解密某些内容,并且可以正常工作。
from M2Crypto import RSA
ServerRSA = RSA.load_key('keys/anytime.pem', passwd)
key = ServerRSA.private_decrypt(b64decode(cipher),1)
但是当我使用 pycrypto 做同样的事情时,会出现以下错误:
>>> 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.
谁能告诉我我的错误是什么?
谢谢!
最佳答案
没有错误。私钥以受密码保护的 PKCS#8 结构(在 PEM 信封内)编码,当前版本的 PyCrypto (2.6) 无法理解。
对 PKCS#8 的支持可在 the current development branch 上获得图书馆虽然。
编辑:PKCS#8,而不是 PKCS#7
关于encryption - 如何使用 PyCrypto 读取标准的 openssl rsa 私钥并用它解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20613946/