【问题标题】:extracting public key from certificate and encrypting data从证书中提取公钥并加密数据
【发布时间】:2011-08-12 22:48:12
【问题描述】:

这是家庭作业! 我使用get_peer_certificate() 获取服务器证书 并调用dump_certificate 将证书转储到变量中。格式是 PEM,我觉得很合适。

-----BEGIN CERTIFICATE-----
GIBBERISH................
......................
........................

-----END CERTIFICATE-----

如何从该文件 ('server.pubkey') 中提取服务器的公钥并使用 RSA 算法和任何 python 库加密 plaintext。在撰写本文时,我正在使用 pyOpenSSL

【问题讨论】:

    标签: python network-programming rsa pyopenssl


    【解决方案1】:

    我建议使用更广泛的crypto library such as M2Crypto,它具有 X509 证书功能以及 RSA 加密:

    from M2Crypto import RSA, X509
    data = ssl_sock.getpeercert(1)
    # load the certificate into M2Crypto to manipulate it
    cert = X509.load_cert_string(data, X509.FORMAT_DER)
    pub_key = cert.get_pubkey()
    rsa_key = pub_key.get_rsa()
    cipher = rsa_key.public_encrypt('plaintext', RSA.pkcs1_padding)
    

    【讨论】:

      【解决方案2】:
          from OpenSSL import crypto        
          crtObj = crypto.load_certificate(crypto.FILETYPE_ASN1, config.x509_certificate)
          pubKeyObject = crtObj.get_pubkey()
          pubKeyString = crypto.dump_publickey(crypto.FILETYPE_PEM, pubKeyObject)
      

      【讨论】:

      • 这个使用什么库?
      • @StudySmarterNotHarder,它来自 OpenSSL。更新的答案。
      猜你喜欢
      • 2014-06-23
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      相关资源
      最近更新 更多