0

I generated an RSA Private Key using openssl genrsa -out mykey.pem 2048

To extract the public key I have found 2 commands, one using openssl :

openssl rsa -in mykey.pem -pubout > mykey.pub

And the other using ssh-keygen:

ssh-keygen -y -f mykey.pem > mykey.pub

However I noticed that boths outputs doesn't look anything alike. I know that a private key can only have one unique public key so why the outputs are different?

1 Answer 1

5

Because they're in different formats.

You can compare this to an image file, roughly. The same pixels, with the same values, can be stored in multiple different ways, depending on the image format. A 1x1 image made of 1 black pixel will look radically different in a hex editor, depending on whether you save it as PNG, JPEG or TIFF.

Likewise, OpenSSL and OpenSSH store public keys in two completely different formats, even if the underlying cryptographic primitives (the public exponent and the modulus) are the same.

PEM Format

OpenSSL uses the PEM format by default. This can be easily identified by -----BEGIN PUBLIC KEY-----. If you use the command openssl -in mykey.pub -pubin -text, you can see the following output:

RSA Public-Key: (2048 bit)
Modulus:
    00:c8:5e:9f:52:16:a0:77:ae:dc:20:14:25:95:9e:
    5c:0e:08:ff:1c:35:27:9c:dd:62:a3:58:1a:39:5b:
    65:67:19:2b:5d:05:d7:05:1d:dd:0a:43:eb:b8:35:
    f2:85:c9:1d:32:66:22:4a:08:ef:04:10:a7:f3:52:
    ef:62:4b:26:81:f4:ab:00:c3:7e:16:29:5e:14:7f:
    e5:f8:3f:2c:62:de:61:ba:6d:d4:4b:d2:13:3c:46:
    c4:22:6d:19:9a:6f:06:3f:96:1e:79:58:09:d1:f3:
    3c:e4:24:b3:6f:5a:fc:01:5e:ff:fb:25:fe:ea:30:
    45:87:57:2c:68:ac:3d:55:c7:bb:d6:23:0b:40:68:
    21:bb:97:1c:5e:66:08:49:eb:a0:a5:e4:24:8f:16:
    08:55:8e:0a:e5:72:59:4c:bf:a1:f1:94:ed:b3:84:
    66:94:32:87:c4:fc:d4:32:35:05:03:3a:b1:50:26:
    ce:41:21:7b:a1:e3:d5:5e:65:f3:1e:81:fd:65:db:
    1b:f9:21:cd:e4:b8:2c:5d:53:63:88:76:b4:c7:34:
    50:c1:89:2f:68:1b:5f:d2:9c:31:47:43:3c:30:3c:
    86:94:da:32:f6:0f:30:2c:f4:2a:64:8c:15:aa:0e:
    6d:f1:33:14:d5:b3:4c:95:08:81:cd:43:02:ac:fe:
    f4:21
Exponent: 65537 (0x10001)
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyF6fUhagd67cIBQllZ5c
Dgj/HDUnnN1io1gaOVtlZxkrXQXXBR3dCkPruDXyhckdMmYiSgjvBBCn81LvYksm
gfSrAMN+FileFH/l+D8sYt5hum3US9ITPEbEIm0Zmm8GP5YeeVgJ0fM85CSzb1r8
AV7/+yX+6jBFh1csaKw9Vce71iMLQGghu5ccXmYISeugpeQkjxYIVY4K5XJZTL+h
8ZTts4RmlDKHxPzUMjUFAzqxUCbOQSF7oePVXmXzHoH9Zdsb+SHN5LgsXVNjiHa0
xzRQwYkvaBtf0pwxR0M8MDyGlNoy9g8wLPQqZIwVqg5t8TMU1bNMlQiBzUMCrP70
IQIDAQAB
-----END PUBLIC KEY-----

That's all the information contained in the public key. The PEM format is simply base64-encoded DER format.

DER Format

A standardized format for storing cryptographic material, which uses the ASN.1 notation. OpenSSL also supports it, and DER files are smaller than PEM files. The disadvantage is, since they're binary, you can't easily copy and paste them as text, which is why PEM is generally preferred.

SSH Format

OpenSSH uses the "SSH Format" by default, which is described in RFC 4253, section 6.6. It's a rather simple format, and it works. Since the underlying cryptographic primitives are the same, you can easily convert a key from one format to another.

2
  • 1
    The OpenSSL-and-some-others format is codified by RFC7468, and ssh-keygen -e -m pkcs8 [-f file] outputs it; why OpenSSH people decided to call this pkcs8 I have no idea, it isn't anywhere in PKCS8 Jan 24, 2021 at 2:36
  • @dave_thompson_085 Almost like non-crypto folks shouldn't be let anywhere near crypto when coding :D
    – user163495
    Jan 24, 2021 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.