> For the complete documentation index, see [llms.txt](https://docs.seismic.systems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.seismic.systems/clients/python/precompiles/aes-gcm-encrypt.md).

# aes\_gcm\_encrypt

On-chain AES-256-GCM encryption

Encrypt bytes with the AES-GCM precompile at `0x66`.

## Overview

`aes_gcm_encrypt()` and `async_aes_gcm_encrypt()` accept a 32-byte key, a 12-byte nonce (or nonce integer), and plaintext bytes.

The returned ciphertext includes the 16-byte authentication tag.

## Signature

```python
def aes_gcm_encrypt(
    w3: Web3,
    *,
    aes_key: Bytes32,
    nonce: int | EncryptionNonce,
    plaintext: bytes,
) -> HexBytes

async def async_aes_gcm_encrypt(
    w3: AsyncWeb3,
    *,
    aes_key: Bytes32,
    nonce: int | EncryptionNonce,
    plaintext: bytes,
) -> HexBytes
```

## Parameters

| Parameter   | Type                                                                                                              | Required | Description                                            |
| ----------- | ----------------------------------------------------------------------------------------------------------------- | -------- | ------------------------------------------------------ |
| `w3`        | `Web3` / `AsyncWeb3`                                                                                              | Yes      | Connected Seismic client                               |
| `aes_key`   | [`Bytes32`](https://docs.seismic.systems/clients/python/api-reference/types/bytes32.md)                           | Yes      | 32-byte AES key                                        |
| `nonce`     | `int` or [`EncryptionNonce`](https://docs.seismic.systems/clients/python/api-reference/types/encryption-nonce.md) | Yes      | 12-byte nonce (or int converted to 12-byte big-endian) |
| `plaintext` | `bytes`                                                                                                           | Yes      | Data to encrypt                                        |

## Returns

| Type       | Description                                 |
| ---------- | ------------------------------------------- |
| `HexBytes` | Ciphertext with authentication tag appended |

## Examples

### Basic Usage

```python
import os
from seismic_web3 import Bytes32, create_public_client
from seismic_web3 import precompiles as sp

w3 = create_public_client("https://testnet-1.seismictest.net/rpc")

key = Bytes32(os.urandom(32))
plaintext = b"secret message"

ciphertext = sp.aes_gcm_encrypt(w3, aes_key=key, nonce=1, plaintext=plaintext)
print(ciphertext.hex())
```

### With `EncryptionNonce`

```python
import os
from seismic_web3 import EncryptionNonce
from seismic_web3 import precompiles as sp

nonce = EncryptionNonce(os.urandom(12))
ciphertext = sp.aes_gcm_encrypt(w3, aes_key=key, nonce=nonce, plaintext=b"hello")
```

### Async Usage

```python
import os
from seismic_web3 import create_async_public_client
from seismic_web3 import Bytes32
from seismic_web3 import precompiles as sp

async def main():
    w3 = create_async_public_client("https://testnet-1.seismictest.net/rpc")
    key = Bytes32(os.urandom(32))
    ct = await sp.async_aes_gcm_encrypt(w3, aes_key=key, nonce=1, plaintext=b"async")
    print(ct.hex())
```

### Encrypt/Decrypt Round Trip

```python
from seismic_web3 import precompiles as sp

ct = sp.aes_gcm_encrypt(w3, aes_key=key, nonce=42, plaintext=b"round trip")
pt = sp.aes_gcm_decrypt(w3, aes_key=key, nonce=42, ciphertext=bytes(ct))
assert bytes(pt) == b"round trip"
```

## Gas Cost

The SDK uses:

```python
from math import ceil

total_gas = 1000 + ceil(len(plaintext) / 16) * 30
```

Examples:

* `len(plaintext)=0`: `1000`
* `len(plaintext)=16`: `1030`
* `len(plaintext)=17`: `1060`

## Notes

* Never reuse a nonce with the same key.
* `int` nonces are encoded to 12-byte big-endian values.
* Returned bytes are `ciphertext || tag`.

## See Also

* [AES-GCM Encrypt — Precompiles Reference](https://docs.seismic.systems/reference/precompiles/aes-gcm-encrypt.md)
* [aes-gcm-decrypt](https://docs.seismic.systems/clients/python/precompiles/aes-gcm-decrypt.md)
* [ecdh](https://docs.seismic.systems/clients/python/precompiles/ecdh.md)
* [Bytes32](https://docs.seismic.systems/clients/python/api-reference/types/bytes32.md)
* [EncryptionNonce](https://docs.seismic.systems/clients/python/api-reference/types/encryption-nonce.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://docs.seismic.systems/clients/python/precompiles/aes-gcm-encrypt.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.
