JWE: encrypt data for a recipient and understand its limits
Learn the five parts of Compact JWE, how recipient encryption works, why encryption alone does not identify the sender, and what to validate.
JWE: encrypt data for a recipient and understand its limits
JSON Web Encryption (JWE) is a JSON-based format that encrypts a message so only an authorized recipient can read it, while also providing integrity checks for the encrypted content. It can protect a JWT or other data in transit or storage. Encryption alone does not prove who sent the message. If sender authentication is required, check what the key arrangement and protocol provide; a signature such as JWS may also be needed.
The five parts of Compact JWE
JWE Compact Serialization contains five period-separated parts:
protected-header.encrypted-key.iv.ciphertext.authentication-tag**Protected header:** describes encryption, including key-management algorithm `alg` and content-encryption algorithm `enc`. The protected header is integrity-checked.
**Encrypted key:** delivers the symmetric content-encryption key (CEK) to the recipient according to the key-management method. It can be empty when the defined method requires that.
**IV:** initialization vector required by the encryption algorithm.
**Ciphertext:** encrypted message content.
**Authentication tag:** checks integrity of the ciphertext and protected header, among other authenticated data.
JWE differs in structure and purpose from the three-part Compact JWS. Base64url represents JWE components; confidentiality comes from the encryption algorithm. Some metadata, such as ciphertext length and protected header, may remain visible. JWE does not hide every detail.
How can only the recipient read it?
In a common public-key method, the sender encrypts for the recipient’s public key and the recipient decrypts with its private key. Rather than encrypting a large message directly with public-key cryptography, a hybrid method commonly encrypts the content with a random symmetric CEK and protects that CEK for the recipient using the agreed key-management method. Header values alg and enc identify the methods, but the recipient must check them against a pre-agreed safe allowlist instead of executing whatever the header requests.
For a hypothetical example, service A sends sensitive results to user B using B’s verified public key; B decrypts them with a private key it controls. Another party that knows B’s public key can also create ciphertext for B. Therefore, successful decryption does not prove A sent the message. To establish A’s identity, use a structure that includes A’s signature or another authenticated channel.
Combining JWE and JWS
JWS provides integrity and key-based origin checks through a signature or MAC; JWE provides content confidentiality. When both are needed, one possible design is to create a JWS first and then encrypt that result as JWE. The recipient decrypts and validates the outer JWE, then separately validates the inner JWS. The token profile and system contract must define the permitted nesting and order. A signed JWT is not automatically encrypted, and an encrypted JWT does not automatically contain a sender’s signature.
| Need | Protection concept | Receiver checks |
|---|---|---|
| Detect changes and verify signer | JWS | Trusted key, allowed algorithm, signature, issuer and profile |
| Reveal content only to recipient | JWE | Key source, alg/enc policy, decryption and authentication tag |
| Confidentiality and sender signature | Profile-defined nested JWE/JWS | Validate both outer encryption and inner signature |
Practical safeguards
Obtain the recipient’s key through an authenticated channel. Encrypting with an attacker’s public key can expose sensitive content to that attacker.
Use a standard library and algorithm policy. Do not automatically allow the header’s `alg` or `enc`; reject weak or prohibited algorithms.
Do not treat encryption as sender authentication. Add JWS or mutual authentication if the protocol needs sender identity.
Avoid exposing detailed decryption or tag errors to external callers; detailed distinctions can help attackers alter ciphertext repeatedly. Keep internal diagnostics free of plaintext secrets.
Protect keys and plaintext. JWE does not replace separate controls for private keys, CEKs, decrypted content, logs, or backups.
References
[RFC 7516 — JSON Web Encryption (JWE)](https://www.rfc-editor.org/rfc/rfc7516.html)
[RFC 7515 — JSON Web Signature (JWS)](https://www.rfc-editor.org/rfc/rfc7515.html)
[RFC 7519 — JSON Web Token (JWT)](https://www.rfc-editor.org/rfc/rfc7519.html)