Python: How do I extract public key from a '.cert' file?

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)


#python

4 Likes54.35 GEEK