我有以下提取主机叶证书和证书 RSA 公钥的代码:
c = ssock.getpeercert(True)
x509 = M2Crypto.X509.load_cert_der_string(c)
publickey=x509.get_pubkey()
m=publickey.get_modulus()
我试图找到提取 RSA key 的公共(public)指数的函数,但找不到任何函数。你能帮我弄清楚如何提取 RSA 公钥的公共(public)指数吗?
编辑:如果 M2Crypt 不可能。请指向任何其他方向。
编辑 2:当我尝试将证书加载为 DER 时:
key = RSA.importKey(publickey.as_der())
我遇到了这个错误:
('file() argument 1 must be encoded string without NULL bytes, not str',)
我想避免在本地系统中保存证书。我只想提取模数和指数等信息。 我从错误中了解到,importKey 函数将参数作为 DER 文件而不是字符串。你能帮我找到解决方法吗?
请您参考如下方法:
此代码应为您提供模数和公共(public)指数:
import Crypto.PublicKey.RSA
c = ssock.getpeercert(True)
x509 = M2Crypto.X509.load_cert_der_string(c)
publickey = x509.get_pubkey()
key = Crypto.PublicKey.RSA.importKey(publickey.as_der())
modulus = key.n
public_exponent = key.e
