Encrypt the file or protect its data key?

The essentials in 30 seconds

before you dive into the details
Picture this

Small safe, large box

Box key

Data key

Encrypts the payload locally.

Small safe

KMS key protection

Protects the wrapped data key.

Where the analogy stops: The KMS key is distinct from the generated data key. The plaintext data key is needed briefly, not stored with the file.
01Mental model

A small safe protects the key to a large box. The box is encrypted locally; its wrapped key can travel beside it.

02Decision

Use envelope encryption for large payloads. KMS protects the data key while the application encrypts the payload locally.

03In your code

GenerateDataKey returns plaintext and encrypted key copies. Store the encrypted copy; discard plaintext key material after use.

Exam trap

EncryptionContext is authenticated, non-secret metadata. It must match on decrypt and must not contain secrets.

One sentence to remember

Encrypt the payload locally and keep only the wrapped data key with it.

1 media item5 rule steps5 scenarios6 flashcards

Focus mode