Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 80 ยท Cryptography Basics

Randomness, Key Management and Secrets

Learn why secrets must come from a cryptographic random source, how entropy and key rotation work, why vaults beat config files, and how to scan for leaked secrets.

Every cryptographic system rests on a secret that an attacker cannot guess or steal. Strong algorithms such as AES and SHA-256 are worthless if the key is predictable, never rotated, or sitting in a public repository. In practice, most real-world cryptographic failures are not mathematical breaks. They are bad randomness and bad key handling.

This lesson covers the two halves of that problem. First, where secrets should come from: cryptographically secure randomness, how to measure entropy, and why random and secrets are different tools. Second, how to look after secrets once they exist: key management, rotation, KMS and vaults, and a habit that prevents the most common leak, committing secrets to version control. You will finish with a small scanner that finds leaked secrets in text. This chapter builds on Symmetric Encryption and Modes and HMAC and Message Authentication, both of which assumed a good key.

Two kinds of random

Most programming languages offer a pseudo-random number generator (PRNG) for simulations, games and shuffling. Python's random module uses the Mersenne Twister, which is fast and statistically well-behaved but completely predictable once you know its seed or observe enough output. A cryptographically secure PRNG (CSPRNG) is designed so that, even if an attacker sees every previous output, the next one is still infeasible to predict. CSPRNGs are seeded from the operating system's entropy pool, which collects unpredictable events from hardware.

In Python:

  • random: reproducible, never for security.
  • secrets: wrapper around the operating system CSPRNG (os.urandom), the right tool for tokens, passwords, keys and nonces.
  • os.urandom(n): n random bytes from the OS.

The classic mistake is seeding a normal PRNG with the clock. The demo below issues a "password reset token" from random.Random(timestamp). An attacker who knows roughly when the token was issued simply tries every second in the window:

Python A guessable token versus a secure one

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The weak token has about 62 bits of apparent randomness, but only about 600 real possibilities, because the only secret input was the timestamp. A secure 32-byte token has 256 bits of real unpredictability. The output length can never tell you how random something is. Only the source can.

Never generate session IDs, reset links, API keys, passwords, salts, nonces or encryption keys with random, Math.random(), timestamps, counters, or hashes of guessable values. Use secrets (Python), crypto.randomBytes (Node.js), SecureRandom (Java) or random_bytes (PHP).

Entropy: measuring unpredictability

Entropy, measured in bits, describes how many equally likely possibilities an attacker must consider. A secret with n bits of entropy takes about 2^n guesses to find by brute force, and on average half that. For a value chosen uniformly from an alphabet of size s with length L, entropy is L * log2(s).

Entropy is a property of the generation process, not of how complicated the result looks. Tr0ub4dor&3 looks strong, but if a human invented it by common substitutions, the process has little entropy. The same is true of the passphrase "correct horse battery staple" if you picked it from a famous comic. Real entropy requires random selection.

Python Bits of entropy and brute-force time

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice how length beats complexity: six random words (about 77 bits) are much stronger than eight random characters from the full keyboard (about 52 bits), and a 128-bit or 256-bit random key is out of reach of any known attack. Human-chosen passwords are a different problem, covered in Password Strength and Policies. For keys and tokens, simply use at least 128 bits from a CSPRNG (256 bits for long-lived keys).

The key lifecycle

A key is not a one-time decision. It has a lifecycle, and each stage can go wrong.

flowchart LR
    G["Generate<br/>(CSPRNG)"] --> S["Store<br/>(vault / KMS / HSM)"]
    S --> U["Use<br/>(least privilege)"]
    U --> R["Rotate<br/>(new key, old verify-only)"]
    R --> X["Retire and destroy"]
    U -. "suspected leak" .-> R

Keys are generated, stored, used and rotated on a schedule. A suspected leak jumps straight to rotation.

  • Generate keys with a CSPRNG, in the place where they will be used. Never invent them by hand.
  • Separate keys by purpose: one for signing sessions, another for encrypting a database column, another per environment. A leaked test key must not unlock production.
  • Limit access to people and services that need it, and log every use.
  • Rotate regularly and always after a possible leak or when someone with access leaves.
  • Destroy retired keys once nothing depends on them.

Key rotation without an outage

The difficulty with rotation is data already protected by the old key: sessions still in flight, tokens already issued, files already encrypted. The standard solution is to give every key an identifier and let the system hold several keys at once. New data uses the newest key. Old data can still be verified or decrypted with older keys until they are retired.

Python A key ring with identifiers and rotation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The same pattern appears in JSON Web Key sets (the kid header), database column encryption, and TLS session ticket keys. If you were to sign without a key identifier, rotation would force you to try every key or break every user at once.

