> ## Documentation Index
> Fetch the complete documentation index at: https://docs.conduktor.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Change the encryption algorithm in Gateway

> Move Kafka data encrypted by Conduktor Gateway to a stronger algorithm, for new records and for records already stored.

This tutorial starts with a topic, `orders`, that an [encryption Interceptor](https://docs.conduktor.io/guide/conduktor-concepts/interceptors.md) already encrypts. You change the algorithm, then decide what to do with the records already in `orders`.

## Change the algorithm

Set `algorithm` in the encryption Interceptor and apply it with the Conduktor CLI or the API. Every Gateway node applies the change without a restart. See [choosing an encryption algorithm](https://docs.conduktor.io/guide/reference/data-security.md#choosing-an-encryption-algorithm).

```yaml theme={null}
apiVersion: gateway/v2
kind: Interceptor
metadata:
  name: full-payload-encrypt
spec:
  pluginClass: io.conduktor.gateway.interceptor.EncryptPlugin
  priority: 100
  config:
    # Covers orders, and orders-v2 if you re-encrypt
    topic: "orders.*"
    kmsConfig:
      vault:
        uri: http://vault:8200
        token: $${VAULT_TOKEN}
    recordValue:
      payload:
        keySecretId: vault-kms://vault:8200/transit/keys/payload-key
        # The algorithm to move to
        algorithm: AES256_GCM
```

For [schema-based encryption](https://docs.conduktor.io/guide/tutorials/configure-encryption.md#schema-based-field-level-encryption), set `defaultAlgorithm` in the Interceptor and `conduktor.algorithm` on any field in the schema that sets its own.

After the change:

* New records in `orders` are encrypted with the new algorithm, under a new data encryption key (DEK).
* Records already in `orders` keep the old algorithm. Gateway still decrypts them, because it stores the settings it used with each record: in a record header, or in a topic if you set `externalStorage: true` before Gateway 3.18.
* Your consumers read old and new records with no change to the decrypt Interceptor or your clients.

## Do you need to act on older records?

| If the older records are | What to do |
| - | - |
| Deleted by retention | Nothing. Kafka will delete them after the retention period ends. |
| Kept: compacted or unlimited retention | [Re-encrypt them](#re-encrypt-older-records). |
| Not needed by anyone | [Crypto shred](https://docs.conduktor.io/guide/tutorials/crypto-shredding.md) their keys. After an algorithm change, Gateway stores one key per algorithm for each key ID, so shred all of them. |

## Re-encrypt older records

Copy `orders` through Gateway into a new topic, `orders-v2`. Gateway decrypts the records as they're read and encrypts them with the new algorithm as they're written. Then give the copy the name `orders`, so your clients don't change.

1. Create `orders-v2` with the same number of partitions as `orders`, so records with the same key stay in order.
2. Check that the encryption Interceptor covers `orders-v2`.
3. Run a copy job that reads `orders` through Gateway with a [decrypt Interceptor](https://docs.conduktor.io/guide/tutorials/configure-encryption.md#decrypt-the-full-payload) and writes to `orders-v2` through Gateway. For MirrorMaker 2, see the [sample configuration](#sample-mirrormaker-2-configuration).
4. Stop your producers and consumers, let the copy job catch up, then stop it. Kafka can reset a consumer group's offsets only while none of its consumers are running.
5. Delete `orders`, then create an [alias topic](https://docs.conduktor.io/guide/conduktor-concepts/logical-topics.md#alias-topics) named `orders` that points to `orders-v2`. Keep this order: Gateway accepts an alias with the same name as an existing topic, and deleting `orders` would then delete `orders-v2`.
6. Restart your producers.
7. Reset each consumer group to the earliest offset, then start your consumers. Deleting `orders` deleted the groups' committed offsets, so consumers using the default `auto.offset.reset=latest` would skip every copied record. Consumers read again the records they processed before the copy.

   ```bash theme={null}
   # Run once for each consumer group, through Gateway
   kafka-consumer-groups --bootstrap-server gateway:6969 \
     --group <group> --topic orders \
     --reset-offsets --to-earliest --execute
   ```

### Sample MirrorMaker 2 configuration

Run it with `connect-mirror-maker.sh mm2.properties --clusters dst`.

```properties theme={null}
# Both sides connect through Gateway to the same Kafka cluster
clusters = src, dst
src.bootstrap.servers = gateway:6969
dst.bootstrap.servers = gateway:6969

# Copy orders one way only
src->dst.enabled = true
dst->src.enabled = false
src->dst.topics = orders

# Keep the topic name, then rename orders to orders-v2
replication.policy.class = org.apache.kafka.connect.mirror.IdentityReplicationPolicy
src->dst.transforms = rename
src->dst.transforms.rename.type = org.apache.kafka.connect.transforms.RegexRouter
src->dst.transforms.rename.regex = orders
src->dst.transforms.rename.replacement = orders-v2

# Copy records only: no heartbeats, checkpoints, or offset, config or ACL syncs
src->dst.emit.heartbeats.enabled = false
src->dst.emit.checkpoints.enabled = false
src->dst.sync.group.offsets.enabled = false
src->dst.sync.topic.configs.enabled = false
src->dst.sync.topic.acls.enabled = false

# Find topics once, at start: source and target are the same cluster, so
# rescanning would copy MirrorMaker's own topics
src->dst.refresh.topics.enabled = false
src->dst.refresh.groups.enabled = false
```

## Check your master key

Gateway wraps each DEK with a key in your Key Management Service (KMS).

* To rotate that key, see [key rotation](https://docs.conduktor.io/guide/reference/data-security.md#key-rotation).
* If you use the [Gateway KMS](https://docs.conduktor.io/guide/reference/data-security.md#crypto-shredding), keep `kmsConfig.gateway.masterKeyId` the same. Changing it makes older records unreadable.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.