Choose encryption algorithms
Pick the alg that delivers the content key to your recipient and the enc that locks the content, and know what each needs and what is refused.
Every JWE header names two algorithms. alg (key management) is how the content key reaches the recipient: wrapped with a key you share, locked with their public key, derived from a password, or no wrapping at all. enc (content encryption) is how the payload itself is locked with that content key. The first depends on who opens the token; the second you can almost always leave at the default, A256GCM.
#Pick alg by who opens the token
| Your situation | Pick | Key |
|---|---|---|
| Your own server encrypts and decrypts (cookies, state, caches) | A256KW, or dir | generateJWK("A256KW"), or generateJWK("A256GCM") for dir |
| Many senders, one recipient who publishes a public key | ECDH-ES+A256KW | generateJWK("ECDH-ES+A256KW") |
| The recipient's system only speaks RSA | RSA-OAEP-256 | generateJWK("RSA-OAEP-256") |
| A person types the secret | PBES2-HS256+A128KW (or a stronger PBES2) | the password string |
| One ciphertext for several recipients | ECDH-ES+A*KW, RSA-OAEP* or A*KW per recipient | see Encrypt for several recipients |
diris the shortest token: your key is the content key, so there's no encrypted key segment. It suits a key that never leaves your servers.ECDH-ES+A256KWderives a fresh wrapping key per token from an ephemeral key pair; keys are small andX25519works too (generateJWK("ECDH-ES+A256KW", { namedCurve: "X25519" })). BareECDH-ESuses the derived key directly, for one recipient only.RSA-OAEP(SHA-1 inside OAEP) exists for systems that only speak it. PickRSA-OAEP-256for new keys.- PBES2 is only as strong as the password. Prefer a generated key whenever no person has to type it.
#Key management algorithms
alg | Key | Encrypted key segment |
|---|---|---|
dir | the content key itself, an oct key of the enc size | empty |
A128KW, A192KW, A256KW | AES key of 16, 24 or 32 bytes | content key plus 8 bytes |
A128GCMKW, A192GCMKW, A256GCMKW | AES key of 16, 24 or 32 bytes; adds iv and tag | content key length |
RSA-OAEP, RSA-OAEP-256, RSA-OAEP-384, RSA-OAEP-512 | RSA key pair, modulus of 2048 bits or more | modulus length |
ECDH-ES | EC (P-256, P-384, P-521) or X25519 key pair; adds epk | empty |
ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KW | same as ECDH-ES | content key plus 8 bytes |
PBES2-HS256+A128KW, PBES2-HS384+A192KW, PBES2-HS512+A256KW | a password string; adds p2s and p2c | content key plus 8 bytes |
PBES2 counts and salts:
| Parameter | encrypt default | encrypt accepts | decrypt accepts by default |
|---|---|---|---|
p2c (count) | 600,000 | 1000 to 600,000 | 1000 to 600,000 (minIterations, maxIterations) |
p2s (salt) | 16 random bytes | 8 bytes or more | 8 bytes or more |
#Content encryption algorithms
enc | Content key | Construction |
|---|---|---|
A128GCM | 16 bytes | AES-GCM |
A192GCM | 24 bytes | AES-GCM |
A256GCM | 32 bytes | AES-GCM, the default |
A128CBC-HS256 | 32 bytes | AES-128-CBC with HMAC-SHA-256 |
A192CBC-HS384 | 48 bytes | AES-192-CBC with HMAC-SHA-384 |
A256CBC-HS512 | 64 bytes | AES-256-CBC with HMAC-SHA-512 |
Keep A256GCM unless a recipient requires another. The A*CBC-HS* family exists for systems that predate GCM. The 192-bit variants need a runtime whose Web Crypto supports 192-bit AES keys, which Chromium-based browsers don't. A dir key's size follows the enc it serves: generateJWK("A128CBC-HS256") makes 32 bytes.
#What is refused
noneasalg:ERR_JWE_ALG_NOT_ALLOWED, before any key is read.RSA1_5(RSA PKCS#1 v1.5 encryption), open to padding-oracle attacks:ERR_JWE_ALG_NOT_ALLOWED("Key management algorithm not supported").- Any other
enc, such asA256CTR:ERR_JWE_INVALID. zipcompression, on both sides.X448keys, and RSA keys under 2048 bits, at generation and use.dirand bareECDH-ESin a multi-recipient object.- Generating a
diror PBES2 key:generateJWKthrowsERR_JWK_UNSUPPORTED. Generate a content key (generateJWK("A256GCM")) fordir, and pass a password string for PBES2.
#Going further
- Generating and storing keys: Generate keys.
- Turning a password into a reusable key: Derive keys from a password.
- Signing instead of encrypting: Choose a signing algorithm.