SECURITY ENGINEERING · PART 4 · CRYPTOGRAPHY IN PRACTICE · PRACTICE · 56 OF 229

Cryptography Mistakes That Break Real Systems

Strong algorithms fail through weak usage. This episode is a review checklist of the cryptography mistakes found in real code: inventing your own scheme, predictable random numbers (a seeded generator repeated exactly in our test), reused nonces (we recovered a second message from two ciphertexts), hard-coded keys, missing authentication, disabled certificate checks and weak algorithms, with how to find and fix each.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 14

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Never invent
  • secure randomness
  • unique nonces
  • authenticated modes
  • verify everything
  • keys in a KMS

Notes

Where it breaks

Think of it in two layers. The algorithm was designed and attacked by experts for decades. The usage was written last week, under a deadline. The mode, the nonce, where the key comes from, where it is stored, whether verification happens, how errors are handled and where random numbers come from: each of those can undo everything. OWASP lists this as cryptographic failures, near the top of its list of web application risks.

A way to think about it
  • the algorithm: designed and attacked by experts for decades
  • the usage: written by you, last week, under deadline
  • mode, nonce, key source, key storage, verification, error handling, randomness
  • each one can undo everything
  • OWASP: Cryptographic Failures

1. Rolling your own

Mistake one is inventing your own. A custom cipher that XORs data with a key. Base sixty four called encryption. A home-made protocol that uses correct parts in the wrong order. A custom password hash. Anyone can design a scheme that they themselves cannot break; that proves nothing. Use a high-level library such as libsodium or Tink, where the safe choices are made for you, and standard protocols such as TLS.

Do not invent
  • custom ciphers: XOR with a key, shifted alphabets
  • "encrypting" with Base64
  • your own protocol built from correct parts in the wrong order
  • your own password hash
  • instead: a high-level library (libsodium, Tink, the platform crypto API) and standard protocols (TLS)

2. Weak randomness

Mistake two is weak randomness. Ordinary random functions are built for simulations, not security, and are fully predictable. We seeded Python's random with a timestamp and generated a one hundred and twenty eight bit value. Seeding again with the same time gave exactly the same value. A token generated that way can be guessed by trying every second of the day. For keys, tokens, salts and nonces, always use the secure source: the secrets module, or the operating system.

random vs secrets
random.seed(1700000000)
random.getrandbits(128)
# 0x6b4b2d0c310f9fcb...
random.seed(1700000000)
random.getrandbits(128)
# 0x6b4b2d0c310f9fcb...  identical

secrets.token_hex(16)
# 3db14d09825e03f0a193c25bffc3a97a
# unpredictable, from the OS

3. Reused nonce

Mistake three: a reused nonce. We encrypted two messages with AES in GCM mode, the same key and a fixed nonce of zeros. Combining the two ciphertexts with XOR gave exactly the XOR of the two plaintexts. Knowing the first message, we recovered the second in full, without the key. In GCM, nonce reuse also lets an attacker forge authentication tags. Generate a fresh random nonce every time, or let the library do it.

what nonce reuse gives away
n = b"\x00" * 12            # fixed
c1 = AESGCM(k).encrypt(n, p1, None)
c2 = AESGCM(k).encrypt(n, p2, None)

xor(c1, c2) == xor(p1, p2)   # True
# knowing p1, recover p2:
# b"transfer 9999 to mallo"

# no key needed. Forging tags
# becomes possible as well

4. Key handling

Mistake four is key handling, the most common of all. Keys hard-coded in source, or inside a mobile app where anyone can extract them. Default keys from a tutorial left in production. A password used directly as a key instead of passing it through a key derivation function. The same key in development, test and production. Keys pasted into logs, tickets and chat. The fixes: a key management service or secrets manager, secret scanning, and rotation.

Keys in the wrong place
  • hard-coded in source, or in a mobile app binary
  • default or example keys left in production
  • a password used directly as the key (use a KDF)
  • the same key in dev, test and production
  • keys in logs, tickets, chat
  • fix: KMS or secrets manager, scanning, rotation

5. No authentication

Mistake five: encryption without authentication. ECB shows patterns. CBC or counter mode without a MAC lets an attacker flip bits in the ciphertext and change the decrypted result in a controlled way. If the server also reveals whether padding was valid, the attacker can decrypt data byte by byte, the padding oracle attack. Encrypted cookies get edited to change a role. The fix is simple: only use authenticated modes, GCM or ChaCha twenty with Poly thirteen oh five.

Encrypt without integrity
  • ECB: patterns visible
  • CBC or CTR with no MAC: attacker flips bits and changes the plaintext
  • padding oracle: error differences let an attacker decrypt without the key
  • encrypted cookies edited to change role or user id
  • fix: AEAD only (GCM, ChaCha20-Poly1305)

6. Skipped verification

Mistake six: skipped verification. Each of these lines turns off certificate checking in a different language, usually added to make a test pass and never removed. With it, the connection is still encrypted, to anyone who cares to intercept it. The same pattern appears with tokens: decoding a JWT without verifying its signature, or accepting the algorithm none. Search your codebase for these strings. It takes one minute, and it often finds something.

grep for these
requests.get(url, verify=False)
curl -k / --insecure
InsecureSkipVerify: true
NODE_TLS_REJECT_UNAUTHORIZED=0
rejectUnauthorized: false
TrustAllCerts, ALLOW_ALL_HOSTNAME
jwt.decode(t, verify=False)
"alg": "none" accepted

# also: a failed check that is
# logged and then ignored

7. Weak algorithms

