Quick start¶
This tutorial seals and opens data with a key that lives in your process, so it runs anywhere. Production code uses the same calls with a key service; the key services guide shows how.
1. Create a key and a protector¶
A provider holds keys. The built-in HPKE provider uses public-key cryptography (RFC 9180): the private key opens envelopes, the public key only seals them.
from cryptography_suite import EncryptionContext, Policy, Protector
from cryptography_suite.providers.hpke import (
HpkeRecipientProvider,
generate_private_key,
)
identity = generate_private_key() # X25519 by default
provider = HpkeRecipientProvider()
key = provider.add_identity("invoices", identity)
protector = Protector(provider=provider, policy=Policy(), primary_key=key)
A Protector combines providers, the keys every envelope is sealed to, and a
Policy that limits sizes, providers, and algorithms. It is immutable and
safe to share between threads.
2. Seal and open¶
context = EncryptionContext({"tenant": "acme"}, purpose="invoice-archive")
envelope = protector.seal(b"invoice 2026-0042: 1,200 EUR", context=context)
plaintext = protector.open(envelope, context=context)
assert plaintext == b"invoice 2026-0042: 1,200 EUR"
envelope.data holds the bytes to store. The envelope is not secret: it
contains the ciphertext and the data key wrapped for each recipient, never the
data key itself.
The encryption context says what the data is and who it belongs to. It is authenticated but not stored, so opening requires the same context. A database row encrypted for one tenant cannot be opened, or swapped in, as another tenant’s.
3. See what failure looks like¶
Every failure is a typed error with a stable code, and the message never reveals what was wrong with the data.
from cryptography_suite import AuthenticationError, ContextMismatchError, Envelope
try:
protector.open(envelope, context=EncryptionContext({"tenant": "globex"}))
except ContextMismatchError as error:
print(error.code) # CONTEXT_MISMATCH
tampered = bytearray(envelope.data)
tampered[-1] ^= 0x01
try:
protector.open(Envelope(bytes(tampered)), context=context)
except AuthenticationError as error:
print(error.code) # AUTHENTICATION_FAILED
Both errors carry the same message, so the text cannot be used to tell a
wrong context from tampering. Programs read the code.
4. Inspect without a key¶
metadata = protector.inspect(envelope)
print(metadata.recipients[0].key_id) # invoices
print(metadata.plaintext_size) # 28
print(metadata.authentication_status) # not_verified
inspect reads the structure without any key or provider call. It is useful
for routing and diagnostics, and it is marked not_verified: nothing in it is
authenticated until open succeeds.
5. Separate sealing from opening¶
A service that only writes data should not be able to read it. Give it the public key:
from cryptography_suite.providers.hpke import load_public_key_text, public_key_text
published = public_key_text(identity.public_key()) # cs4hpke1-x25519:...
writer_provider = HpkeRecipientProvider()
writer_key = writer_provider.add_recipient("invoices", load_public_key_text(published))
writer = Protector(provider=writer_provider, policy=Policy(), primary_key=writer_key)
sealed = writer.seal(b"invoice 2026-0043", context=context)
assert protector.open(sealed, context=context) == b"invoice 2026-0043"
The writer can seal but never open; the holder of identity opens what it
wrote.
6. Store the private key¶
from cryptography_suite.providers.hpke import load_private_key_pem, private_key_pem
pem = private_key_pem(identity, password=b"correct horse battery staple")
restored = load_private_key_pem(pem, password=b"correct horse battery staple")
Keep the PEM somewhere access-controlled, such as a secrets manager, or offline for a recovery key. In production, the primary key usually lives in a key service instead, where it cannot be exported at all.
Next steps¶
Concepts: how an envelope works and what it guarantees.
Key services: AWS KMS, Google Cloud KMS, Azure Key Vault, and HashiCorp Vault.
Rotation and recovery: escrow keys, key rotation, and disaster recovery.