How to extract public key and private key from .key file?

17,016

the .key file corresponds to the private key itself. If you open your .key file in a text editor you would see that they have -----BEGIN RSA PRIVATE KEY----- as the prefix and -----END RSA PRIVATE KEY----- as the suffix. See below for example:

-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAr/8dgslQvZLyDnPnBVJbihYjhPw6hUuCH1tKop5RitQuob4r
i4ixAhNyrjB3dmO39o8cG56/kb1MHszt256476LGW20Q3s902Nckc4yYqaA02XuE
...
...
rG8D6oIoim5XwtS68fHJpfvciuuE/TQcnS4Ek04jkReP1EXv2hp4CTAZ4l5Tm4HJ
oDmCiSOsQE/YjiyQp5eGFadqosOZZFU7k+8ipKIstr71kEQRiLE=
-----END RSA PRIVATE KEY-----

The command that you shared generates a public key pair from the existing private key.

openssl rsa -in server.key -pubout -out key.pub

HTH

Share:
17,016
Sato
Author by

Sato

Updated on June 04, 2022

Comments

  • Sato
    Sato almost 2 years

    I have a file server.key generated by openssl genrsa -out server.key 2048, which contains both public and private key.

    I searched and fount that I can get public key by openssl rsa -in server.key -pubout -out key.pub

    How can I get private key from server.key?

  • T.Nel
    T.Nel about 5 years
    I still don't get that. On every tutorial we see how to generate the "key pair" and then how to extract the public key from this key pair. So isn't the public key also in that file? I understood it was just a copy since you have to generate public and private key at the same time in RSA?
  • PrafulPravin
    PrafulPravin almost 3 years
    Public key is the .crt file which you get from the SSL provider.