What an RSA key pair is
RSA is a public-key algorithm. A key pair has a private key, which only its owner holds, and a public key, which anyone can have. What the private key signs, the public key verifies. What the public key encrypts, only the private key decrypts.
In identity systems RSA is mostly used for signatures. An identity provider signs tokens and SAML assertions with its private key, and every application checks them with the public key. Because verifiers need only the public key, many services can trust one issuer without sharing a secret.
This tool generates RSA key pairs in your browser with Web Crypto and gives you the keys in the formats other software reads: PEM in PKCS#8 or PKCS#1, and JWK.
Key sizes and security strength
RSA strength grows slowly with key size. NIST SP 800-57 maps RSA moduli to comparable symmetric strength:
| RSA modulus | Security strength | Comparable EC curve |
|---|---|---|
| 2048 bits | 112 bits | P-224 |
| 3072 bits | 128 bits | P-256 |
| 7680 bits | 192 bits | P-384 |
| 15360 bits | 256 bits | P-521 |
2048 bits is the accepted minimum today and what most identity providers use for token signing. 3072 bits gives a longer margin. 4096 bits is common for long-lived keys, though it makes signing slower for a modest gain.
Generation time grows quickly with size. A 2048-bit key takes a moment in a browser; a 4096-bit key can take several seconds.
The formats this tool produces
The same key can be written in several containers. They hold the same numbers; only the wrapping differs.
| Format | PEM label | Holds | Defined in |
|---|---|---|---|
| PKCS#8 | PRIVATE KEY | Any private key, with its algorithm named | RFC 5208 and RFC 5958 |
| SubjectPublicKeyInfo (SPKI) | PUBLIC KEY | Any public key, with its algorithm named | RFC 5280 |
| PKCS#1 private | RSA PRIVATE KEY | An RSA private key only | RFC 8017 |
| PKCS#1 public | RSA PUBLIC KEY | An RSA public key only | RFC 8017 |
| JWK | (JSON) | Key parameters as Base64url fields | RFC 7517 and RFC 7518 |
PKCS#8 and SPKI are the modern defaults that most libraries and tools read. PKCS#1 is the older RSA-specific form, which some software, including older OpenSSL-based tools, still expects.
The JWK and its kid
The public JWK has kty RSA, the modulus n and the exponent e, plus alg and use from your choice of algorithm. The kid is the RFC 7638 thumbprint: a SHA-256 hash of the key’s required members, so the same key always gets the same id.
{
"kid": "<RFC 7638 thumbprint>",
"use": "sig",
"alg": "RS256",
"kty": "RSA",
"n": "<modulus, Base64url>",
"e": "AQAB"
}AQAB is 65537, the public exponent nearly every RSA key uses.
RS256, PS256 and RSA-OAEP-256
The algorithm choice sets the JWK’s alg and use and the Web Crypto algorithm the key is created for.
- RS256: RSASSA-PKCS1-v1_5 signatures with SHA-256. The most widely supported JWT algorithm and Keycloak’s default for tokens.
- PS256: RSA-PSS signatures with SHA-256. A more modern padding with a stronger security proof, supported by Keycloak and many libraries, though not all.
- RSA-OAEP-256: encryption with OAEP padding and SHA-256, used for encrypted tokens (JWE) and encrypted SAML assertions.
Keep a key to one purpose. Use separate keys for signing and for encryption, which is also how Keycloak organizes realm keys.
How to generate a key pair
- Choose the key size. 2048 bits suits token signing; choose 3072 for a longer margin.
- Choose the algorithm the key is for.
- Choose PKCS#8 and SPKI unless the receiving software asks for PKCS#1.
- Copy the private key into a secrets manager or the target system’s key store.
- Copy the public key or the public JWK to wherever verifiers fetch it.
The private key appears on screen. Generate production keys on a trusted device, copy them straight to their destination and close the tab.
RSA keys in Keycloak
Every Keycloak realm has its own keys, listed under Realm settings, Keys. By default Keycloak generates them itself with the rsa-generated provider for signing and rsa-enc-generated for encryption. You rarely need to supply your own.
Bringing your own key
Add an rsa key provider under the Providers tab and paste the private key in PEM form, with the matching certificate if you have one. Keycloak generates a self-signed certificate if you do not supply one. Reasons to bring a key include moving a realm between servers without changing keys, or a policy that keys come from a central process.
Rotating keys without breaking sessions
Keycloak keys have a priority and can be active, passive or disabled. Active keys with the highest priority sign new tokens. Passive keys no longer sign but still verify. To rotate, add a new key with a higher priority, then make the old key passive. Delete the old key once every token and session signed with it has expired.
Applications that fetch keys from the realm’s JWKS endpoint, /realms/<realm>/protocol/openid-connect/certs, pick up the new key by its kid automatically.
Client keys
Clients that authenticate with a signed JWT (private_key_jwt) hold their own key pair. The client keeps the private key and Keycloak needs the public key, either pasted in the client’s Keys tab or fetched from a JWKS URL the client publishes.
Common mistakes
- Sharing the private key with every service that verifies tokens. Verifiers need only the public key.
- Mixing up PEM labels: giving software a PKCS#1
RSA PRIVATE KEYwhen it expects PKCS#8, or the reverse. Convert with this tool’s format choice or withopenssl pkey. - Pinning a single key in applications instead of fetching the JWKS, which breaks every application on rotation.
- Using keys under 2048 bits, which current guidance no longer accepts.
- Storing private keys in source control or container images.
Security notes
Keys made here come from the browser’s Web Crypto implementation, the same code browsers use for their own cryptography. The key is created in memory and never sent anywhere.
Still, the most secure private key is one that never leaves the system that uses it. Where you can, let the identity provider generate its own keys, or create keys in a hardware security module or cloud key service.
NIST’s current guidance treats 112-bit strength, which 2048-bit RSA provides, as acceptable for now and plans to retire it in the coming years. New long-lived keys are a reasonable place to start using 3072 bits.
Checking an RSA key with OpenSSL
A few commands confirm that a key is what you think it is before it goes into production.
# Check the private key's internal consistency
openssl rsa -in private.pem -check -noout
# Print the size and parameters
openssl pkey -in private.pem -noout -text | head -1
# Confirm a public key matches the private key
openssl pkey -in private.pem -pubout | diff - public.pem && echo match
# Convert PKCS#8 to PKCS#1 and back
openssl pkey -in private.pem -traditional -out private-pkcs1.pem
openssl pkey -in private-pkcs1.pem -out private-pkcs8.pemIf a system rejects a key, the PEM label is the first thing to check: BEGIN PRIVATE KEY is PKCS#8 and BEGIN RSA PRIVATE KEY is PKCS#1. The bytes inside differ, so renaming the label alone does not convert a key.