JWT

How to Generate JWT Private and Public Keys (Windows & Linux)

You can quickly generate private and public keys for your JWT using ssh-keygen which is available in Windows (Powershell) and Linux. So the command below will work on both OSes.

Private Key Generation

First, let’s create a private key in the PEM format with the following command

ssh-keygen -t rsa -b 4096 -m PEM -f private.key

Now type this command to view the private key contents

cat private.key

Copy the output into your .env file or wherever you will save the private key for JWT singing.

Public Key Generation

Now to generate the public key from the private key we created, execute this command in the same directory.

ssh-keygen -f private.key -e -m PKCS8 > public.key

Print the content of the public key using the following command.

cat public.key

Copy it somewhere safe where you will be using it for your JWT.

Clean up

Now that you copied both of them to a secure place. It is very recommended you delete the private.key from your computer. You can do so using this command in the same directory.

rm private.key

Conclusion

In this tutorial, you learned how to create a private and public key to be used with your JWT. However, that is not limited to JWT, using the same method you can generate public and private keys to be used anywhere the PEM format is supported.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top