Encryption 如何使用PyCrypto读取标准openssl rsa私钥并用其解密

Encryption 如何使用PyCrypto读取标准openssl rsa私钥并用其解密,encryption,cryptography,rsa,x509,pycrypto,Encryption,Cryptography,Rsa,X509,Pycrypto,我生成了一个私钥,其中包含: openssl req -x509 -out anytime-pub.der -outform der -new -newkey rsa:2048 -keyout anytime.pem -days 3650 在我的旧代码中,我使用M2Crypto加载密钥文件来解密某些内容,它可以正常工作 from M2Crypto import RSA ServerRSA = RSA.load_key('keys/anytime.pem', passwd) key = Se

我生成了一个私钥,其中包含:

openssl req -x509 -out anytime-pub.der -outform der -new -newkey rsa:2048 -keyout anytime.pem -days 3650
在我的旧代码中,我使用M2Crypto加载密钥文件来解密某些内容,它可以正常工作

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.
>从Crypto.PublicKey导入RSA
>>>key=RSA.importKey(open('key/anytime.pem','r'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
importKey中的文件“/Users/xyzizer/Projects/AnytimeBackend/env/lib/python2.7/site packages/Crypto/PublicKey/RSA.py”,第641行
raise VALUERROR(“不支持PEM加密格式”)
ValueError:不支持PEM加密格式。
有人能告诉我我错在哪里吗


谢谢大家!

它试图告诉您,包装器库不支持ASCII装甲(PEM)

要绕过此问题,请尝试在
openssl req
行中指定参数
-keyform DER


或者从PEM格式中取出base64,
openssl base64-d
并将其输入python代码。

没有错误。私钥编码在密码保护的PKCS#8结构中(在PEM信封中),当前版本的PyCrypto(2.6)无法理解该结构

但是,对PKCS#8的支持可以在库的上找到


编辑:PKCS#8,而不是PKCS#7

如果这解决了您的问题,您能接受它作为答案吗?否则问题就悬而未决。这是在Stackoverflow上表示感谢的方式:)