Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Encrypting tokens with JWE

A signed token proves who made it, but hides nothing. When a token must also stay private from the systems that carry it, JOSE wraps it in an encrypted envelope for one recipient. This lesson covers how that envelope is built, what its parts hold, and why opening it says nothing about who sent it.

When signing is not enough

A signed JWT proves who issued its claims and that nobody changed them, but as Inside a JWT showed, the payload is only base64url, so anyone holding the token can read it. Usually that is fine. Sometimes a token has to pass through a component that should carry it without reading it, or it carries data that must stay private even from the application that finally receives it. For that, JOSE adds JSON Web Encryption (JWE): an encrypted envelope addressed to one intended recipient.

Authenticated and hybrid encryption

Two ideas sit under JWE, and both are worth having before reading the format. The first is authenticated encryption, which Keys, encryption, and signatures introduced as encryption that also detects changes. A plain cipher keeps data secret but doesn't notice if someone alters the scrambled bytes; the recipient might decrypt tampered bytes into plausible nonsense. An authenticated cipher such as AES-GCM, the shared-key cipher you saw named in the TLS handshake in HTTPS and certificates in practice, produces an authentication tag alongside the scrambled bytes, and decryption fails outright if a single bit of either has changed. These ciphers also take an initialization vector (IV), a fresh random value for each message, so that encrypting the same content twice gives different output.

The second idea is hybrid encryption. Secrets and key pairs noted that public-key operations are much slower than shared-key ones and can encrypt only a small amount of data. So almost nothing encrypts a whole message directly with a public key. Instead the sender generates a fresh random content-encryption key, encrypts the message with that key using a fast cipher like AES-GCM, and then handles the small content key with the recipient's public key. The recipient recovers the content key with its private key, then uses it to decrypt the message.

JWE offers two ways to handle the content key with the recipient's key. In key wrapping, the sender encrypts the content key to the recipient's public key and sends the result in the envelope. In key agreement, both sides derive the same content key from the recipient's public key and a fresh key the sender makes, so no wrapped key is sent. Key agreement is closer to the TLS handshake, where both sides compute a shared key from a key exchange, while key wrapping has the sender choose the content key and encrypt it across. They reach the same place: a shared content key only the recipient can obtain.

The five parts

A JWE in compact form has five base64url parts, separated by four dots. Under them is the decoded header of an ID token that Lab Photos encrypted for its printing service:

protected header . encrypted key . initialization vector . ciphertext . authentication tag

Decoded protected header:
{"alg":"RSA-OAEP-256","enc":"A256GCM","kid":"printer-enc-2026","cty":"JWT"}

The header names two algorithms, and that split is the heart of JWE. alg is the key-management algorithm, how the content key is handled: RSA-OAEP-256 wraps it with the recipient's RSA public key. enc is the content-encryption algorithm, how the message itself is encrypted: A256GCM is AES-GCM with a 256-bit key. They are two separate decisions. The encrypted key part holds the wrapped content key; in a key-agreement mode, where both sides derive the key, that part is empty, and an empty section does not turn a five-part JWE into a three-part JWS.

Generate the content key

Content key
Fresh and random
Message
Readable plaintext
Encrypted key
Not made yet
Tag
Not made yet

Encrypt the message

Content key
Used by the cipher
Message
Ciphertext (enc: A256GCM)
Encrypted key
Not made yet
Tag
Made with the ciphertext

Wrap the content key

Content key
Wrapped (alg: RSA-OAEP-256)
Message
Ciphertext
Encrypted key
The wrapped content key
Tag
Sent with the ciphertext

Five parts: protected header . encrypted key . initialization vector . ciphertext . authentication tag.

Unwrap the content key

Content key
Recovered with its private key
Message
Ciphertext
Encrypted key
Opened
Tag
Not checked yet

Check the authentication tag

Content key
Recovered
Message
Ciphertext
Encrypted key
Opened
Tag
Fails if a bit changed

Decrypt the message

Content key
Recovered
Message
Readable plaintext
Encrypted key
Opened
Tag
Checked first

The recipient checks the tag before it uses a single byte of plaintext.

The sender builds the five parts; the recipient reverses the steps, checking the tag before using any plaintext.

One point about the protected header is easy to miss. The header stays readable, so nothing secret belongs in it, but it is not unprotected: the encoded header is fed into the authenticated cipher as additional authenticated data. A change to the header, such as swapping the enc value, makes the authentication tag fail just as a change to the scrambled bytes would. The recipient checks that tag before it uses a single byte of plaintext.

Algorithm choices

As with signing, the recipient decides which algorithms it accepts rather than following the header. RSA encryption needs a padding scheme, the fixed way a message is prepared before the RSA operation, and the choice matters. Prefer RSA-OAEP-256, which uses the modern OAEP padding, or an elliptic-curve key-agreement mode, over the older RSA1_5, whose padding has a long history of exploitable flaws. Use an authenticated enc such as A256GCM.

Be cautious with compression. JWE can compress the plaintext before encrypting, but compressing secret data together with attacker-influenced data can leak the secret through the length of the result, the shape of attack that has broken compressed-then-encrypted protocols before. Unless there is a clear need, leave compression off.

Decryption is not identity

There is a trap that mirrors "decoding is not validating". Because a recipient's public encryption key is public, anyone can encrypt a message to it. So the fact that a message decrypts proves only that it was encrypted to this recipient; it says nothing about who sent it. Encryption answers "who may read this", not "who wrote this".

To get both, a sender uses sign then encrypt: it signs the claims into a JWS, then encrypts that JWS inside a JWE, and the outer header's cty: JWT marks the content as a nested token. The recipient reverses the layers: decrypt the envelope, then validate the inner signature under the expected issuer, algorithm, audience and claims, exactly as for any signed JWT. Both layers need their own checks. A readable inner token is not an accepted one, and a valid tag on the outer envelope says nothing about the issuer inside.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

A message encrypted to a service's public key decrypts cleanly. What has that established about who sent it? 1 of 3

Choose one answer.

In transit, the enc value in a JWE's protected header is altered. What happens when the recipient decrypts? 2 of 3

Choose one answer.

A recipient replaces its RSA encryption key with an elliptic-curve key, and senders keep A256GCM for the message itself. Which protected header value changes? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity