Modern data encryption strategies often employ a two-layer model of key management to balance strong security with operational flexibility. Two commonly used concepts in this model are the Data Encryption Key (DEK) and the Key Encryption Key (KEK). In this post, we’ll unpack how these two keys work together, and how you might store and retrieve them effectively.

What Are DEK and KEK?

Data Encryption Key (DEK):
A DEK is the key used directly to encrypt and decrypt your data (e.g., files, database entries, or messages). Symmetric encryption algorithms such as AES (Advanced Encryption Standard) are typically used here because they’re efficient and optimized for handling large amounts of data.

Key Encryption Key (KEK):
A KEK is used to encrypt (or “wrap”) your DEK, rather than encrypting the data directly. By storing the DEK in a protected, encrypted form, you ensure that even if the encrypted DEK is compromised, it’s unreadable without the KEK.

Why Do We Separate Keys?

Efficiency:
Encrypting large data sets is best done using symmetric keys (DEKs) because they operate quickly and efficiently at scale.

Security:
The KEK ensures that DEKs are never stored in plaintext. If an attacker obtains an encrypted DEK, they can’t decrypt the actual data without the KEK.

Scalability and Flexibility:
Rotating a KEK doesn’t require re-encrypting all the data—just re-wrapping the DEKs. This modular approach makes it easier to manage large fleets of encrypted objects.

Practical Workflow

  1. Encrypting Data:

    • Generate a DEK.
    • Encrypt your data with the DEK.
    • Encrypt the DEK using the KEK.
    • Store the encrypted data along with the encrypted DEK.
  2. Decrypting Data:

    • Retrieve the encrypted data and the encrypted DEK.
    • Use the KEK to unwrap (decrypt) the DEK.
    • Use the now-unwrapped DEK to decrypt your data.

Analogy:
Think of the data as a treasure in a chest, and the DEK as the key to that chest. Instead of leaving that key out in the open, you lock it in a secure safe. The KEK is the combination to that safe. Without knowing the combination (KEK), the chest key (DEK) remains inaccessible, and so does your treasure (data).

Identifying the Right KEK: Using Key Aliases

A common question is how to know which KEK was used if you store many DEKs and data blobs. Since you’ll often have a “table” of encrypted DEKs, you need some metadata to help you fetch the right KEK.

Solution: Store a Key Alias or Key Identifier
Alongside each encrypted DEK, store a key alias or unique identifier that maps to the KEK used. When you need to decrypt, you look up this alias, fetch the corresponding KEK, and unwrap the DEK.

Example

  • Encryption Time:

    • Data is encrypted with DEK_123.
    • DEK_123 is wrapped with a KEK referenced by “KEK_Alias”.
    • You store the encrypted data, encrypted DEK_123, and “KEK_Alias” together.
  • Decryption Time:

    • Look up “KEK_Alias”.
    • Fetch the KEK that “KEK_Alias” represents.
    • Unwrap DEK_123 using the KEK.
    • Decrypt the data using the now-unwrapped DEK_123.

Why Versioning Helps

Over time, you may rotate KEKs for security reasons. By storing a key alias and possibly a version number, you can keep track of which version of the KEK was used at the time of encryption. This makes it possible to decrypt older data even after you’ve updated the KEK.

Pros and Cons

Pros:

  • Efficient Encryption: Bulk data encryption using symmetric DEKs is fast.
  • Enhanced Security: DEKs aren’t stored in plaintext; they’re always protected by a KEK.
  • Scalable Rotation: You can rotate KEKs without re-encrypting all your data.

Cons:

  • Added Complexity: Two-tier key management is more complex to implement.
  • Centralized Dependence: If the KEK or its associated alias system is compromised, attackers might gain access to multiple DEKs.

Key Takeaways

  • Use DEKs for fast, efficient encryption of data.
  • Wrap DEKs with KEKs to keep them secure.
  • Store a key alias (or key ID) alongside encrypted DEKs so you know which KEK to use during decryption.

Further Reading

By keeping your key management strategy organized and using clear metadata like aliases, you can confidently store and retrieve your encrypted data, even as KEKs evolve over time.