← Concepts

阅读中文版本

Key Rotation

Key rotation replaces cryptographic keys on a schedule or after an incident while keeping old data decryptable. Versioned key IDs and automated re-encryption make rotation safe.

security

Definition

Key rotation is the controlled replacement of a cryptographic key with a new key, combined with versioning and re-encryption or rewrapping so that data protected by the old key remains available. It limits the amount of data exposed by any single key compromise and supports algorithm upgrades.

Context and scope

Rotation applies to data encryption keys, key encryption keys, signing keys, TLS certificates, and API secrets. It is a core operational control in key management and incident response.

Common misconceptions

  • Rotating keys removes an attacker who already has persistent access to the system
  • Rotating a key encryption key requires decrypting and rewriting every stored record

Anti-patterns

  • Deleting an old key before all data and backups are re-encrypted or expired
  • Relying on manual rotation without monitoring or a tested rollback plan

Related concepts

Related examples

Authoritative source

https://csrc.nist.gov/pubs/sp/800/57pt1r5/final

How key rotation works

Rotation replaces a cryptographic key with a new one while keeping data encrypted under the old key decryptable. Production systems version keys and tag ciphertext with a key ID. The application decrypts with the retired key during an overlap window, and a background job re-encrypts or rewraps data under the new key. For envelope encryption, rotating the KEK only requires rewrapping data encryption keys, not re-encrypting every record.

When to use it

Rotate on a defined schedule, after suspected compromise, when personnel with key access leave, and when algorithms or parameters become obsolete. Certificates and TLS keys rotate more frequently than data keys. Rotation limits the blast radius of a leak, because an attacker who steals one key cannot read data written after rotation. It complements revocation, which stops future use immediately. Compared with revocation, rotation limits future exposure but does not stop current use.

Trade-offs

Rotation adds operational complexity and failure modes: a missing key ID, a cache holding old keys, or a partial re-encryption can cause outages. Very frequent rotation increases cost without much benefit unless automation is mature. Old backups may still need retired keys, so deletion must wait for the backup retention window. Test rotation in staging and monitor decryption errors after each run.

Example

const [version, payload] = ciphertext.split(':', 2);
const key = version === 'v2' ? keys.current : keys.previous;
const plaintext = decrypt(key, payload);

Write new records with a v2: prefix and schedule a job to rewrite old rows. Retire v1 only after the re-encryption report shows zero remaining rows.

Rotation does not remove an attacker who already has persistent access; investigate and revoke credentials too. Rotating a KEK does not require decrypting every record when data keys are rewrapped. Manual rotation does not scale and is often skipped. Related concepts include encryption at rest, secret management, and TLS.