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

倾然丶 夕夏残阳落幕 提交于 2019-12-03 16:21:01

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/28060159/how-to-extract-the-rsa-public-key-from-a-cer-and-store-it-in-a-pem-using-opens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!