How to generate RSA public and private keys with OpenSSL

Before anything, I assume that you already have OpenSSL installed in your PC. In case you don’t and you are a Windows you can download it from this URLs:

I personally used the first one. After you are done with the installation follow this steps:

  1. Open a command line window and run the following command:
    openssl genrsa -out priv1024.pem 1024
    
    Your console output should look similar to this:
    E:\NodeJS>openssl genrsa -out priv1024.pem 1024
    
    Generating RSA private key, 1024 bit long modulus
    
    .......................++++++
    
    .........++++++
    
    e is 65537 (0x10001)
    
  2. Now that we have a 1024 bit private key in PEM format we have to extract the public key from it and save it to a different file. For that just run this command:
    openssl rsa -in priv1024.pem -outform PEM -pubout -out pub1024.pem
    
    Your console output should be similar to this:
    E:\NodeJS>openssl rsa -in priv1024.pem -outform PEM -pubout -out pub1024.pem
    
    writing RSA key
    

That’s it, now we have a RSA key pair in PEM format ready to be used. Depending on the key size specified, and the resources available on your computer, the generation of a key can take more time. Here is an example of how the files look:

E:\NodeJS>more priv1024.pem
-----BEGIN RSA PRIVATE KEY-----
MIICWgIBAAKBgQC/gvPLdlTe4uwrHzPfj/gTj6QghOL1gC0padG6Qp/totpWIluQ
hgqfR1/X9vNbNxx1N88e1S/i2wtfFxfBZAcayoVOJwsikGWuRb2Z+OZDP2R2Qegh
SE0VBL74RsMpcfSPP9MsJ66ZMEyEFFBAlSvEJgvnpbVQljFj0jgRNA6KTwIDAQAB
An8sIwIM4jitJZ9IUOijnYqO94vb8sRb0C6YXMr5ImG5Ys7vCvwPUtUf4FM2crOj
UhA885/Ie1mN7u2v/KJ875CzNdnf+1YYyuIBwl2fQuAS2vaEDkVqzT0T+4GA9x6j
DCbkOVbtju6zox2sqsr3xugzcDSPVA6UHc3Cm0xoL3j5AkEA5VD8z4F/mZ1/QbpV
LunE1QBqq/myAxvx/LbCw6nkaWBqxOx+PE+OV2Zivad8tDwnxYpjXbnDpnMv6rLU
Ff7qlQJBANXLz+XOuBmU/IA6BVKs8GXkmrDjtGC4KsATHcXRO6FcACMebUcYfYc/
5YizBtR2NxT7BCKYKGM8kZTTSAjjjFMCQD2BjW1yxHDFZ56Phc2BcgPXisT5wh9H
XY50zqSAav1AmSKcc/dlNz6F/bH0vGPzRo+bYK+A5dM9TfrbYDtOFkkCQQDUz5py
wxFIDaBFtvtjb1ef5S0B1LbQ66FKLf0d0obLuHs+eeQGt98DyNue9a1fwQhVLwPP
t6vEFB9JiAIVLwOXAkBneT454XNDIH2+4l5zyDAZCBWDR/mwlKpNv54ORdUZWlxK
5Fun+9bK5NFE5SHj/f8dkMosHPRWg2ZaK/Az7+zJ
-----END RSA PRIVATE KEY-----

E:\NodeJS>more pub1024.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/gvPLdlTe4uwrHzPfj/gTj6Qg
hOL1gC0padG6Qp/totpWIluQhgqfR1/X9vNbNxx1N88e1S/i2wtfFxfBZAcayoVO
JwsikGWuRb2Z+OZDP2R2QeghSE0VBL74RsMpcfSPP9MsJ66ZMEyEFFBAlSvEJgvn
pbVQljFj0jgRNA6KTwIDAQAB
-----END PUBLIC KEY-----

Just for fun I generated a 8192 bit key pair. It took around 10 seconds.

E:\NodeJS>more priv8192.pem
-----BEGIN RSA PRIVATE KEY-----
MIISKAIBAAKCBAEArWVyl6FmMZnUz3vzA4AlSF501WZ6Rs1qB8qmvtfF9ihxr5OM
IZhdJnAt7HMk8wn7ZWQwN2xZeUSOYnHuAC39LvajcsTIJwmluCSuzP33cB4P6uhz
SP6Wwf3aaHuuRUt6nw32iRwtSvCnRPI5L74E2gp2H19fM4Wd9IWwAAjesbN0vNqo
7HBUaYOumLQQDEOxKtiUrn/CwE7zABFEdCn4awTxhhm9CpIjBlcqBCUYMfoMB7SO
mOCWLbBkCHoome9WnkwcFcSLMSH+om2Uzjh+P+lOda+mZpLqcHEiwkPuRTT3UHRp
s9/yPL52mWcG1933DxD/MMlgQvS/oOHCFWNmIKp4U0g5pxJtgf0AKpx8Ao/ai4De
M2CwmnrrOUYTnw3roQ1yz3mI5AMfm5vAx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