# Message Encryption

You can protect single message by using Data Protection attributes directly in your class.

Adding `#[Sensitive]` annotation to class will tell Ecotone to treat all attributes as sensitive.

```php
use Ecotone\DataProtection\Attribute\Sensitive;
use Ecotone\DataProtection\Attribute\WithEncryptionKey;

#[Sensitive] // tells Ecotone that this message is sensitive
#[WithEncryptionKey('secondary-key')] // optional, tells Ecotone which key should be used. If not defined, Ecotone will use default key.
readonly class ChargeCreditCard
{
    public function __construct(
        // ...
    ) {
    }
}
```

With `#[Sensitive]` attribute you can annotate whole class or specific properties, which will make Ecotone to protect only those. Attribute can be used with any type: scalar, class or enum.

```php
use Ecotone\DataProtection\Attribute\Sensitive;
use Ecotone\DataProtection\Attribute\WithEncryptionKey;

#[WithEncryptionKey('secondary-key')] // optional, tells Ecotone which key should be used. If not defined, Ecotone will use default key.
readonly class ChargeCreditCard
{
    public function __construct(
        public string $walletId,
        #[Sensitive] public IbanNumber $iban,// tells Ecotone that this property is sensitive
        // ...
    ) {
    }
}
```

{% hint style="info" %}
If you use #\[Sensitive] with Event Sourcing Events, they will be persisted with encrypted data.
{% endhint %}

```php
use Ecotone\DataProtection\Attribute\Sensitive;
use Ecotone\DataProtection\Attribute\WithEncryptionKey;

#[NamedEvent('payment.credit-card-was-changed')]
readonly class CreditCardWasCharged
{
    public function __construct(
        public string $walletId,
        #[Sensitive] public IbanNumber $iban, // event payload will contain encrypted value for iban in Event Store
        // ...
    ) {
    }
}
```

### Custom Converters

Data Protection extends standard conversion and uses actual property names. If you are using custom converters for your messages, you may change name of property you want to be protected. In that case, you can pass custom name with `#[Sensitive]` attribute to tell Ecotone how to handle data properly.

```php
use Ecotone\DataProtection\Attribute\Sensitive;
use Ecotone\Messaging\Attribute\Converter;

readonly class ChargeCreditCard
{
    public function __construct(
        public string $walletId,
        #[Sensitive('iban-number')] public IbanNumber $iban, // passing custom name works only with properties
    ) {
    }
}

class ChargeCreditCardConverter
{
    #[Converter]
    public function convertFrom(ChargeCreditCard $object): array
    {
        return [
            'walletId' => $object->walletId,
            'iban-number' => $object->iban->number, // Ecotone knows to look for value under `iban-number`
        ];
    }

    #[Converter]
    public function convertTo(array $payload): ChargeCreditCard
    {
        return new ChargeCreditCard(
            walletId: $payload['walletId'],
            iban: new IbanNumber($payload['iban-number']),
        );
    }
}

```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ecotone.tech/modules/data-protection/message-encryption.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
