Extract PEM Public Key from X.509 Certificate
Asked Answered
P

1

13

I've created what I believe is a certificate containing a Public Key DER file, but I need the Public Key in PEM format now for a different platform. The aim is to use the same public key.

I created it using RSA Encryption in iOS and Decrypt It Using PHP:

openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem -days 3650

I have an existing public key in use (public_key.der) and can't change it. However I now need a PEM version of the public key

public_key.pem

How can I convert from DER to PEM in this way?

Note: If I had created my keypair using the following method, things would be easy. I could extract a public key PEM file:

openssl genrsa -out rsa.pem 1024 
openssl rsa -in rsa.pem -pubout

Public PEM files generated this way work. Is it possible that what I've created eariler on (with the -x590 command) are entirely different creatures to the output of the rsa commands?

Petrarch answered 14/3, 2015 at 13:5 Comment(1)
Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask. Also see Where do I post questions about Dev Ops?Odontoid
S
27

Assuming you've created certificate in DER format with the command

openssl req -x509 -out certificate.der -outform der -new -newkey rsa:1024 -keyout private_key.pem -days 3650

Then extracting public key in PEM format can be done with a command

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

-inform defines certificate format (default is PEM) and -noout suppresses output except of requested -pubkey.

The same operation with certificate in PEM format:

openssl x509 -in certificate.pem -pubkey -noout > public_key.pem
Sheepshearing answered 14/3, 2015 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.