We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Key Management and Rotation

Encrypted data is only as safe as the key used to encrypt it. But a single unversioned key means replacing it makes every value encrypted with the old key unreadable!

Instead, use a keyring. This isn't as complex as it sounds: start with one key and call it version 1 (v1):

DATA_ENCRYPTION_ACTIVE_VERSION=v1
DATA_ENCRYPTION_KEY_V1=<64 hex characters>

Then, whenever you encrypt a value, store its key version with the ciphertext:

Ciphertext Key Version
0xdeadbeef... v1
0xfeedface... v1
... ...

Rotate Without Losing Data

When you need to rotate your key, add a new one and change the active version:

DATA_ENCRYPTION_ACTIVE_VERSION=v2
DATA_ENCRYPTION_KEY_V1=<old key>
DATA_ENCRYPTION_KEY_V2=<new key>

Your application should always decrypt with the version recorded on the payload and encrypt with the current active version. That way, old ciphertext remains readable after rotation.

If a key was compromised, rotate it and re-encrypt live data as soon as you can. This limits future exposure, but it can't protect ciphertext an attacker already copied with the old key.

Losing an encryption key can mean permanently losing the data it protects. Treat rotation, backup recovery, access control, and key retirement as one lifecycle.

TOTP Secrets

A TOTP secret is a long-lived authentication credential. Bearly Secure must decrypt it to verify codes, so hashing isn't an option. The keyring can instead encrypt active and pending secrets at rest.

Bearly Secure already routes TOTP secrets through the keyring and includes a transactional startup migration for existing plaintext records. The keyring's Encrypt and Decrypt methods are still insecure pass-throughs, and the server doesn't require or inject a configured keyring yet.

Assignment

Bearly Secure needs to rotate encryption keys without losing protected data. Complete and activate the versioned keyring so TOTP secrets are encrypted at rest.

Run and submit the CLI tests from the project root.