DEV Community

Cover image for Create RSA key using openssl
Goffity Corleone
Goffity Corleone

Posted on

Create RSA key using openssl

The following command may be used with OpenSSL to create an RSA encryption key.

Step to create key to encrypt data with RSA algorithm

  1. Create private key
  2. Use private from (1) to create public key.

Generate private key using the following command:

openssl genrsa -out private.pem 4096
Enter fullscreen mode Exit fullscreen mode

In this command, i Have used a key length of 4096 bits. recommended for length is 2048.

This gives you a private key look like:

-----BEGIN RSA PRIVATE KEY-----
MIIJKAIBAAKCAgEAn0yZTRLxV2sbxzQfqeTCwwU9mpjTaL5fBnSsOLpFe5/hbD93
mofHpO0ybA596ofqMtxxl8xAmcKk4fdBZzBzwoDUqjBQqTQbU6hspGj9L4y1Hf1Y
TlqeSpyFJT3I3efmz6EFy7NjDvfLTmCC4kCwdhzh6PJkWMI+4mZ2g6rktHRBt4Tt
q3jTm74WtWjVCmozJefBI7fcyLpIuZrZ98YZE697nigjfU0GxaJEgiDtdmYrEbtZ
aPRK7ZusX/m5FQ97Iff1KccVYlDtRoBXaORACnhI2MVpjYq9DQFQOU5syATi+geu
2Yrazlb/yy/LTkUaNYNwHXA3YwjT3xU+m169CT5GVT4VV4EikEen/FZSqvZYqt1B
HU1X14wmjgcm0NTSeDf2vpnruAXDGP3KW5j0PU/nnlx2DuYtnPKztrH5JHmMvz6Y
Nvu7eI7l+yGz6tOw9P8jQqRf3DwzDuGxHVFKdqGczBTzPRjRYrtqDuHnZCtcwbxv
Lv8EpDlONsywQ5Jh5AOwrAyasd8xvJrd2cPGJyfyk2f9/Ye9T/AWm1j52mB1INX+
W+F2xU9abjJJ405SfHeMtIuBw+bB1KbFzgPSyintBzuhzGXX5BTybFYSYQoumAAA
189/dr1mgSJqwkoKFYg9cvTn66mLzxUSpiG4EDVpL7NnWXDgzQYYhRYXb4ECAwEA
AQKCAgEAntyX4Uorzn8tY5pynU5agHxgdCVGZ6W6REjkW2o/0sqd1wLwNvaDvdEi
wKovHk3O3EwFKFn0bqKzJfobilnCh81MyDCIsEUbn3FOb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z1
KKAuYUpmFVM3YWqujfoLjUvIpcioyt4qkBkGr6TcHDAI7J8jg91NufSW3XcLH/C6
oWGm3zRjjIk+jJKygqm1y4ile26IGEF1Ttw1R9nqgQoL4gxxEjhNqU1Vs9cJSJvx
QKWxwStHpMxYNu3F1MLYKhRbna6hCJotK35ZK9PV0e0EL/bC3CW2PTtD6qA=
-----END RSA PRIVATE KEY-----
Enter fullscreen mode Exit fullscreen mode

Generate public key from public

The private key from step (1) we can use it to generate public key following.

openssl rsa -in private.pem -puout -out public.pem
Enter fullscreen mode Exit fullscreen mode

This should give you a public key look like:

-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAn0yZTRLxV2sbxzQfqeTC
wwU9mpjTaL5fBnSsOLpFe5/hbD93mofHpO0ybA596ofqMtxxl8xAmcKk4fdBZzBz
woDUqjBQqTQbU6hspGj9L4y1Hf1YTlqeSpyFJT3I3efmz6EFy7NjDvfLTmCC4kCw
dhzh6PJkWMI+4mZ2g6rktHRBt4Ttq3jTm74WtWjVCmozJefBI7fcyLpIuZrZ98YZ
E697nigjfU0GxaJEgiDtdmYrEbtZaPRK7ZusX/m5FQ97Iff1KccVYlDtRoBXaORA
CnhI2MVpjYq9DQFQOU5syATi+geu2Yrazlb/yy/LTkUaNYNwHXA3YwjT3xU+m169
CT5GVT4VV4EikEen/FZSqvZYqt1BHU1X14wmjgcm0NTSeDf2vpnruAXDGP3KW5j0
PU/nnlx2DuYtnPKztrH5JHmMvz6YNvu7eI7l+yGz6tOw9P8jQqRf3DwzDuGxHVFK
dqGczBTzPRjRYrtqDuHnZCtcwbxvLv8EpDlONsywQ5Jh5AOwrAyasd8xvJrd2cPG
Jyfyk2f9/Ye9T/AWm1j52mB1INX+W+F2xU9abjJJ405SfHeMtIuBw+bB1KbFzgPS
yintBzuhzGXX5BTybFYSYQoumAAA189/dr1mgSJqwkoKFYg9cvTn66mLzxUSpiG4
EDVpL7NnWXDgzQYYhRYXb4ECAwEAAQ==
-----END PUBLIC KEY-----
Enter fullscreen mode Exit fullscreen mode

Reference:
https://www.scottbrady91.com/openssl/creating-rsa-keys-using-openssl

Top comments (0)