RSA & SSH Key Generator · Tutorial
How to generate an RSA key pair for signing JWTs
7 min readintermediateUpdated
Your API should verify JWTs signed with RS256, so the service that issues tokens holds a private key and every API that checks them only needs the public key. This tutorial generates the key pair, shows where each half goes, and checks that a signed token verifies.
What you need
- The RSA & SSH Key Generator
- The JWT Generator and JWT Decoder for testing
- Access to the configuration of your token issuer and your API
Steps
Choose size and use
Select 3072 bits and Signing (RSASSA-PKCS1-v1_5). RS256 uses exactly this algorithm, so the key will work with any JWT library.
Generate the pair
Press Generate key pair. After a moment, both keys appear. The private key starts with
-----BEGIN PRIVATE KEY-----and the public key with-----BEGIN PUBLIC KEY-----.Save the private key securely
Press Download next to the private key, then store it in your secrets manager or as an environment variable for the token issuer. Never commit it to a repository.
Give the public key to your API
Copy the public key into your API's JWT configuration. Many libraries accept the PEM text directly. Public keys can be shared freely, so it's also fine to publish them, for example in a JWKS endpoint.
jwt.verify(token, publicKeyPem, { algorithms: ['RS256'] });Test end to end
In the JWT Generator, choose RS256, paste the private key and create a token. Then paste the token into the JWT Decoder, open Verify the signature, paste the public key and press Verify. A green badge confirms the pair works.
Try it yourself
Opens the RSA & SSH Key Generator.
Common mistakes
- Swapping the keys
- The private key signs and the public key verifies. If your API is given the private key, it works, but you've spread a secret to every service. Give APIs only the public key.
- Allowing any algorithm
- Configure your API to accept only
RS256. Accepting whatever algorithm the token header names has led to serious vulnerabilities. - Never rotating keys
- Plan for replacing keys, for example yearly, by supporting two public keys at once during the switch.
Next steps
Go back to the RSA & SSH Key Generator, or keep going with one of these:
FAQ
Can I convert the PEM key to JWK format?
Yes. Most JWT libraries can export a JWK from a PEM key, for example jose's exportJWK in JavaScript.