JweToken Class
Namespace
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)
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()
Signature
public Blob getAad()
Return Value
Type: Blob
The Additional Authenticated Data bytes. Never null for tokens produced by Crypto.parseJwe.
getAlgorithm()
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()
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()
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()
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()
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()
Signature
public Blob getIv()
Return Value
Type: Blob
getTag()
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()
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>]