Key management
About this lesson
Ciphers are public; the key is the secret. Follow one small file through envelope encryption in Python: a random data key encrypts it, a key-encryption key held by a key management service (KMS) wraps that data key, and the wrapped key is stored beside the data. Then read it back, rotate to a new key without re-encrypting the file, and safely destroy the old key. Along the way, see the most common mistake (keys in code or beside the data) and its fix.
The key is the secret
Ciphers like AES are public: anyone can read how they work. Only the key is secret, so whoever holds the key can read everything. Key management means keeping keys safe for their whole life, from creation to destruction.
cipher public (AES is a published standard)
key the only secret
key management create, store, use, rotate, destroy,
safely, for every keyRead the lesson step by step
The key is the secret
Ciphers like AES are public: anyone can read how they work. Only the key is secret, so whoever holds the key can read everything. Key management means keeping keys safe for their whole life, from creation to destruction.
cipher public (AES is a published standard) key the only secret key management create, store, use, rotate, destroy, safely, for every keyWhere keys must not live
A common mistake is keeping a key where code or data lives: typed into source code, or saved beside the files. One leak then exposes both. The fix: keep keys in a key management service (KMS), ideally backed by a hardware security module (HSM).
mistake key = "..." written in the source code key file saved next to the data why bad whoever sees the repo or copies the disk gets the key and the data together fix a KMS holds the key; code asks it to act OWASP "never committed to source code repositories"Create: random keys
Keys must come from a secure random generator, never from a password or a pattern. Line 1 makes the key-encryption key (KEK); line 2 makes a data-encryption key (DEK). Each is 256 bits: 32 random bytes.
line 1 kek: 32 random bytes (a real KMS makes it inside and never hands it out; here it stands in) line 2 dek: 32 random bytes, one per file 256 bits = 32 bytes = an AES-256 key never a password, a date, a counter as a keyUse the DEK on the data
Line 3 picks a fresh 12-byte nonce, a number used once. Line 4 encrypts the file with the DEK using AES-GCM. The 9-byte file becomes 25 bytes: the scrambled data plus a 16-byte tag that detects any change.
doc b"pay: 4200" 9 bytes nonce 12 random bytes, never reused with this key ct 9 bytes scrambled + 16-byte tag = 25 bytes tag decrypting fails if anything was changedWrap the DEK with the KEK
Line 5 encrypts the DEK itself with the KEK. This is called wrapping, and the pattern is envelope encryption. The 32-byte DEK becomes 40 bytes. In a real system the KMS wraps it inside, so the KEK never leaves.
wrap encrypt a key with another key (AES Key Wrap: NIST SP 800-38F, RFC 3394) in dek 32 bytes out wrapped 40 bytes (32 + an 8-byte check) envelope data under the DEK, DEK under the KEKStore the wrapped key
Line 6 drops the plain DEK. The ciphertext, nonce and wrapped DEK are stored together. Anyone who copies the disk gets nothing readable: to unwrap the DEK they would also need the KMS.
line 6 del dek: the app keeps no plain copy stored ct (25 B) + nonce (12 B) + wrapped (40 B) stolen disk copy alone: unreadable needed the KMS, which checks who is askingUse: unwrap, then decrypt
To read the file, the app sends the wrapped DEK to the KMS (line 7). The KMS checks the app is allowed, logs the request, and returns the DEK. Line 8 decrypts and prints b'pay: 4200'.
line 7 wrapped (40 B) → KMS → dek (32 B) KMS is this app allowed? log who and when line 8 decrypt → b'pay: 4200' least privilege: only this app may unwrapRotate: a new KEK
Each key has a cryptoperiod: a set time it may be used. When it ends, line 9 makes KEK v2 and line 10 re-wraps the DEK with it. Only the 40-byte wrapped key changes; the file is not re-encrypted.
cryptoperiod how long a key may be used (NIST SP 800-57) line 9 kek2: KEK v2, now the active key line 10 re-wrap: still 40 bytes, now under v2 KEK v1 deactivated: wraps nothing new the 25-byte ciphertext is untouchedDestroy the old key
Once nothing needs KEK v1, the KMS destroys it and every copy. That cannot be undone: data wrapped only by it is unreadable for good. So re-wrap first, then destroy. NIST calls doing it on purpose cryptographic erase.
check nothing is still wrapped by v1 (OWASP: re-wrap DEKs before destroying the old KEK) destroy v1 and all copies: backups, archives, memory result our file: readable (wrapped by v2) on purpose cryptographic erase (NIST SP 800-88)The whole life of a key
Every key follows the same path: create, store, use, rotate, destroy. Write down who may use each key and for what, log every use, and plan for a leak: stop using the key, make a new one, re-wrap, check the logs.
1 Create 2 Store 3 Use 4 Rotate 5 Destroy policy who may use which key, for what logs every wrap and unwrap leak revoke → new key → re-wrap → review logs
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A developer puts an AES key directly in the app's source code and pushes it to the Git repo. What is the problem?
The key is the only secret. Repos are copied, shared and leaked, so OWASP says keys should never be committed to source code. Keep them in a KMS or HSM instead.
02In envelope encryption, what encrypts the file itself?
A DEK encrypts the data. The KEK only encrypts (wraps) the DEK, and it stays inside the KMS.
03In the example, a 32-byte DEK is wrapped with AES Key Wrap. How long is the wrapped key?
AES Key Wrap (NIST SP 800-38F, RFC 3394) adds one 8-byte block that is checked on unwrapping, so 32 bytes become 40.
04The KEK's cryptoperiod ends. With envelope encryption, what has to change?
Rotation makes a new KEK and re-wraps the DEKs. The large ciphertext is untouched, which is why envelope encryption makes rotation cheap.
05When is it safe to destroy the old KEK v1?
Destroying a key cannot be undone: data still wrapped only by it would be unreadable for good. OWASP says re-wrap stored DEKs first, then destroy the old KEK and all its copies.
Glossary
- Key management
- Handling cryptographic keys safely for their whole life: creating, storing, distributing, using, rotating and destroying them, with rules for who may use each key.
- Key management service (KMS) and HSM
- A KMS is a service that holds keys and performs operations with them (such as wrapping and unwrapping) so the keys never leave it; it checks who is asking and logs every use. A hardware security module (HSM) is tamper-resistant hardware that protects keys inside a KMS or on its own.
- DEK and KEK
- A data-encryption key (DEK) encrypts the data. A key-encryption key (KEK), also called a key-wrapping key, encrypts other keys.
- Key wrapping and envelope encryption
- Wrapping is encrypting a key with another key. Envelope encryption encrypts data with a DEK, wraps the DEK with a KEK, and stores the wrapped DEK beside the data.
- Nonce
- A number used once. AES-GCM needs a fresh one for every encryption with the same key; it is not secret and is stored with the ciphertext.
- Cryptoperiod and rotation
- A cryptoperiod is the time a key is authorised for use. Rotation replaces a key with a new one when its cryptoperiod ends, or sooner if it may have leaked.
- Key destruction and cryptographic erase
- Destroying a key removes it and all its copies so it can never be used again. Doing it on purpose so encrypted data can never be read is called cryptographic erase.
Notes and sources
- The full program, runnable with Python 3 and the cryptography package: import os ⏎ from cryptography.hazmat.primitives.ciphers.aead import AESGCM ⏎ from cryptography.hazmat.primitives.keywrap import aes_key_wrap, aes_key_unwrap ⏎ doc = b"pay: 4200" ⏎ kek = AESGCM.generate_key(256) ⏎ dek = AESGCM.generate_key(256) ⏎ nonce = os.urandom(12) ⏎ ct = AESGCM(dek).encrypt(nonce, doc, None) ⏎ wrapped = aes_key_wrap(kek, dek) ⏎ del dek ⏎ dek = aes_key_unwrap(kek, wrapped) ⏎ print(AESGCM(dek).decrypt(nonce, ct, None)) ⏎ kek2 = AESGCM.generate_key(256) ⏎ wrapped = aes_key_wrap(kek2, dek) It prints b'pay: 4200'.
- In the code, kek is a plain Python value so the example runs on its own. In a real system the KEK never leaves the KMS or HSM: the app sends the DEK to be wrapped or unwrapped, and the service does it inside.
- del removes the name dek, but Python cannot promise the bytes are wiped from memory at once. Key management services and HSMs handle wiping (zeroization) for you; that is one more reason to let them hold keys.
- The five stages are a simplification. NIST SP 800-57 names the key states pre-activation, active, suspended, deactivated, compromised and destroyed; a deactivated key may still decrypt old data but must not protect new data.
- AES-GCM needs a nonce that is never reused with the same key; a fresh random 12-byte nonce per encryption is the usual choice. The nonce is not secret and is stored with the ciphertext.
- This lesson is about defence. It names mistakes only to show their fix, and never how to attack a real system.
- NIST SP 800-57 Part 1 Rev. 5: Recommendation for Key Management, Part 1 - General (May 2020)
- NIST CSRC Glossary: cryptoperiod
- NIST SP 800-133 Rev. 2: Recommendation for Cryptographic Key Generation
- NIST SP 800-38F: Recommendation for Block Cipher Modes of Operation: Methods for Key Wrapping
- NIST SP 800-38D: Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC
- NIST SP 800-88 Rev. 2: Guidelines for Media Sanitization (September 2025)
- RFC 3394: Advanced Encryption Standard (AES) Key Wrap Algorithm
- OWASP Cheat Sheet Series: Key Management Cheat Sheet
- CISA: CISA and NSA Release Cybersecurity Information Sheets on Cloud Security Best Practices (March 7, 2024)
- NSA and CISA: Use Secure Cloud Key Management Practices (Cybersecurity Information Sheet, March 2024)
- cryptography (Python): Key wrapping
- cryptography (Python): Authenticated encryption (AESGCM)
Quiz Unlocks after the last step.