Mistake seven: weak algorithms that linger. MD five and SHA one for signatures or integrity. DES, triple DES, RC four. RSA keys under two thousand and forty eight bits. Old SSL and TLS versions. Fast hashes for passwords. Tokens with too little randomness. Most of these survive in configuration files and legacy code nobody wants to touch. Keep an inventory of which cryptography is used where; you will need it again for the post-quantum migration.

Retire these
  • MD5, SHA-1: signatures, integrity, certificates
  • DES, 3DES, RC4, Blowfish
  • RSA under 2048 bits; RSA PKCS#1 v1.5 encryption
  • SSLv3, TLS 1.0, TLS 1.1
  • fast hashes for passwords
  • short keys or tokens (under 128 bits of randomness)
  • keep an inventory: what crypto is used where

8. Side channels

Mistake eight: side channels. Comparing a MAC or a token with a normal equals leaks through timing how many bytes were right. Returning different errors for bad padding and bad MAC tells an attacker which step failed. Secrets and decrypted data end up in logs and stack traces. Compressing data before encrypting it leaks content through length. The fixes: constant-time comparison, one generic error for any decryption failure, and care with logs.

Leaks around the crypto
  • comparing MACs or tokens with == (timing)
  • different errors for "bad padding" and "bad MAC"
  • secrets and plaintext in logs and stack traces
  • keys left in memory dumps
  • compression before encryption (length leaks content)
  • fix: constant-time compare, one generic error

Find them

The good news is that most of these can be found automatically. Static analysis rules in Semgrep, CodeQL and Bandit flag ECB mode, MD five, disabled verification and insecure random. Secret scanners find hard-coded keys. TLS scanners report old versions and weak ciphers. Dependency checks cover the crypto libraries themselves. And one review rule: any code that calls a low-level primitive directly gets a second reviewer who knows this checklist.

Tools and habits
  • SAST rules: Semgrep, CodeQL, Bandit flag ECB, MD5, verify=False, random
  • secret scanning: gitleaks, TruffleHog
  • TLS scans: testssl.sh, sslyze
  • dependency checks for the crypto libraries themselves
  • review rule: any direct use of a primitive needs a second pair of eyes

The safe defaults

Here is the page to keep. Encrypt with AES two fifty six in GCM mode or ChaCha twenty Poly thirteen oh five. Hash with SHA two fifty six. Authenticate with HMAC. Store passwords with Argon two i d or bcrypt. Sign with E D two five five one nine or ECDSA. Exchange keys with X two five five one nine. Transport over TLS 1.3. Take randomness from the operating system. Keep keys in a key service, versioned and rotated. And use a high-level, vetted library.

Use this, not that
  • encrypt: AES-256-GCM or ChaCha20-Poly1305
  • hash: SHA-256
  • MAC: HMAC-SHA-256
  • passwords: Argon2id, bcrypt
  • sign: Ed25519, ECDSA P-256
  • key exchange: X25519
  • transport: TLS 1.3
  • random: the OS secure source
  • keys: KMS, versioned, rotated
  • library: high-level, vetted

Say it like this

In the interview: I look for custom cryptography, ECB or unauthenticated modes, fixed or reused nonces, insecure random number generators, hard-coded keys, MD five or SHA one, fast hashes for passwords, disabled certificate or signature verification, and comparisons that are not constant-time. Then I ask where the keys live and how they rotate. Most of this I automate with static analysis and secret scanning, and I steer teams to high-level libraries.

In the interview
  • "Custom crypto, ECB or unauthenticated modes, fixed
  • or reused nonces, insecure
  • random, hard-coded keys, MD5 or SHA-1, fast password
  • hashes, disabled certificate
  • or signature checks, and non-constant-time compares.
  • Then: where do keys live?"

Interview questions

Q1What are the most common cryptography mistakes in applications?

Hard-coded or poorly stored keys, weak or outdated algorithms such as MD5 and SHA-1, ECB or unauthenticated encryption modes, reused nonces or IVs, insecure random number generators, fast hashes for passwords and disabled certificate or signature verification.

Q2Why should you not write your own cryptography?

Secure designs require expertise and years of public analysis; subtle errors are invisible in testing and catastrophic in use. Vetted high-level libraries and standard protocols encode safe choices, so developers should compose those rather than primitives.

Q3What is a cryptographically secure random number generator?

A generator whose output cannot be predicted even by someone who has seen earlier outputs, seeded from operating system entropy. Examples are /dev/urandom, Python's secrets module, Java's SecureRandom and crypto.randomBytes in Node.js.

Q4What happens if a nonce or IV is reused?

In stream-based modes such as CTR and GCM, reuse with the same key reveals the XOR of the plaintexts and, in GCM, enables tag forgery. In CBC, a predictable or reused IV leaks whether messages share a prefix.

Q5What is a padding oracle attack?

An attack on unauthenticated CBC encryption in which the system reveals whether decrypted padding is valid. By submitting modified ciphertexts and observing responses, an attacker can decrypt data without the key. Authenticated encryption prevents it.

Q6What does OWASP mean by Cryptographic Failures?

An OWASP Top 10 category covering failures to protect data with cryptography: data sent or stored in clear text, weak or old algorithms, poor key management, missing certificate validation and insufficient randomness.

Q7How do you find cryptographic weaknesses in a codebase?

Combine static analysis rules for insecure APIs and modes, secret scanning for embedded keys, TLS configuration scanning, dependency checks and manual review of any direct use of low-level primitives, backed by an inventory of where cryptography is used.

Q8Is Base64 a form of encryption?

No. Base64 is an encoding with no key; anyone can reverse it instantly. Treating encoded data, such as Base64 credentials in configuration or the payload of a JWT, as protected is a common and serious mistake.