> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-framework/modularity/jwt/encryption-jwe.md).

# Encryption (JWE)

Encrypt and decrypt JWTs using RSA-OAEP key wrap or direct symmetric encryption.

A JSON Web Encryption (JWE) token has its payload encrypted — even the issuer cannot read it without the decryption key. Use JWE when the claims contain sensitive data (PII, PHI, secrets) that must remain confidential in transit *and* at rest in client storage.

`bx-jwt` supports two encryption modes:

* **RSA-OAEP-256 + A256GCM** — asymmetric key wrapping. Anyone with the RSA public key can encrypt; only the private-key holder can decrypt.
* **`dir` + A256GCM** — direct symmetric encryption with a 256-bit (32-byte) shared key.

## RSA Key Wrapping (Asymmetric)

```javascript
keys = jwtGenerateKeyPair( "RS256" );  // 2048-bit RSA pair

// Encrypt with the public key (anyone can do this)
token = jwtEncrypt( { sub: "u1", ssn: "123-45-6789" }, keys.publicKey, {
    keyAlgorithm: "RSA-OAEP-256",
    encAlgorithm: "A256GCM"
} );

// Decrypt with the private key (only the holder can do this)
payload = jwtDecrypt( token, keys.privateKey, {
    keyAlgorithm: "RSA-OAEP-256",
    encAlgorithm: "A256GCM"
} );
```

This pattern is ideal when many producers must encrypt for a single consumer (e.g. a central service that receives sensitive payloads from many clients).

## Direct Symmetric Encryption

When both ends of the conversation can hold the same secret, `dir` is faster and produces smaller tokens.

```javascript
secret = jwtGenerateSecret( 256 );  // 32 bytes for A256GCM

token   = jwtEncrypt( { sub: "u1" }, secret, {
    keyAlgorithm: "dir",
    encAlgorithm: "A256GCM"
} );

payload = jwtDecrypt( token, secret, {
    keyAlgorithm: "dir",
    encAlgorithm: "A256GCM"
} );
```

{% hint style="warning" %}
The `dir` mode requires the encryption key to be exactly the right size for the content encryption algorithm. A256GCM = 32 bytes (256 bits).
{% endhint %}

## Nested JWT (Sign → Encrypt)

When you need both **integrity** (proof of issuer) and **confidentiality** (encrypted payload), sign the token first and then encrypt the result. The standard `cty: "JWT"` header tells consumers the JWE wraps a JWS.

```javascript
// 1. Sign the inner JWS
signed = jwtCreate( { sub: "u1", role: "admin" }, innerPrivKey, "RS256", {
    headers: { cty: "JWT" }
} );

// 2. Encrypt the signed token as the outer JWE
encrypted = jwtEncrypt( signed, outerPubKey, {
    keyAlgorithm : "RSA-OAEP-256",
    encAlgorithm : "A256GCM",
    headers      : { cty: "JWT" }
} );

// On the consumer side:
decrypted = jwtDecrypt( encrypted, outerPrivKey, {
    keyAlgorithm : "RSA-OAEP-256",
    encAlgorithm : "A256GCM"
} );
payload = jwtVerify( decrypted, innerPubKey, "RS256" );
```

## Fluent Builder for JWE

The same fluent builder used for signing also produces JWEs — just call `.encrypt()` to terminate the chain:

```javascript
token = jwtNew()
    .subject( "patient-456" )
    .claim( "phi", { dob: "1990-01-15", ssn: "xxx-xx-1234" } )
    .expireIn( 900 )
    .encrypt( secret, "dir", "A256GCM" );
```

## Related

* [Signing (JWS)](/boxlang-framework/modularity/jwt/signing-jws.md)
* [Key Management](/boxlang-framework/modularity/jwt/key-management.md)
* [Security Best Practices](/boxlang-framework/modularity/jwt/security-best-practices.md)
* [JWTEncrypt()](/boxlang-framework/modularity/jwt/reference/built-in-functions/jwtencrypt.md)
* [JWTDecrypt()](/boxlang-framework/modularity/jwt/reference/built-in-functions/jwtdecrypt.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://boxlang.ortusbooks.com/boxlang-framework/modularity/jwt/encryption-jwe.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
