Why convert a JWK to PEM
Identity providers publish keys as JSON Web Keys. Much other software wants PEM: web servers, API gateways, reverse proxies, OpenSSL, and many JWT libraries that take a public key as a string starting with -----BEGIN PUBLIC KEY-----.
Both formats hold the same numbers. A JWK lists them as named JSON members; PEM wraps a binary ASN.1 structure in Base64 between header lines. Converting is lossless for the key itself, but JWK extras such as kid, use and alg have no place in PEM and are dropped.
How the two formats relate
| Key | JWK members | PEM form |
|---|---|---|
| RSA public | n, e | BEGIN PUBLIC KEY (SPKI) or BEGIN RSA PUBLIC KEY (PKCS #1) |
| RSA private | n, e, d, p, q, dp, dq, qi | BEGIN PRIVATE KEY (PKCS #8) or BEGIN RSA PRIVATE KEY (PKCS #1) |
| EC public | crv, x, y | BEGIN PUBLIC KEY (SPKI) |
| EC private | crv, x, y, d | BEGIN PRIVATE KEY (PKCS #8) |
| Ed25519 | crv, x (and d) | BEGIN PUBLIC KEY or BEGIN PRIVATE KEY |
SPKI (SubjectPublicKeyInfo) names the algorithm inside the structure, so one format fits every key type. PKCS #1 is older and holds only RSA keys, without naming the algorithm.
What the conversion does
JWK numbers are Base64url-encoded big-endian integers. The converter decodes them, places them in the ASN.1 structure for the key type, encodes that structure as DER bytes, and writes the bytes as Base64 in 64-character lines between the PEM header and footer.
JWK { "kty": "RSA", "n": "6SoS…", "e": "AQAB" }
↓ decode n and e, build SubjectPublicKeyInfo, DER-encode
PEM -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6SoS…
-----END PUBLIC KEY-----What this converter does
- Accepts one JWK or a whole JWKS; with a set, you pick the key from a list showing each
kid, type and algorithm. - Converts RSA, EC (P-256, P-384, P-521) and Ed25519 keys.
- Outputs the public key in SPKI form, and for RSA also in PKCS #1 form.
- For a private JWK, also outputs the private key as PKCS #8, and for RSA as PKCS #1.
- If the key has a certificate in
x5c, outputs it as a PEM certificate too.
The work is done by your browser’s Web Crypto, which also checks that the numbers form a valid key. Symmetric oct keys have no PEM form and are refused.
How to convert a key
- Paste a JWK, or paste a JWKS such as your realm’s certs JSON.
- If you pasted a set, choose the key by its
kid. - For RSA keys, choose SPKI and PKCS #8, or PKCS #1.
- Copy the PEM block you need.
Getting a Keycloak realm’s key as PEM
There are two ways. The admin console shows each RSA key under Realm settings, Keys, with a button for the public key and one for the certificate. The public key there is the Base64 body without the header lines, so you add -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY----- yourself.
The other way needs no admin access: open https://<host>/realms/<realm>/protocol/openid-connect/certs, paste the JSON here, pick the signing key by its kid, and copy the PEM. The certificate from x5c comes out as PEM too.
Pick the key with use: sig for verifying tokens. The enc key in the same set is for encryption.
Use cases
Configuring a gateway or proxy
Some gateways and proxy plugins validate JWTs with a fixed PEM public key rather than a JWKS URL. Convert the realm’s signing key and paste the PEM into the configuration. Remember to update it when the realm rotates keys.
Using OpenSSL
With a PEM file you can inspect a key with openssl pkey -pubin -in key.pem -text -noout, or verify a signature from the command line.
Libraries that only take PEM
Older JWT libraries and some language runtimes load keys only from PEM. Converting lets you test them against the same key your identity provider publishes.
Common errors
| Message | Cause |
|---|---|
| An RSA JWK needs n and e | The JSON is not a complete RSA key, or a member name is misspelled |
| An EC JWK needs crv P-256, P-384 or P-521 | Another curve, such as secp256k1, which Web Crypto does not support |
| A symmetric (oct) key has no PEM form | The key is an HMAC or AES secret; use its k value directly |
| The private key could not be read | An RSA private JWK without p, q, dp, dq and qi |
| Expected a JWKS or a single JWK | The JSON has neither keys nor kty |
Doing the same with command-line tools
If you prefer to stay in a terminal, OpenSSL can work with the PEM output, and a few lines of code can produce it from a JWK.
# inspect the PEM public key you copied
openssl pkey -pubin -in realm-key.pem -text -noout
# Node.js: JWK to PEM with the built-in crypto module
node -e "const c=require('crypto');
const k=c.createPublicKey({key: JSON.parse(process.argv[1]), format:'jwk'});
console.log(k.export({type:'spki', format:'pem'}))" "$(cat key.json)"The result should match this tool byte for byte, because SPKI and PKCS #8 encodings of a given key are fixed.
After a Keycloak key rotation
A fixed PEM key stops working when the realm starts signing with a new key. Plan for it.
- Watch for a new
kidin the realm’s certs endpoint, or rotate on a schedule you control. - Convert the new signing key to PEM here.
- Add it to the software that verifies tokens, next to the old key if it supports more than one.
- Make the new key active in Keycloak.
- Remove the old PEM key once tokens signed with it have expired.
Software that reads the jwks_uri directly handles all of this by itself, which is the main reason to prefer it over fixed PEM keys.
Reading the PEM output
A PEM block is easy to sanity-check by eye.
- The first and last lines name the format, such as
BEGIN PUBLIC KEY; both must match. - The body is standard Base64 in lines of 64 characters, with the last line shorter.
- An RSA 2048-bit public key in SPKI form is about 450 characters of Base64; an EC P-256 key is much shorter.
- Any change to the body, even one character, makes the key unreadable.
If a library rejects the PEM, check which form it expects. Some older RSA libraries accept only BEGIN RSA PUBLIC KEY; most modern ones prefer BEGIN PUBLIC KEY. Switch the format here and try again.
Security notes and limits
Public keys are safe to convert anywhere. Private keys deserve more care: the conversion runs only in your browser and nothing is sent to us, but a private key in a clipboard or a chat window is easy to leak. Prefer converting private keys with local tools such as OpenSSL.
A PEM key carries no kid, use or alg. If the software you configure needs to know the algorithm, set it there.
Best practices
- Prefer a JWKS URL over a fixed PEM key where the software supports it, so key rotation needs no manual step.
- When you must use PEM, note the
kidnext to it, so you know which key it is after a rotation. - Use SPKI (
BEGIN PUBLIC KEY) unless a tool asks for PKCS #1. - Never store private keys in the same place as public configuration.