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()