Bekzod Erkinov
Listen to Article
Loading...Encrypt User Data at Rest: Envelope Encryption and Key Rotation
Turning on disk encryption doesn't protect you from a leaked database dump or a SQL injection, because the database decrypts everything for any query it serves. This post builds field-level envelope encryption in Python, rotates the master key without downtime, makes an encrypted column searchable with a blind index, and does the KMS cost arithmetic from the published AWS and Google Cloud prices.
Everything was run on one Windows laptop (Python 3.12.10, cryptography 50.0.2, SQLite). I haven't operated this at production scale, so I give per-operation numbers I measured and label extrapolations as arithmetic. All the scripts are shown below in full, so you can reproduce every printed output.
Which layer encrypts which field
There are three layers, and they defend against different things:
| Layer | Stops | Doesn't stop |
|---|---|---|
| Disk / volume encryption | stolen disk, discarded hardware | anyone who can query the database |
| Database TDE or managed-service encryption | stolen backups, snapshots | SQL injection, a leaked app credential, a dump taken through the DB |
| Field-level (application) encryption | all of the above, plus DB-level read access | a compromised app server (it holds keys at runtime) |
A decision rule you can copy:
| Field | Example columns | Layer |
|---|---|---|
| Harmful in a breach, never searched | ssn, passport_no, oauth_refresh_token, diagnosis |
Field-level envelope encryption |
| Harmful in a breach, looked up by equality | email, phone |
Field-level + blind index |
Not sensitive alone, or needed for LIKE/sorting/joins |
display_name, created_at, country |
Disk/TDE only |
Field-level encryption costs you SQL: you can't LIKE, sort or index ciphertext.
Where does the KEK live at runtime? Pick one:
- KMS call per value if you decrypt a few values per request and you need revoking IAM access to stop an attacker immediately. Every read pays a network round trip.
- KEK cached in process memory, TTL 5 minutes if you decrypt many values per request. The TTL is my choice, not a documented value. As arithmetic: one process unwraps at most 12 times an hour per KEK instead of once per read. The threat you accept: someone who can read your process memory gets the raw KEK for up to the TTL, not just the plaintexts they can already reach, and revoking IAM access takes up to 5 minutes to bite.
How to encrypt user data at rest in Python
The idea, as AWS describes it: encrypt data with a data key (DEK), then encrypt the DEK under a key-encryption key (KEK). Store the wrapped DEK next to the data. Google's guidance adds the rules I followed: generate DEKs locally, use a new DEK on every write, and never share a DEK between two users.
envelope.py (it also holds the rotation helper used later):
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
class Keyring:
"""KEKs by id. In production these live in a KMS; here they're in memory."""
def __init__(self):
self.keks, self.current = {}, None
def add(self, kid):
self.keks[kid] = AESGCM.generate_key(256)
self.current = kid
def _wrap(kr, kid, dek, aad):
n = os.urandom(12)
return n, AESGCM(kr.keks[kid]).encrypt(n, dek, aad + kid.encode())
def _unwrap(kr, env, aad):
return AESGCM(kr.keks[env["kek"]]).decrypt(env["n2"], env["wdek"], aad + env["kek"].encode())
def encrypt_field(kr, plaintext: bytes, aad: bytes):
dek = AESGCM.generate_key(256) # fresh DEK per value
n1 = os.urandom(12)
n2, wdek = _wrap(kr, kr.current, dek, aad)
return {"kek": kr.current, "n1": n1, "ct": AESGCM(dek).encrypt(n1, plaintext, aad),
"n2": n2, "wdek": wdek}
def decrypt_field(kr, env, aad: bytes):
return AESGCM(_unwrap(kr, env, aad)).decrypt(env["n1"], env["ct"], aad)
def rewrap(kr, env, aad: bytes):
"""Move the DEK to the current KEK. The payload ciphertext is untouched."""
if env["kek"] == kr.current:
return env
n2, wdek = _wrap(kr, kr.current, _unwrap(kr, env, aad), aad)
return {**env, "kek": kr.current, "n2": n2, "wdek": wdek}
if __name__ == "__main__":
kr = Keyring(); kr.add("kek-1")
aad = b"users.ssn:42" # table.column:row id
env = encrypt_field(kr, b"078-05-1120", aad)
print("roundtrip:", decrypt_field(kr, env, aad))
try: decrypt_field(kr, env, b"users.ssn:43") # same ciphertext pasted into row 43
except Exception as e: print("row-swap ->", type(e).
Unlock Premium Content
You've read 30% of this article
What's in the full article
- Complete step-by-step implementation guide
- Working code examples you can copy-paste
- Advanced techniques and pro tips
- Common mistakes to avoid
- Real-world examples and metrics
Don't have an account? Start your free trial
Join 10,000+ developers who love our premium content
Keep reading
Production-Ready Authentication: A Deep Dive into JWT, OAuth2, and Session Security
21 min · 1,385 views
Artificial IntelligenceAdvanced Security Measures for Protecting Against Sophisticated Threats
18 min · 1,384 views
Web DevelopmentMastering Multi-Tenant SaaS Architecture Patterns for Enterprise Applications
27 min · 1,027 views
Bekzod Erkinov
AuthorFounder of NextGenBeing. Software engineer working with Laravel, Python, and cloud infrastructure. Writes about patterns that actually hold up in production. Based in Tashkent, Uzbekistan.
Get the AI-Assisted Developer's Field Guide
The workflow, prompts, and tools I use to ship faster with AI — free when you subscribe. Plus new deep-dives in your inbox. No spam, unsubscribe anytime.
Comments (0)
Please log in to leave a comment.
Log In