Openssl RSA - Private and Public Keys

If I use the open ssl command

sudo openssl genrsa -out privkey.pem 2048 

to generate rsa keys, it generates only 1 file. which is the private key. How to get a public key.

+6
source share
1 answer

To answer your question: the file that you create with sudo openssl genrsa -out privkey.pem 2048 contains both a private and a public key.

openssl genrsa generates a key pair . The public key is a module (n) and a public indicator (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 only extract the public key using the - pubout option for openssl rsa :

 $ openssl rsa -in dummy.key -pubout writing RSA key -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1Eo8jEGzm6MmLUuMYgjA [...] gW6qhadXVsKWQhEhVU9s7V9vJOhfeSSahvuZR0zjursTm6PSJWo7lKNgFCFC0NXO LQIDAQAB -----END PUBLIC KEY----- 

If you extract this data into a file and re-read it, you will see that it contains a module and a metric:

 $ 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) $ 
+14
source

Source: https://habr.com/ru/post/926602/


All Articles