12 Real-World Snippets  |  Code Review Practice  |  With Fixes

Spot the Bug: Code Review Challenges

12 short Java code snippets, each with one real cryptographic implementation mistake — the same kind that shows up in actual security audits, from ECB mode to timing-unsafe comparisons. Read the code, identify what's wrong, and see the fixed version afterward. These are the mistakes a working knowledge of cryptography theory doesn't automatically protect you from — only practiced code review does.

12 code snippets · multiple choice · every answer includes an explanation and the fixed code

Why Theory Alone Doesn't Prevent These Bugs

Understanding that AES is secure and RSA relies on factoring doesn't automatically protect against implementing them badly. Nearly every widely publicized "crypto failure" in real software — from early WEP Wi-Fi encryption to countless CVEs in production systems — came from correct algorithms used incorrectly: a properly-implemented AES cipher running in ECB mode, a cryptographically sound RSA implementation missing padding, a well-designed hash function applied to passwords with no salt. Code review for these patterns is a distinct skill from understanding the underlying math, and like any skill, it improves with deliberate practice against real examples.

The Patterns Behind These 12 Challenges

CategoryWhat to Look For
Mode of operationECB in a Cipher string, or a hardcoded/reused IV with CBC or GCM
Randomness sourcejava.util.Random instead of SecureRandom for keys, IVs, or tokens
Password storageRaw MD5/SHA-1/SHA-256 on a password with no salt and no iteration count
Key managementEncryption keys or secrets hardcoded directly in source code
AuthenticationEncryption with no accompanying MAC/AEAD — confidentiality without integrity
Comparison logic.equals() or == used on secrets, instead of a constant-time comparison

Want to check your own codebase for some of these patterns automatically? The CBOM Generator scans pasted code for several of the algorithm-level issues covered here (MD5, DES, weak ciphers) as a starting point.

References

  1. OWASP Cryptographic Storage Cheat Sheet
  2. NIST SP 800-38A — Block Cipher Modes of Operation
  3. KF-Cipher CBOM Generator