编程语言
首页 > 编程语言> > python – 从证书中提取公钥并加密数据

python – 从证书中提取公钥并加密数据

作者:互联网

这是一个家庭作业!
我使用get_peer_certificate()获取服务器的证书
以及调用dump_certificate以将证书转储到变量中.格式是PEM,看起来对我来说.

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

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

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

解决方法:

我建议使用更广泛的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)

标签:python,network-programming,rsa,pyopenssl
来源: https://codeday.me/bug/20190726/1543248.html