Why turn a PEM key into a JWK
Keys are usually created with tools that write PEM: OpenSSL, ssh-keygen in PEM mode, cloud key services, certificate authorities. OAuth and OpenID Connect, on the other hand, exchange keys as JSON Web Keys.
You need the JWK form when you publish a key set for your own service, register a client’s public key with an authorization server, or configure a library that reads JWKs. This tool reads the common PEM forms and writes the matching JWK, with a key id and the metadata you choose.
The PEM forms it reads
The label on the BEGIN line tells you what is inside the Base64.
| PEM label | Contents | Standard |
|---|---|---|
PUBLIC KEY | Any public key with its algorithm | SPKI (RFC 5280) |
RSA PUBLIC KEY | An RSA public key only | PKCS #1 (RFC 8017) |
CERTIFICATE | An X.509 certificate with a public key inside | RFC 5280 |
PRIVATE KEY | Any private key with its algorithm | PKCS #8 (RFC 5208) |
RSA PRIVATE KEY | An RSA private key only | PKCS #1 |
EC PRIVATE KEY | An EC private key with its curve | SEC 1 (RFC 5915) |
Keys protected with a passphrase (ENCRYPTED PRIVATE KEY) and OpenSSH keys (OPENSSH PRIVATE KEY) are not read. Convert them to plain PEM first with openssl pkcs8 or ssh-keygen -p -m PEM.
How the conversion works
The tool decodes the Base64 body into DER bytes and reads the ASN.1 structure. Older RSA and EC forms are rewrapped into SPKI or PKCS #8, and a certificate gives up its SPKI. Then Web Crypto imports the key and exports it as a JWK, which checks that the key is valid on the way.
Only the key material is kept: n and e for RSA, crv, x and y for EC, and the private members when the input is a private key. You then add the metadata.
{
"kty": "EC",
"kid": "F53etudom86_iDj_5yR-Z9hsuTXdSuYhTtAKVu4i2Y8",
"use": "sig",
"alg": "ES256",
"crv": "P-256",
"x": "…",
"y": "…"
}Choosing kid, alg and use
Key id
The kid lets a verifier pick this key out of a set. Any string unique in the set works. The default here is the RFC 7638 thumbprint: a SHA-256 hash of the key’s required members, so the same key always gets the same id. You can type your own, or leave it out.
Algorithm
The alg member ties the key to one algorithm, which protects against a verifier using it in an unexpected way. RSA keys can serve RS256, PS256 and others, or RSA-OAEP for encryption. EC keys follow their curve: P-256 for ES256, P-384 for ES384, P-521 for ES512.
Use
Set use to sig for signing keys and enc for encryption keys. Using one key for both is discouraged; keep two.
How to convert a key
- Paste the PEM block, including the
BEGINandENDlines. - Check the summary: what was found, the key type and the size.
- Choose the
kid: thumbprint, your own, or none. - Choose the algorithm and use.
- Copy the JWK, or the JWKS that contains only the public key.
Using the result with Keycloak
Clients that sign their own JWTs
A Keycloak client can authenticate with “Signed JWT” (private_key_jwt). Keycloak then needs the client’s public key. On the client’s Keys tab you can import a certificate or turn on “Use JWKS URL” and give the address where your app publishes its key set. This tool produces that key set from your PEM key.
Identity provider federation
When Keycloak brokers another OpenID Connect provider, it validates that provider’s tokens with keys from its JWKS URL or with a key you enter. Converting the provider’s PEM key to JWK helps you compare it with what the provider publishes.
Your own services
If one of your services issues tokens, it should publish a JWKS like Keycloak’s certs endpoint. Paste its public key here to get the JSON.
Certificates and x5c
When you paste a certificate, the JWK gets an x5c member holding the certificate in standard Base64, and the summary shows the subject and expiry. Some clients use x5c to check a key against a trusted certificate authority; most just use the key.
To read the rest of a certificate, such as its issuer, serial number and extensions, use the X.509 decoder.
Common errors
- “No PEM block found”: the
BEGINorENDline is missing or damaged, often from copying only the Base64 body. - “Only RSA, EC and Ed25519 keys are supported”: DSA and other key types cannot become JWKs here.
- “Only the P-256, P-384 and P-521 curves”: curves such as secp256k1 are not supported by Web Crypto.
- “This private key is encrypted”: remove the passphrase locally first.
- “This EC private key does not name its curve”: rare SEC 1 keys omit the curve; export the key again with its parameters.
Publishing a key set for your own client
When a client authenticates to Keycloak with a signed JWT, Keycloak can fetch the client’s public keys from a URL. Here is a simple way to set that up.
- Generate a key pair and keep the private key in your app’s secret store.
- Paste the public key here, keep the thumbprint
kid, setalgto the algorithm your app signs with, andusetosig. - Serve the “As a JWKS” output at a stable HTTPS address, such as
/.well-known/jwks.jsonon your app. - In Keycloak, open the client, then the Keys tab, turn on “Use JWKS URL” and enter that address.
- Sign the client assertion with the private key and put the same
kidin its header.
To rotate later, add the new public key to the served set first, switch your app to the new private key, then remove the old key from the set.
Checking the result
Before you rely on a converted key, confirm it is the one you think it is.
- Compare the size in the summary with the key you generated: 2048 bits, P-256, and so on.
- Paste the JWKS into the JWKS viewer to see the thumbprint and any warnings.
- Sign a test token with the private key and verify it with the JWK in the signature verifier.
Keep the PEM file and the JWK side by side in your notes during the first rollout, labelled with the kid. It makes the next rotation much easier to reason about.
Security notes
Converting a public key or certificate is harmless. With a private key, the tool shows a private JWK and, separately, the public JWK that is safe to publish. Only the public one belongs in a JWKS.
Nothing you paste leaves your browser. Still, treat a private key you pasted into any web page as something to handle carefully, and prefer local tools for production keys.
Best practices
- Publish only public keys, and only over HTTPS.
- Set
kid,useandalgon every published key. - Use the thumbprint as
kidwhen you have no naming scheme of your own. - Keep the previous key in your published set until tokens signed with it have expired.