If I use open ssl command
sudo openssl genrsa -out privkey.pem 2048
to generate rsa keys, it generates only 1 file. which is private key. How do I get the public key.
To answer your question: The file you generate with sudo openssl genrsa -out privkey.pem 2048
contains both the private and the public key.
openssl genrsa
generates a key pair. The public key is the modulus (n) and the public exponent (e):
$ openssl genrsa 2048 > dummy.key
$ openssl rsa -in dummy.key -noout -text
Private-Key: (2048 bit)
modulus:
00:d4:4a:3c:8c:41:b3:9b:a3:26:2d:4b:8c:62:08:
c0:fc:ad:6c:5e:5f:3f:28:e6:a3:7c:3d:43:5e:98:
[...]
publicExponent: 65537 (0x10001)
privateExponent:
00:c7:6e:f6:72:cd:46:6e:70:56:ed:36:8c:2b:8d:
0d:c0:53:2b:fb:7c:7f:59:6d:53:28:e4:64:e5:f9:
d6:84:64:7e:e4:be:20:64:7d:5b:50:06:ba:1f:df:
[...]
You can extract only the public key using the -pubout option to openssl rsa:
$ openssl rsa -in dummy.key -pubout
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1Eo8jEGzm6MmLUuMYgjA
[...]
gW6qhadXVsKWQhEhVU9s7V9vJOhfeSSahvuZR0zjursTm6PSJWo7lKNgFCFC0NXO
LQIDAQAB
-----END PUBLIC KEY-----
If you extract that data into a file and re-read it, you will see it contains the modulus and exponent:
$ openssl rsa -in dummy.key -pubout > public.key
$ openssl rsa -in public.key -pubin -noout -text
Public-Key: (2048 bit)
Modulus:
00:d4:4a:3c:8c:41:b3:9b:a3:26:2d:4b:8c:62:08:
c0:fc:ad:6c:5e:5f:3f:28:e6:a3:7c:3d:43:5e:98:
[...]
6f:24:e8:5f:79:24:9a:86:fb:99:47:4c:e3:ba:bb:
13:9b:a3:d2:25:6a:3b:94:a3:60:14:21:42:d0:d5:
ce:2d
Exponent: 65537 (0x10001)
$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With