Skip to content

JWK to PEM converter

Turn an RSA, EC or Ed25519 JSON Web Key into a PEM key. Public keys give BEGIN PUBLIC KEY; private keys also give BEGIN PRIVATE KEY.

Runs in your browser. Nothing you paste is sent to us.

JWK

A sample RSA public key for the made-up realm acme is filled in.

RSAkid sample-acme-rs256

PEM

Public key (BEGIN PUBLIC KEY)
 
BEGIN PUBLIC KEY (SPKI) is what most libraries expect. PKCS #1 is the older RSA-only form.

How to use it

  1. Paste a JWK, or a whole JWKS and pick a key from it.
  2. For RSA keys, choose SPKI and PKCS #8 or the older PKCS #1 form.
  3. Copy the PEM block.
  4. If the key has a certificate in x5c, copy that as a PEM certificate too.

What to know

Many libraries, proxies and API gateways want a PEM public key rather than a JWK. Both hold the same numbers; only the wrapping differs.

To get a Keycloak realm’s signing key as PEM, take the key from the realm’s certs endpoint and convert it here. The admin console also shows the public key and certificate under Realm settings, Keys.

The conversion uses your browser’s Web Crypto. A private RSA JWK needs all of d, p, q, dp, dq and qi.

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

KeyJWK membersPEM form
RSA publicn, eBEGIN PUBLIC KEY (SPKI) or BEGIN RSA PUBLIC KEY (PKCS #1)
RSA privaten, e, d, p, q, dp, dq, qiBEGIN PRIVATE KEY (PKCS #8) or BEGIN RSA PRIVATE KEY (PKCS #1)
EC publiccrv, x, yBEGIN PUBLIC KEY (SPKI)
EC privatecrv, x, y, dBEGIN PRIVATE KEY (PKCS #8)
Ed25519crv, 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.

An RSA public key, shortened
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

  1. Paste a JWK, or paste a JWKS such as your realm’s certs JSON.
  2. If you pasted a set, choose the key by its kid.
  3. For RSA keys, choose SPKI and PKCS #8, or PKCS #1.
  4. 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

MessageCause
An RSA JWK needs n and eThe JSON is not a complete RSA key, or a member name is misspelled
An EC JWK needs crv P-256, P-384 or P-521Another curve, such as secp256k1, which Web Crypto does not support
A symmetric (oct) key has no PEM formThe key is an HMAC or AES secret; use its k value directly
The private key could not be readAn RSA private JWK without p, q, dp, dq and qi
Expected a JWKS or a single JWKThe 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.

Checking or reproducing the conversion locally
# 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.

  1. Watch for a new kid in the realm’s certs endpoint, or rotate on a schedule you control.
  2. Convert the new signing key to PEM here.
  3. Add it to the software that verifies tokens, next to the old key if it supports more than one.
  4. Make the new key active in Keycloak.
  5. 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 kid next 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.
Questions

Frequently asked questions

What is the difference between BEGIN PUBLIC KEY and BEGIN RSA PUBLIC KEY?

`BEGIN PUBLIC KEY` is SPKI: it names the algorithm and works for any key type. `BEGIN RSA PUBLIC KEY` is PKCS #1 and holds only an RSA key. Most software expects SPKI.

Can I convert a symmetric (oct) key?

No. A symmetric key is just bytes and has no PEM form. Use its `k` value, Base64url-decoded, as the secret.

Is the `kid` kept in the PEM?

No. PEM has no place for the key id, use or algorithm. Keep a note of the `kid` yourself if you need to match the PEM to a key later.

How do I get the public key of a Keycloak realm as PEM?

Copy the JSON from the realm’s certs endpoint, paste it here and pick the signing key. Or copy the public key from Realm settings, Keys in the admin console and add the PEM header and footer lines.

Why does my private RSA JWK fail to convert?

Web Crypto needs the full set of private members: `d`, `p`, `q`, `dp`, `dq` and `qi`. A JWK with only `n`, `e` and `d` cannot be imported.

Which EC curves are supported?

P-256, P-384 and P-521, which are the curves for ES256, ES384 and ES512. Ed25519 keys (`kty: OKP`) also convert.

Is the conversion done on a server?

No. It runs in your browser with Web Crypto and nothing is sent anywhere.

Which key should I pick from a Keycloak key set?

For verifying tokens, the key whose `kid` matches the token’s header, with `use: sig`. The `enc` key in the same set is for encryption and will not verify signatures.

Does the PEM change if I convert the same key twice?

No. The DER encoding of a given public key is fixed, so the PEM is identical every time.

Can I paste the whole certs response from my realm?

Yes. Paste the full JSON and pick the key you need from the list, which shows each key’s `kid`, type and algorithm.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.