Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the RSA public key from a .cer and store it in a .pem using OpenSSL?

I have the requirement to extract the public key (RSA) from a *.cer file. I wish to extract the key and store it in a .pem file so I can use its value to encrypt values using jsencrypt.

The following command converts a .cer to .pem:

openssl x509 -inform der -in certificate.cer -out certificate.pem 

Yet it doesn't generate a file with the public key but a file with the contents of the *.cer file.

-----BEGIN CERTIFICATE----- MIICPDCCAamgAwIBAg............ *lots of extra contents* -----END CERTIFICATE----- 

What command should I use to extract the public key and store it in a .pem file?

like image 819
Steven Anderson Avatar asked Jan 21 '15 05:01

Steven Anderson


People also ask

Can we convert CER to PEM?

cer, and . key. They are Base64 encoded ASCII files and contain "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" statements. Server certificates, intermediate certificates, and private keys can all be put into the PEM format.

Can we extract public key from certificate?

Click Security > Certificates. On the Certificates page, click the certificate. On the Certificate Details page, click Export Private/Public Keypair.


1 Answers

Using this command I was able to generate the .pem with the contents of the public key.

openssl x509 -inform der -in certificate.cer -pubkey -noout > certificate_publickey.pem 

Which produces:

-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsM+whXrxmbCkPfkwY2EehYpIp *blah blah blah blah* -----END PUBLIC KEY----- 
like image 86
Steven Anderson Avatar answered Sep 19 '22 14:09

Steven Anderson