Send an encrypted message
A sender wants to send shipping instructions to the recipient for order 42. The recipient keeps the secret key and gives the sender the public key through a channel that establishes whose key it is. The tutorial plays both roles in one process.
Setup
From a checkout of this repository, use Rust 1.88 or later and Python 3.10 or later. Create a virtual environment:
python -m venv .venv
Activate it with .\.venv\Scripts\Activate.ps1 in PowerShell or source .venv/bin/activate in bash. Then build the Python binding for this checkout and run the example.
python -m pip install .
python docs/examples/encrypt.py
Seal and open the order
from quantaegis import DecryptionError, Envelope
# Recipient
public_key, secret_key = Envelope.generate_keypair("ml-kem-768")
# Sender
order_id = b"order=42"
packet = Envelope.seal(public_key, b"ship on Friday", aad=order_id)
# Recipient
message = Envelope.open(secret_key, packet, aad=order_id)
assert message == b"ship on Friday"
print(message.decode())
# This check demonstrates the wrong-AAD case for a locally generated packet.
try:
Envelope.open(secret_key, packet, aad=b"order=99")
except DecryptionError:
print("Rejected a different order ID")
else:
raise AssertionError("Unexpected success with different AAD")
The first call to open recovers ship on Friday. The second raises DecryptionError because order 99 has different associated data. Both parties supply the order ID as exactly the same bytes. AAD is authenticated and managed separately from the packet. Put confidential values in the encrypted body. The example catches DecryptionError specifically to demonstrate an AAD mismatch. Incoming packets can also fail the earlier policy or key-algorithm checks. Receiver error handling
ML-KEM-768 establishes a shared secret. The SDK derives an encryption key and nonce, then protects the body with AES-256-GCM by default. Anyone with the recipient’s public key can create a new packet for them. To verify a publisher’s identity, use a signature. Signing tutorial · Python API