JweToken Class

Represents a parsed JWE (JSON Web Encryption) token in the compact serialization form defined by RFC 7516. A JweToken holds the components needed for decryption: protected header, ephemeral public key, wrapped content encryption key, initialization vector, ciphertext, authentication tag, and Additional Authenticated Data (AAD).

Namespace

System

Usage

Use JweToken when you need programmatic access to the individual components of a JWE compact serialization token. For example, to inspect the algorithm used, verify the ephemeral public key, or route decryption based on the enc header. The typical flow is: parseJwe → inspect the header via getAlgorithm() / getEncryptionAlgorithm() → call Crypto.decrypt(...) with the extracted components.

Crypto.parseJwe(Blob) produces an instance. The caller then passes the extracted components to the nine-argument Crypto.decrypt(...) overload to complete the two-step JWE decryption flow. JweToken is immutable. All fields are set at construction and exposed only through read-only accessors.

Example

1// Parse a JWE compact serialization token received from an external service.
2Blob jweCompact = Blob.valueOf(response.getBody());
3JweToken token = Crypto.parseJwe(jweCompact);
4
5System.debug('Key management algorithm: ' + token.getAlgorithm());        // e.g. ECDH-ES+A128KW
6System.debug('Content encryption algorithm: ' + token.getEncryptionAlgorithm()); // e.g. A128GCM
7System.debug('IV length: ' + token.getIv().size());                       // 12 for GCM, 16 for CBC
8
9// Decrypt using the recipient's EC private key (PKCS#8 DER) plus the components
10// carried by the token. This performs ECDH-ES key agreement, Concat KDF,
11// AES Key Unwrap, and content decryption.
12Blob plaintext = Crypto.decrypt(
13    token.getAlgorithm(),
14    token.getEncryptionAlgorithm(),
15    recipientPrivateKey,
16    token.getEphemeralPublicKey(),
17    token.getEncryptedKey(),
18    token.getIv(),
19    token.getCiphertext(),
20    token.getTag(),
21    token.getAad()
22);

JweToken Constructors

The following are constructors for JweToken.

JweToken(alg, enc, epk, encrypted_key, iv, ciphertext, tag, aad)

Creates a new JweToken from the parsed components of a JWE compact serialization token. This constructor is typically invoked internally after Crypto.parseJwe(Blob) has decomposed the token.

Signature

public JweToken(String alg, String enc, Blob epk, Blob encrypted_key, Blob iv, Blob ciphertext, Blob tag, Blob aad)

Parameters

  • alg: Type: StringKey management algorithm from the JWE protected header (alg field). For example, ECDH-ES+A128KW.
  • enc: Type: String Content encryption algorithm from the JWE protected header (enc field). For example, A128GCM or A256CBC-HS512.
  • epk: Type: Blob Ephemeral public key from the JWE protected header (epk JWK), encoded as X.509 SubjectPublicKeyInfo DER bytes.
  • encrypted_key: Type: Blob Encrypted content encryption key (wrapped CEK) — part 2 of the compact serialization.
  • iv: Type: Blob Initialization vector for content decryption — part 3 of the compact serialization.
  • ciphertext: Type: Blob Encrypted content — part 4 of the compact serialization.
  • tag: Type: Blob Authentication tag — part 5 of the compact serialization.
  • aad: Type: Blob Additional Authenticated Data — the ASCII bytes of the base64url-encoded protected header (per RFC 7516 §5.1, step 14).

JweToken Methods

The following are methods for JweToken.

getAad()

Returns the Additional Authenticated Data (AAD) associated with the JWE token. Per RFC 7516 §5.1 step 14, the AAD is the US-ASCII byte encoding of the base64url-encoded JWE protected header. This value is bound to the ciphertext by the AEAD algorithm and must match at decryption time.

Signature

public Blob getAad()

Return Value

Type: Blob

The Additional Authenticated Data bytes. Never null for tokens produced by Crypto.parseJwe.

getAlgorithm()

Returns the key management algorithm from the JWE protected header. This is the alg value that governs how the content encryption key (CEK) is protected.

Signature

public String getAlgorithm()

Return Value

Type: String

The key management algorithm identifier. The supported values are ECDH-ES+A128KW, ECDH-ES+A192KW, and ECDH-ES+A256KW.

