Skip to content
Get started
Bastionary › Knowledge base › Tokens and APIs

Encrypted JWTs (JWE): when signing isn't enough

By Bastionary · Tokens and APIs

Here is a mistake that makes it into production more often than it should: a team puts a user's email, role, and internal account ID into a JWT, signs it, and ships it to the browser. The token is signed, so it cannot be tampered with — true. But signing does nothing to hide the contents. Anyone who copies that token out of local storage, or finds it in a log, can paste it into a decoder and read every claim. A JWS protects integrity. It does not protect confidentiality.

When the claims themselves are sensitive, you need JWE — JSON Web Encryption (RFC 7516). It encrypts the payload so that only the intended recipient can read it. The distinction between the two is worth making precise, because reaching for JWE when you only need JWS adds cost and complexity for no benefit, and reaching for JWS when you need JWE leaks data.

JWS proves who; JWE hides what

A signed token (JWS) answers "did the issuer I trust produce this, and has it been altered?" Every holder can still read it. That is usually fine — an access token's scopes and subject are not secrets, and the resource server needs to read them anyway.

Rule of thumb: sign everything, encrypt only what must stay private from the parties that hold it. If a claim would be harmless printed in your access logs, it does not need JWE. If it would trigger an incident report, it does.

The structure of a JWE

A JWS compact serialization has three dot-separated parts. A JWE has five: protected header, encrypted key, initialization vector, ciphertext, and authentication tag.

BASE64URL(header) . BASE64URL(encrypted_key) . BASE64URL(iv) . BASE64URL(ciphertext) . BASE64URL(tag)

The header declares two algorithms, not one. alg is the key management algorithm — how the content encryption key is wrapped for the recipient. enc is the content encryption algorithm — how the payload itself is encrypted.

{
  "alg": "RSA-OAEP-256",
  "enc": "A256GCM",
  "kid": "enc-2026-q2",
  "cty": "JWT"
}

Here the content encryption key is wrapped with RSA-OAEP-256 using the recipient's public key, and the payload is encrypted with AES-256 in GCM mode. GCM is authenticated encryption: the tag at the end detects any tampering with the ciphertext, so a JWE gives you integrity as well as confidentiality for its payload.

Direct versus key-wrapping modes

If the two parties already share a symmetric key, alg: dir uses it directly as the content encryption key and the encrypted-key segment is empty. Otherwise a fresh random content encryption key is generated per token and wrapped — with RSA-OAEP for public-key recipients, or ECDH-ES for elliptic-curve key agreement. Per-token keys are what let you encrypt to a recipient you have never shared a secret with, using only their public key.

Nested JWTs: sign, then encrypt

The cty: JWT header above signals a nested token — a JWS wrapped inside a JWE. This is the pattern you almost always want when you need both guarantees, and the order matters.

Sign first, then encrypt. You produce a JWS over your claims, then use that entire JWS as the plaintext payload of a JWE. The recipient decrypts the outer JWE, finds a JWS inside, and verifies its signature. Signing first means the signature covers the real claims; the issuer commits to the content before it is hidden.

Encrypting first and signing the ciphertext is weaker: the signer attests only to "some opaque blob," not to what it contains, and it opens the door to surprising attacks where the ciphertext is detached and re-signed. The sign-then-encrypt order is the convention for good reason.

import { SignJWT, CompactEncrypt, importPKCS8, importSPKI } from 'jose';

const signingKey = await importPKCS8(SIGNING_PEM, 'RS256');
const recipientPub = await importSPKI(RECIPIENT_PEM, 'RSA-OAEP-256');

// 1. Sign the claims (inner JWS)
const jws = await new SignJWT({ accountId: 'acct-91', tier: 'enterprise' })
  .setProtectedHeader({ alg: 'RS256', kid: 'sig-2026-q2' })
  .setIssuedAt()
  .setExpirationTime('5m')
  .sign(signingKey);

// 2. Encrypt the JWS as the payload of a JWE (outer)
const jwe = await new CompactEncrypt(new TextEncoder().encode(jws))
  .setProtectedHeader({ alg: 'RSA-OAEP-256', enc: 'A256GCM', cty: 'JWT' })
  .encrypt(recipientPub);
The recipient must pin the accepted alg and enc values. Accepting whatever the token header declares reintroduces the algorithm-confusion class of bugs — an attacker who can influence the header could downgrade to a weaker cipher or, worse, a mode your library mishandles. Configure an allowlist and reject anything outside it before decryption.

The performance cost

Encryption is not free, and the cost is dominated by the key management step, not the payload cipher. AES-GCM is fast and hardware-accelerated on modern CPUs; encrypting a few hundred bytes of claims is negligible. RSA-OAEP key wrapping is the expensive part — an asymmetric operation on every token mint and every decrypt.

  • Size. A JWE is meaningfully larger than the equivalent JWS — five segments, the wrapped key, the IV, and the tag. If you put encrypted tokens in a cookie, watch the 4KB limit.
  • CPU. If you encrypt to RSA recipients at high volume, the asymmetric wrap will show up in profiles. ECDH-ES with a P-256 key is generally cheaper per operation than RSA-OAEP-256 and produces smaller tokens.
  • Caching. You cannot cache a decrypted token across recipients the way you might cache a JWKS lookup — each JWE is encrypted to one recipient. Decryption happens per request.

When to actually reach for it

JWE earns its complexity in a narrow set of cases: tokens that pass through a party you do not fully trust to read them, claims that are regulated data, and ID tokens delivered through the front channel that carry more than a subject identifier. For a plain access token consumed by your own resource server over TLS, a signed JWS plus transport encryption is the right answer — adding JWE there buys nothing and costs latency.

The honest summary is that most tokens should be signed, a few should be signed and encrypted, and you should know for each token type which category it is in and why. That decision — what to expose, what to hide, which algorithms to permit — belongs to whoever controls your token minting, which is a strong argument for that being you rather than a platform whose defaults you cannot see or change.

Run it yourself

Bastionary is free, open-source software. Follow the quick start to have it running on your own server, or browse the releases on Git.

Get started