2011-04-26 76 views
2

這是作業作業! 我使用get_peer_certificate() 和調用dump_certificate將服務器的證書轉儲到變量中。格式是PEM,看起來很適合我。從證書中提取公鑰並加密數據

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

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

我如何提取該文件(「server.pubkey」)的服務器的公鑰和使用RSA算法和任何Python庫加密plaintext。在寫這個的時候,我使用pyOpenSSL

回答

5

我推薦使用它具有X509證書的功能更加廣闊的crypto library such as M2Crypto以及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)