我有一个 PEM 格式的 RSA 公钥 + PKCS#1(我猜):
I have a RSA public key in PEM format + PKCS#1(I guess):
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAJNrHWRFgWLqgzSmLBq2G89exgi/Jk1NWhbFB9gHc9MLORmP3BOCJS9k
onzT/+Dk1hdZf00JGgZeuJGoXK9PX3CIKQKRQRHpi5e1vmOCrmHN5VMOxGO4d+zn
JDEbNHODZR4HzsSdpQ9SGMSx7raJJedEIbr0IP6DgnWgiA7R1mUdAgMBAAE=
-----END RSA PUBLIC KEY-----
我想在 Python 中获取其 ASN1 编码版本的 SHA1 摘要.第一步应该是读取这个密钥,但是我在 PyCrypto 中没有这样做:
I want to get the SHA1 digest of its ASN1 encoded version in Python. The first step should be to read this key, but I failed to do it in PyCrypto:
>> from Crypto.PublicKey import RSA
>> RSA.importKey(my_key)
ValueError: RSA key format is not supported
PyCrypto 文档说 PEM + PKCS#支持1,所以我很困惑.我也试过 M2Crypto,但事实证明 M2Crypto 不支持 PKCS#1,只支持 X.509.
The documentation of PyCrypto says PEM + PKCS#1 is supported, so I'm confused. I've also tried M2Crypto, but it turns out that M2Crypto does not support PKCS#1 but only X.509.
PyCrypto 支持 PKCS#1,因为它可以读取 X.509 SubjectPublicKeyInfo
对象包含一个以 PKCS#1 编码的 RSA 公钥.
PyCrypto supports PKCS#1 in the sense that it can read in X.509 SubjectPublicKeyInfo
objects that contain an RSA public key encoded in PKCS#1.
相反,在您的密钥中编码的数据是一个纯 RSAPublicKey
对象(即具有两个整数、模数和公共指数的 ASN.1 SEQUENCE).
Instead, the data encoded in your key is a pure RSAPublicKey
object (that is, an ASN.1 SEQUENCE with two INTEGERs, modulus and public exponent).
您仍然可以阅读它.尝试类似:
You can still read it in though. Try something like:
from Crypto.PublicKey import RSA
from Crypto.Util import asn1
from base64 import b64decode
key64 = 'MIGJAoGBAJNrHWRFgWLqgzSmLBq2G89exgi/Jk1NWhbFB9gHc9MLORmP3BOCJS9k
onzT/+Dk1hdZf00JGgZeuJGoXK9PX3CIKQKRQRHpi5e1vmOCrmHN5VMOxGO4d+znJDEbNHOD
ZR4HzsSdpQ9SGMSx7raJJedEIbr0IP6DgnWgiA7R1mUdAgMBAAE='
keyDER = b64decode(key64)
seq = asn1.DerSequence()
seq.decode(keyDER)
keyPub = RSA.construct( (seq[0], seq[1]) )
<小时>
从 2.6 版开始,PyCrypto 还可以导入 RsaPublicKey
ASN.1 对象.代码就简单多了:
Starting from version 2.6, PyCrypto can import also RsaPublicKey
ASN.1 objects.
The code is then much simpler:
from Crypto.PublicKey import RSA
from base64 import b64decode
key64 = b'MIGJAoGBAJNrHWRFgWLqgzSmLBq2G89exgi/Jk1NWhbFB9gHc9MLORmP3BOCJS9k
onzT/+Dk1hdZf00JGgZeuJGoXK9PX3CIKQKRQRHpi5e1vmOCrmHN5VMOxGO4d+znJDEbNHOD
ZR4HzsSdpQ9SGMSx7raJJedEIbr0IP6DgnWgiA7R1mUdAgMBAAE='
keyDER = b64decode(key64)
keyPub = RSA.importKey(keyDER)
这篇关于如何读取 PEM + PKCS#1 格式的 RSA 公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!