How to extract RSA public key from .cer and save it to .pem using OpenSSL?

I have a requirement to extract the public key (RSA) from a *.cer file. I want to extract a key and save it in a .pem file .pem that I can use its value to encrypt values ​​with jsencrypt .

The following command converts .cer to .pem :

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

However, it does not create 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----- 

Which command should be used to extract the public key and save it in a .pem file?

+6
source share
1 answer

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

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

What produces:

 -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsM+whXrxmbCkPfkwY2EehYpIp *blah blah blah blah* -----END PUBLIC KEY----- 
+17
source

Source: https://habr.com/ru/post/981329/


All Articles