encryption - 如何使用 PyCrypto 读取标准的 openssl rsa 私钥并用它解密

标签 encryption cryptography rsa x509 pycrypto

我生成了一个私钥:

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/

相关文章:

php - 如何使用 PHP 的 OpenSSL 模块更改私钥的密码?

c - 如何使用 RSA 公钥解密数据?

haskell - 通过生成素数使用 QuickCheck

amazon-web-services - 如何使用 Cloud Formation Template 在 S3 存储桶上设置 SSE-S3 或 SSE-KMS 加密?

c++ - 使用 CryptoPP::HexDecoder( )

c# - iOS和C#之间的AES加密

python - 输出 SecKeyCopyExternalRepresentation

python - 如何在python中使用RSA私钥加密数据?

c# - 为什么此加密的字符串中包含有趣的字符?不可读吗?

encryption - RSA 私钥的哪些部分是保密的?