> For the complete documentation index, see [llms.txt](https://web-token.spomky-labs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://web-token.spomky-labs.com/2.x/advanced-topics-1/encrypted-tokens-and/multiple-recipients.md).

# Multiple Recipients

When you need to encrypt the same payload for several audiences, you may want to do it at once. The JWE Builder supports multiple recipients.

With the example below, we will create an encrypted token for three different recipients using three different key encryption algorithms.

**Important notes:**

* The content encryption algorithm MUST be the same for all recipients.
* The Key Management Modes of the key encryption algorithms MUST be compatible (see table below).

```php
$jweBuilder
    ->create()
    ->withPayload('...')
    ->withSharedProtectedHeader(['enc' => 'A128GCM'])
    ->addRecipient($recipient_key_1, ['alg' => 'RSA1_5'])
    ->addRecipient($recipient_key_2, ['alg' => 'RSA-OAEP-256'])
    ->build();
```

**Note: when an unprotected header is set, the Compact Serialization mode is not available.**

## Key Management Modes

Each Key Encryption Algorithm has its own Key Management Mode.

### Key Encryption Algorithms And Associated Key Management Mode.

| Algorithm  Key Management Mode | Key Encryption | Key Wrapping | Direct Key Agreement | Key Agreement with Key Wrapping | Direct Encryption |
| ------------------------------ | -------------- | ------------ | -------------------- | ------------------------------- | ----------------- |
| dir                            |                |              |                      |                                 | X                 |
| A128KW                         |                | X            |                      |                                 |                   |
| A192KW                         |                | X            |                      |                                 |                   |
| A256KW                         |                | X            |                      |                                 |                   |
| ECDH-ES                        |                |              | X                    |                                 |                   |
| ECDH-ES+A128KW                 |                |              |                      | X                               |                   |
| ECDH-ES+A192KW                 |                |              |                      | X                               |                   |
| ECDH-ES+A256KW                 |                |              |                      | X                               |                   |
| PBES2-HS256+A128KW             |                | X            |                      |                                 |                   |
| PBES2-HS384+A192KW             |                | X            |                      |                                 |                   |
| PBES2-HS512+A256KW             |                | X            |                      |                                 |                   |
| RSA1\_5                        | X              |              |                      |                                 |                   |
| RSA-OAEP                       | X              |              |                      |                                 |                   |
| RSA-OAEP-256                   | X              |              |                      |                                 |                   |
| A128GCMKW                      |                | X            |                      |                                 |                   |
| A192GCMKW                      |                | X            |                      |                                 |                   |
| A256GCMKW                      |                | X            |                      |                                 |                   |

### Compatibility table between Key Management Modes:

| Key Management Mode             | Key Encryption | Key Wrapping | Direct Key Agreement | Key Agreement with Key Wrapping | Direct Encryption |
| ------------------------------- | -------------- | ------------ | -------------------- | ------------------------------- | ----------------- |
| Key Encryption                  | YES            | YES          | NO                   | YES                             | NO                |
| Key Wrapping                    | YES            | YES          | NO                   | YES                             | NO                |
| Direct Key Agreement            | NO             | NO           | NO                   | NO                              | NO                |
| Key Agreement with Key Wrapping | YES            | YES          | NO                   | NO                              | NO                |
| Direct Encryption               | NO             | NO           | NO                   | NO                              | NO                |


---

# 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://web-token.spomky-labs.com/2.x/advanced-topics-1/encrypted-tokens-and/multiple-recipients.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.
