简体   繁体   English

PyOpenSSL将证书对象转换为.pem文件

[英]PyOpenSSL convert certificate object to .pem file

I want to send a certificate from a "certificate authority" to a node through sockets. 我想通过套接字将证书从“证书颁发机构”发送到节点。 I have a certificate created using this example https://skippylovesmalorie.wordpress.com/2010/02/12/how-to-generate-a-self-signed-certificate-using-pyopenssl/ How would I convert this into a .pem file so I can send it as a string through a socket and then convert it on the other end back into a .pem and use get_certificate to extract this certificate from it. 我有一个使用此示例创建的证书https://skippylovesmalorie.wordpress.com/2010/02/12/how-to-generate-a-self-signed-certificate-using-pyopenssl/如何将其转换为。 pem文件,所以我可以通过套接字将其作为字符串发送,然后在另一端将其转换回.pem并使用get_certificate从中提取此证书。 Python: reading a pkcs12 certificate with pyOpenSSL.crypto Its probably a hacky way to do it, but I want to simplify it for myself. Python:使用pyOpenSSL.crypto读取pkcs12证书它可能是一种hacky方式,但我想为自己简化它。 (or not) (或不)

I'm resurrecting the question of this person whos question was not answered How to convert PyOpenSSL object to PEM-encoded string? 我正在复活这个问题没有回答的问题如何将PyOpenSSL对象转换为PEM编码的字符串?

This is for generating a certificate signing request, but the concept should be the same 这用于生成证书签名请求,但概念应该相同

from OpenSSL import crypto

req = crypto.X509Req()
pkey = crypto.PKey()
pkey.generate_key(crypto.TYPE_RSA, 2048)
req.set_pubkey(pkey)
req.sign(pkey, 'sha1')
certreq = crypto.dump_certificate_request(crypto.FILETYPE_PEM, req)
certreq = certreq.replace('-----BEGIN CERTIFICATE REQUEST-----\n', '').replace('-----END CERTIFICATE REQUEST-----\n', '')
private_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey)

for a certificate you can use: 您可以使用的证书:

crypto.dump_certificate(type, cert)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM