python批量传公钥_Python:如何从“ .cert”文件中提取公钥?

I have used openssl to generate a X.509 self-signed like so:

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

This generated two files: cert.pem and a key.pem files.

My cert.pem file contains the public key. How do I extract it using Python?

I am unable to use the OpenSSl library of python. I am able to use the cryptography library.

My current code:

cert = x509.load_pem_x509_certificate(pem_data, default_backend())

print(cert.public_key)

解决方案

Unless I'm misunderstanding your question, you'd do it just the same as you would if you were reading any other file's contents with python.

Something like this should get you started:

#!/usr/bin/env python

from subprocess import Popen, PIPE, STDOUT

p = Popen(["openssl", "rsa", "-in", "key.pem", "-pubout", "-out", "key.pub"], stdout=PIPE, stdin=PIPE, stderr=STDOUT)

p.stdout.readline().rstrip()

pubkey_file=open('key.pub', 'r')

for line in pubkey_file.readlines():

data=line.strip('\n')

print(data)

pubkey_file.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值