Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 38 ยท Cryptographic Failures and Injection

Cryptographic Failures: Protecting Data (A02)

Learn what cryptographic failures are, how to classify sensitive data, protect it in transit and at rest, and avoid weak algorithms, hard-coded keys and bad randomness.

Cryptographic Failures (A02 in 2021, formerly called "Sensitive Data Exposure") covers every way sensitive data ends up readable by someone who should not read it because encryption or hashing was missing, weak or misused. The name changed on purpose: exposure is the symptom, and the cause is usually a failure of cryptography.

You do not need to invent cryptography to avoid these failures. In fact the main rule is the opposite: never design your own algorithms. Use well-reviewed libraries and standard protocols, and spend your effort on using them correctly.

Step 1: know your data

You cannot protect what you have not identified. Classify data by sensitivity:

Class Examples Typical protection
Public Marketing pages Integrity only
Internal Staff handbook Access control
Confidential Emails, addresses, order history Access control + encryption in transit and at rest
Restricted Passwords, card numbers, health records, API keys Strong controls, minimal storage, strict logging

Laws such as GDPR, HIPAA and PCI DSS attach extra duties to some classes. The best protection of all is not collecting or keeping data you do not need. Data you never stored cannot leak.

Step 2: protect data in transit

Everything crossing a network should use TLS (HTTPS). Checklist:

  • Redirect all HTTP to HTTPS and send HSTS (Strict-Transport-Security: max-age=31536000; includeSubDomains).
  • Allow only modern protocol versions (TLS 1.2 and 1.3); disable SSL and early TLS.
  • Use certificates from a trusted authority and renew them automatically (ACME / Let's Encrypt).
  • Mark cookies Secure.
  • Encrypt internal traffic too (service to service, application to database); "inside the network" is not a safe zone.
  • Never disable certificate validation in code to "make an error go away".
Python Spot insecure URLs in a config

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.

Step 3: protect data at rest

Where data sits (databases, backups, logs, laptops) it can be stolen by someone with disk, backup or database access:

  • Disk or database encryption (full-disk, transparent database encryption) protects against stolen hardware and backups.
  • Field-level encryption protects especially sensitive columns even from people with database access.
  • Hash passwords (never encrypt them): the next lesson covers this.
  • Keep secrets out of source code and out of logs. Use a secrets manager or environment variables supplied at runtime.
  • Encrypt backups and test restores.

Common failures

Failure Why it hurts
Data sent in clear text (HTTP, FTP, SMTP without TLS) Anyone on the path can read it
Old algorithms: MD5, SHA-1 for security, DES, RC4, ECB mode Practical attacks exist
Hard-coded keys and passwords in code or repositories Anyone with the code has the key
Default or shared keys across environments One leak opens everything
Weak or predictable randomness (random, timestamps) for tokens Tokens can be guessed
Missing integrity: encryption without authentication Ciphertext can be altered
Reused nonces or IVs Breaks confidentiality of many modes
Not rotating or revoking keys A leaked key stays valid forever

Randomness matters

Security tokens (session ids, reset links, API keys) must come from a cryptographically secure random number generator (CSPRNG), not a general-purpose one. Python's random module is predictable by design; use secrets (or os.urandom). In JavaScript use crypto.getRandomValues; in PHP random_bytes; in Java SecureRandom.

Python Predictable versus secure tokens

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.

Encryption is not enough: authenticate it

Encryption hides data; it does not prove the data was not changed. Use authenticated encryption such as AES-GCM or ChaCha20-Poly1305, which detect tampering. Avoid ECB mode (it leaks patterns: identical blocks give identical ciphertext) and never reuse a nonce with the same key.

The demo below uses a toy keystream cipher (labelled as a stand-in, not real cryptography) to show what a reused nonce does: XORing two ciphertexts cancels the keystream and leaks the relationship between the messages.

Python Stand-in: why nonce reuse is fatal

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 attacker obtained the XOR of both plaintexts without ever knowing the key, which is enough to recover text with guesswork. Real libraries make nonce handling safer, but you must still never reuse one.

Key management

Algorithms are public; keys are the secret. Good practice:

  • Generate keys with a CSPRNG, long enough for the algorithm (for example 256-bit AES keys).
  • Store keys in a KMS or HSM (cloud key management, hardware security module) rather than in config files.
  • Separate keys per environment and purpose; rotate them on a schedule and after any suspected leak.
  • Give each service access only to the keys it needs.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the best protection for data you do not need?

    Data you never stored cannot be breached. Minimisation beats any control.
  2. 2. Which source should you use for session tokens in Python?

    secrets uses the operating system's cryptographically secure generator. random is predictable.
  3. 3. Why use authenticated encryption such as AES-GCM?

    Plain encryption does not prove integrity. Authenticated modes verify data was not altered.
  4. 4. Which is a cryptographic failure?

    A key in the repository is readable by everyone who can read the code, history or build logs.

Summary

  • A02 is about failing to protect sensitive data through missing, weak or misused cryptography.
  • Classify data, collect less, and protect the rest in transit (TLS + HSTS) and at rest (encryption, hashing, secrets management).
  • Avoid weak algorithms, ECB mode, reused nonces, hard-coded keys and predictable randomness.
  • Use a CSPRNG for tokens, authenticated encryption for data, and a KMS with rotation for keys.
  • Never roll your own cryptography.

Next: Hashing, Encryption and Password Storage in Practice