Example

1JweToken token = Crypto.parseJwe(compactJwe);
2if (token.getAlgorithm() == 'ECDH-ES+A128KW') {
3    // ECDH-ES key agreement with A128KW key wrap
4}

getCiphertext()

Returns the encrypted content of the JWE — the fourth component of the compact serialization (before the authentication tag).

Signature

public Blob getCiphertext()

Return Value

Type: Blob

The ciphertext bytes. Length depends on the content encryption algorithm and the size of the original plaintext. The maximum supported ciphertext size is approximately 1 MB plus padding and tag overhead.

getEncryptedKey()

Returns the wrapped content encryption key (CEK), the second component of the JWE compact serialization. This value is decrypted using the recipient's key management key (per getAlgorithm()) to recover the CEK, which is then used to decrypt getCiphertext().

Signature

public Blob getEncryptedKey()

Return Value

Type: Blob

The encrypted content encryption key bytes. Always present for the supported ECDH-ES+A*KW algorithms because these wrap the CEK with AES Key Wrap.

getEncryptionAlgorithm()

Returns the content encryption algorithm from the JWE protected header. This is the enc value that governs how the payload is encrypted.

Signature

public String getEncryptionAlgorithm()

Return Value

Type: String

The content encryption algorithm identifier. Supported values include:

value description
A128GCM AES-128 in Galois/Counter Mode
A192GCM AES-192 in Galois/Counter Mode
A256GCM AES-256 in Galois/Counter Mode
A128CBC-HS256 AES-128 in CBC mode with HMAC-SHA-256
A192CBC-HS384 AES-192 in CBC mode with HMAC-SHA-384
A256CBC-HS512 AES-256 in CBC mode with HMAC-SHA-512

An unsupported value causes Crypto.parseJwe to throw a SecurityException. Therefore, this method never returns an unsupported value for a token that has been parsed successfully.

getEphemeralPublicKey()

Returns the ephemeral public key from the JWE protected header (the epk JWK). The key is returned in X.509 SubjectPublicKeyInfo DER encoding, suitable for passing to standard key deserialization utilities.

Signature

public Blob getEphemeralPublicKey()

Return Value

Type: Blob

The DER-encoded ephemeral public key bytes. Usage For ECDH-ES key agreement algorithms, the ephemeral public key contributes half of the shared secret. Applications that need to perform key agreement outside of Crypto.decrypt(...) can use this method to obtain the peer's ephemeral key material.

getIv()

Returns the initialization vector used for content decryption — the third component of the JWE compact serialization.

Signature

public Blob getIv()

Return Value

Type: Blob

The initialization vector bytes. Length depends on the content encryption algorithm:
  • 12 bytes for A128GCM / A192GCM / A256GCM
  • 16 bytes for A128CBC-HS256 / A192CBC-HS384 / A256CBC-HS512

getTag()

Returns the authentication tag — the fifth and final component of the JWE compact serialization. The tag is used by the AEAD algorithm to detect tampering with the ciphertext or AAD.

Signature

public Blob getTag()

Return Value

Type: Blob

The authentication tag bytes. Length depends on the content encryption algorithm:

  • 16 bytes for GCM variants (A128GCM, A192GCM, and A256GCM)
  • 16, 24, or 32 bytes for CBC-HS variants (A128CBC-HS256, A192CBC-HS384, and A256CBC-HS512 respectively)

toString()

Returns a string representation of the token suitable for debugging and logging. The output contains only metadata (algorithm identifiers and byte lengths) and never exposes the ephemeral public key, wrapped CEK, ciphertext, or authentication tag.

Signature

public String toString()

Return Value

Type: String

A formatted string of the form:

1System.JweToken[alg=<algorithm>, enc=<encryption>, epkLen=<n>, encKeyLen=<n>, ivLen=<n>, ciphertextLen=<n>, tagLen=<n>, aadLen=<n>]

Example

1
2JweToken token = Crypto.parseJwe(compactJwe);
3System.debug(token);
4// System.JweToken[alg=ECDH-ES+A128KW, enc=A128GCM, epkLen=91,
5//   encKeyLen=24, ivLen=12, ciphertextLen=48, tagLen=16, aadLen=112]