Encrypting tokens with JWE
A signed tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. 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 JWTA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing. proves who issued its claims and that nobody changed them, but as Inside a JWT showed, the payload is only base64urlA way of encoding data as ordinary text characters so it can travel safely in headers, URLs, and JSON. Anyone can decode it, so it hides nothing; Base64url is a variant safe for URLs, used in JWTs., 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 TLSTransport Layer Security, the protection behind HTTPS. It encrypts traffic between two connected systems, detects changes on the way, and uses certificates so the client can confirm which server it reached. 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 keyThe half of a key pair that can be shared freely. It checks signatures made with the matching private key and can encrypt data that only that private key can decrypt.. 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 keyThe half of a key pair that its owner keeps to themselves and uses to create signatures or decrypt data. Anyone who obtains it can act as its owner., 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 tokenIn OpenID Connect, a signed JWT from the provider, addressed to one client, saying which account authenticated, when, and in answer to which request. It reports a sign-in and is not used to call APIs. 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 JWSThe JOSE format for signed content, defined in RFC 7515. A signed JWT is a JWS: a header, a payload and a signature, each base64url-encoded and joined by dots..
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.
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 signatureA value made with a private key that lets anyone holding the matching public key check that a message came from the key's holder and has not been changed. It proves origin and integrity; it does not hide the message. under the expected issuerThe party that creates and signs a token, assertion, or certificate, named in a token's iss claim. A recipient must check that the issuer is exactly the one it expected and trusts., 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.
Try it in the Lab
- Separate an encrypted envelope from the signer inside itEncrypt a message to a key of your own and read its five parts, change each part in turn and watch every change refused, see that decrypting proves nothing about the sender, then give your lab tenant an encryption key.