【问题标题】:Extract public-key modulus from PrivateKey/CertificateRequest/Certificate in Python , with the same format as OpenSSL在 Python 中从 PrivateKey/CertificateRequest/Certificate 中提取公钥模数,格式与 OpenSSL 相同
【发布时间】:2020-06-18 15:23:15
【问题描述】:

使用 OpenSSL,我可以使用以下命令从各种对象中提取公钥的模数:

openssl rsa -noout -modulus -in {KEY}
openssl req -noout -modulus -in {CSR}
openssl x509 -noout -modulus -in {CERT}

我正在尝试使用cryptographypyopenssl 包在python 中复制它。

我可以从 Python 中的所有这些对象中提取公钥,但我无法弄清楚如何对模数进行编码以匹配 OpenSSL 命令行输出——这似乎是我可以使用的格式的 base64 编码版本不要从 3 个项目中的任何一个的文档或源代码中找出答案。

【问题讨论】:

    标签: python openssl pycrypto


    【解决方案1】:

    密码学中的 RSAPublicNumbers 类 (link) 拥有您正在寻找的东西。您可以使用 pyopenssl 中PKey 类的to_cryptography_key 方法(link)来获取它。

    from OpenSSL.crypto import load_certificate
    from OpenSSL.crypto import FILETYPE_PEM
    
    with open(certfile, 'rb') as fp:
        cert = load_certificate(FILETYPE_PEM, fp.read())
    
    # This gives you the modulus in integer form
    modn = cert.get_pubkey().to_cryptography_key().public_numbers().n
    
    # Convert it to hex
    print('{:X}'.format(modn))
    
    

    【讨论】:

    • 非常感谢!我自己已经达到了cert.get_pubkey().to_cryptography_key().public_numbers()...但是.n 的十六进制格式是今天早上让我在烤箱里躲了一个小时的缺失部分!
    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多