I am trying to generate RSA 1024 key pair (public/private) using the following command

openssl genrsa -des3 -out server.key 1024

In the server.key file, only RSA private block is there, so where does the public key go ?


2 Answers 2


The RSA private key format includes all the public elements. When you get the private key you really have both the private and public key.

This is described in PKCS#1 (the leading RSA standard); private key format is an encoded ASN.1 structure which contains:

  RSAPrivateKey ::= SEQUENCE {
      version           Version,
      modulus           INTEGER,  -- n
      publicExponent    INTEGER,  -- e
      privateExponent   INTEGER,  -- d
      prime1            INTEGER,  -- p
      prime2            INTEGER,  -- q
      exponent1         INTEGER,  -- d mod (p-1)
      exponent2         INTEGER,  -- d mod (q-1)
      coefficient       INTEGER,  -- (inverse of q) mod p
      otherPrimeInfos   OtherPrimeInfos OPTIONAL

The public key really consists in the modulus and public exponent, which are both in the private key structure, as shown above.

Now your question might be: "what if I want the public key 'alone' ?"

OpenSSL lives in and by the X.509 world. In X.509, public key don't exist as stand-alone values; and there is no standard file format for lone public keys. An application is supposed to use public keys that it found in certificates. This is quite apparent in the SSL/TLS protocol (which is the primary support goal of OpenSSL): when the client sends its public key, it sends it as a certificate. Therefore, if you want the public key in a file, arrange for it to be stored in a certificates (possibly self-signed). The OpenSSL req sub-command, with the -x509 flag, can help.

Of course, every rule has exceptions, so there is also a method to get the public key "only" from the private key; this is what @uwotm8 shows in his answer. However, the resulting public key, which will look like this:

-----END PUBLIC KEY-----

is not really usable anywhere, because this is a non-standard format.

(Technically this is a SubjectPublicKeyInfo element as it appears in an X.509 certificate, but it is not supposed to appear outside of an X.509 certificate.)

  • 1
    It's at least compatible with X509EncodedKeySpec and the "RSA" KeyFactory in Java. Don't know about others. After PEM decoding, e.g. using Bouncy Castle, of course. Jun 22, 2014 at 18:50

The public key needs to be derived from the private key. Try this command:

openssl rsa -in server.key -pubout

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .