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?

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
legoblocks
  • 525
  • 1
  • 6
  • 16
  • 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](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Sep 22 '16 at 14:00

1 Answers1

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
divanov
  • 6,173
  • 3
  • 32
  • 51