Skip to content
uencoder
TASK

Store encryption keys securely

Keys must never be hardcoded or committed to source control. Learn HSM, KMS, envelope encryption, and key derivation patterns for secure key storage.

  1. 1

    The golden rule: never store encryption keys in plaintext alongside the data they encrypt. Options from most to least secure: HSM (hardware), KMS (cloud service), envelope encryption (master key wraps data keys), key derivation (derive from a password/passphrase).

  2. 2
    PBKDF2 Key Derivation →

    Derive an encryption key from a user password using PBKDF2 with a high iteration count (100,000+). The salt must be random and stored separately. This is for user-controlled keys where no server-side storage exists.

  3. 3
    bcrypt Hash →

    Hash a master password with bcrypt for authentication. Note: bcrypt output is not suitable as an encryption key directly — use PBKDF2/scrypt for key derivation, bcrypt for password verification.

  4. 4
    AES Encryptor →

    Use the derived key to encrypt data. The key never touches disk in plaintext — it exists only in memory during the encryption operation and is zeroed afterward.

  5. 5

    For server-side applications, use a KMS (AWS KMS, GCP KMS, Azure Key Vault) or HSM. The master key never leaves the hardware; you call an API to encrypt/decrypt. For client-side, derive keys from user passwords with a strong KDF and store only the salt.

Frequently asked questions

Can I store a key in an environment variable?
Environment variables are better than hardcoding but still visible to anyone with process access (e.g., /proc/pid/environ on Linux). Use a KMS or secrets manager (Vault, AWS Secrets Manager) for production. Environment variables are acceptable for development only.
What is the difference between PBKDF2 and bcrypt?
PBKDF2 is a KDF that produces a key of any length — use it for encryption keys. bcrypt is a password hashing function with a fixed output — use it for password verification. They serve different purposes and are not interchangeable.

Workflows that solve this

Learn more

Want to understand why?

Related tools