KMS, HSMs and vaults

Storing a key in a config file next to the data it protects is like taping the house key to the door. Better options:

  • Key Management Service (KMS): a cloud service such as AWS KMS, Google Cloud KMS or Azure Key Vault. The key never leaves the service. Your application sends data (or a small key) to be encrypted and receives the result. Access is controlled and logged through IAM policies.
  • Hardware Security Module (HSM): a tamper-resistant device that holds keys and performs cryptography inside. Cloud KMS products are usually backed by HSMs.
  • Secrets manager or vault: a service (HashiCorp Vault, cloud secret managers) for passwords, API keys and certificates, with access policies, audit logs, versioning and optional automatic rotation. Some issue short-lived dynamic credentials per request, so there is nothing long-lived to steal.

A common pattern is envelope encryption: a data encryption key (DEK) encrypts the data locally; the KMS holds a key-encryption key (KEK) that encrypts only the DEK. Rotating the KEK means re-wrapping a few small DEKs instead of re-encrypting terabytes.

flowchart LR
    D["Your data"] -->|"encrypt"| C["Ciphertext"]
    DEK["Data key (DEK)"] -->|"used by"| C
    KMS["KMS holds master key (KEK)"] -->|"wraps"| W["Wrapped DEK"]
    DEK --> W
    W --- C

The ciphertext is stored together with the wrapped data key. Only the KMS can unwrap it.

Never commit secrets

Version control remembers everything. A password pushed to a repository for ten seconds and "deleted" in the next commit is still in the history, in every clone and fork, and in public repositories automated bots find new keys within minutes. If a secret has been committed:

  1. Revoke and rotate it immediately. Removing it from history is secondary; assume it is compromised.
  2. Check the logs of the service it unlocked for unauthorized use.
  3. Clean up the history if you must, and add a prevention step.

Prevention habits: keep secrets in environment variables or a secrets manager, never in code; put .env files in .gitignore and commit only a .env.example with placeholders; use a pre-commit hook and CI scanner; and give each credential the minimum permissions. Secrets, Logging and Error Handling goes further on keeping secrets out of logs.

Scanning text for leaked secrets

Secret scanners combine two ideas: patterns for well-known formats (cloud keys, tokens, private-key headers, password = "..." assignments) and an entropy check for long random-looking strings. The demo below scans a fake configuration file. All the "secrets" are invented and non-functional, and the report redacts values, because a scanner's own output should never become another leak.

Python A tiny secret scanner

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The scanner reports the AWS key, the token, the private key header and the hard-coded api_key, and it ignores os.environ[...] (the correct approach) and the changeme placeholder. The long identifier is correctly left alone because its entropy is low. Real tools such as gitleaks and trufflehog use hundreds of patterns and can also verify whether a key is still live, but the principle is the same. Always expect false positives and false negatives, and treat a scanner as a safety net, not a guarantee.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which Python call is appropriate for generating a password-reset token?

    The secrets module uses the operating system's CSPRNG. The others are predictable: the random module is not secure, a clock seed can be brute-forced, and a hash of a user id is guessable.
  2. 2. A key has 128 bits of entropy. About how many guesses does a brute-force attack need on average?

    n bits of entropy means 2^n possibilities, and an attacker finds the right one after trying about half of them, around 2^(n-1) guesses.
  3. 3. Why give every signing key an identifier (kid)?

    The identifier tells the verifier which key to use, so a new key can sign new data while the old key keeps verifying existing tokens until it is retired.
  4. 4. A developer commits an API key by mistake and pushes, then removes it in the next commit. What is the correct response?

    The key remains in the history and in any clones or forks. Treat it as compromised: revoke and replace it, then check for misuse.
  5. 5. What is the main benefit of envelope encryption with a KMS?

    A KMS-held key encrypts only the small data keys. The bulk data is encrypted locally with them, so rotation is cheap and the master key stays protected.

Summary

  • Use a cryptographically secure source (secrets, os.urandom) for every token, key, salt and nonce. random is predictable and must never be used for security.
  • Entropy measures real unpredictability and depends on the generation process. Use at least 128 random bits for keys and tokens.
  • Treat keys as having a lifecycle: generate, store, use, rotate and destroy, with separate keys per purpose and environment.
  • Identify keys with a kid so rotation can happen without breaking existing data.
  • Keep keys in a KMS, HSM or secrets manager, use envelope encryption for bulk data, and prefer short-lived credentials.
  • Never commit secrets. If one leaks, revoke and rotate first. Add pre-commit and CI secret scanning as a safety net.

Next: Password Storage Done Right, the first lesson of the next chapter, which applies these ideas to the most common secret of all.