Course contentsShow
← Security
Lesson 792 of 3,05216. Web Security - Authentication MechanismsFree lesson

JWE (JSON Web Encryption)

Encrypting JWT payloads for confidentiality using JWE to protect sensitive claim data.

JWE (JSON Web Encryption)

You'll learn: How to encrypt JWT payloads using JWE to protect sensitive claim data from being read by anyone who intercepts the token.

The Confidentiality Problem with JWTs

Remember that standard JWTs (JWS) are signed, not encrypted. Anyone who intercepts a JWT can decode the base64-encoded payload and read all the claims inside—they just can't modify it without invalidating the signature. This is fine for non-sensitive data like user IDs, but what if your JWT contains social security numbers, medical records, or financial information?

What is JWE?

JSON Web Encryption (JWE) encrypts the entire JWT payload so that only parties with the correct decryption key can read the claims. Think of it like putting your JWT in a locked safe instead of a tamper-evident envelope—not only can't attackers modify it, they can't even see what's inside.

JWE Structure

A JWE token has five base64url-encoded parts (compared to JWT's three):

HEADER.ENCRYPTED_KEY.INITIALIZATION_VECTOR.CIPHERTEXT.AUTHENTICATION_TAG

The header specifies encryption algorithms (alg for key encryption, enc for content encryption), the encrypted key protects the symmetric key used for encryption, and the ciphertext contains your actual claims in encrypted form.

When to Use JWE

Use JWE when your tokens contain genuinely sensitive data that must remain confidential in transit. Most applications don't need JWE because they avoid storing sensitive data in tokens altogether. JWE adds complexity and performance overhead—only use it when confidentiality truly requires encryption, not just signature validation.

Key Takeaway: JWE encrypts JWT payloads for confidentiality, protecting sensitive claims from being read by anyone who intercepts the token, but adds complexity and should only be used when truly necessary.