01 / 20

Key
Management

Understand how cryptographic keys are generated, stored, and rotated to keep data secure throughout its lifecycle.

Cyber Security

What Is a Cryptographic Key?

A cryptographic key is a string of data used by an algorithm to encrypt or decrypt information. Think of it as the secret ingredient that transforms readable plaintext into unreadable ciphertext, and vice versa.

The Key Lifecycle

Generation Storage Distribution Rotation Revocation Destruction Key Lifecycle Every key passes through these stages

Why Key Management Matters

Confidentiality
Keys protect sensitive data. If a key leaks, an attacker can decrypt everything it was used to encrypt.
Integrity
Digital signatures rely on private keys. A compromised signing key means forged documents go undetected.
Compliance
Regulations like HIPAA, PCI-DSS, and GDPR require documented key management procedures and audit trails.

Key Generation: The Foundation

Secure key generation depends on true randomness. Keys must be unpredictable to an adversary. The strength of a key is often measured by its entropy: the number of bits of randomness it contains.

CSPRNG Cryptographically Secure Pseudo-Random Number Generator Entropy Source Secure Key Hardware noise, OS entropy pools, or dedicated HSM modules feed the generator

Secure Key Storage

Once generated, keys must be stored so they remain confidential and available when needed. There are several approaches, each with trade-offs:

HSM
Hardware Security Module: a dedicated physical device that stores keys in tamper-resistant hardware. Highest security.
Key Vault
Cloud-managed service (e.g., AWS KMS, Azure Key Vault). Access controlled by IAM policies and audit logs.
Encrypted File
Keys stored in encrypted files on disk, protected by a master key or passphrase. Least expensive but most vulnerable.

Key Rotation: Limiting Exposure

Key rotation means replacing an active key with a new one at regular intervals. Even if an attacker discovers an old key, they can only access data encrypted during that key's active period.

Key v1 Key v2 Key v3 Jan - Mar Apr - Jun Jul - Sep Quarterly Key Rotation Breach window limited

Symmetric vs Asymmetric Keys

Symmetric Asymmetric
Keys Used One shared secret key Public + Private key pair
Speed Fast Slower
Key Distribution Difficult (must share secretly) Easy (public key is shared openly)
Examples AES, ChaCha20 RSA, ECC, Ed25519
Key Length 128 - 256 bits 2048 - 4096 bits (RSA)
Use Case Bulk data encryption Key exchange, signatures

The Core Principle

Kerckhoffs' Principle Security depends on the key, not the algorithm Generate Strong randomness Protect Secure storage Rotate Limit exposure Together, these three pillars form secure key management

Separation of Duties

No single person should have access to both the key and the encrypted data. Splitting responsibilities across roles reduces the risk of insider threats and accidental exposure.

Key Custodian
Responsible for generating and managing keys. Cannot access the production data those keys protect.
Data Owner
Uses encrypted data in daily operations. Cannot export or view the raw key material itself.

How to Apply Key Management

1
Identify what needs protection. Classify data by sensitivity and determine which systems require encryption.
2
Choose your algorithm and key length. Select AES-256 for symmetric or RSA-2048+/ECC for asymmetric based on performance and security needs.
3
Generate keys with a CSPRNG. Use hardware entropy sources or approved libraries: never roll your own randomness.
4
Store keys in a secure vault or HSM. Separate key storage from data storage. Enforce access control and logging.
5
Define a rotation schedule. Rotate keys at regular intervals and immediately after any suspected compromise.
6
Plan for revocation and destruction. Maintain a process to revoke compromised keys and securely destroy retired ones.

Example: TLS on a Web Server

Browser Server 1. Server sends public key + certificate 2. Browser encrypts session key with public key 3. AES session key encrypts all data TLS Handshake (Simplified)

Example: Database Encryption at Rest

Master Key (HSM) DEK: Users Table DEK: Orders Table DEK: Logs Table

Example: Responding to a Key Compromise

1
Identify the compromised key. Determine which key was exposed and what data it protects.
2
Revoke the key immediately. Add it to a Certificate Revocation List (CRL) or use OCSP to notify all parties.
3
Generate and distribute a new key. Replace the compromised key using your established generation and distribution procedures.
4
Re-encrypt affected data. All data encrypted with the old key must be re-encrypted with the new key.
5
Destroy the old key. Securely wipe the compromised key from all storage locations (including backups).

Common Mistakes

✕ Hardcoding Keys
Embedding keys directly in source code. Anyone who reads the code (or the compiled binary) gains access.
✕ No Rotation Policy
Using the same key indefinitely. A single breach exposes all data ever encrypted with that key.
✕ Weak Randomness
Generating keys with predictable pseudo-random functions (e.g., Math.random()). Attackers can reproduce the key.
✕ Storing Keys with Data
Placing the encryption key in the same database it protects. If the database is breached, the key is too.
✕ No Destruction Plan
Failing to securely destroy retired keys. Old keys on forgotten backups can be recovered by attackers.

Key Management Summary

Stage Goal Best Practice
Generation Unpredictability Use CSPRNG + HSM
Storage Confidentiality HSM or managed key vault
Distribution Secure delivery Asymmetric key exchange
Rotation Limit exposure window Scheduled + event-driven
Revocation Immediate invalidation CRL / OCSP
Destruction Eliminate residual risk Cryptographic erasure

Question 1 of 5

What is the primary purpose of key rotation?

Question 2 of 5

Which of the following is the most secure method for storing cryptographic keys?

Question 3 of 5

In a hybrid cryptosystem (like TLS), what role does the asymmetric key pair play?

Question 4 of 5

A developer uses Math.random() in JavaScript to generate an encryption key. Why is this a critical security flaw?

Question 5 of 5

Kerckhoffs' Principle states that the security of a cryptosystem should depend entirely on: