11 min read

Cryptography
Beginner
A04

Crypto Fundamentals

Nearly every crypto finding is a parameter problem, not a mathematics problem. The algorithm is usually fine; the mode is wrong, the IV repeats, the key is a passphrase, or the integrity check runs after the plaintext has already been used. This page gives you the vocabulary to say which of those it is.

What broken crypto actually means

Nobody is factoring RSA-2048 in a penetration test. A crypto finding means one of four things: a primitive is used outside its contract (a hash protecting a password), a mode leaks structure (ECB), a parameter repeats (IV, nonce, salt), or the implementation is exploitable through a side channel (padding oracle, timing). All four are visible in the code.

Primitives and what each promises

A hash is not encryption. A cipher is not integrity. A signature is not encryption either — the operation is different and so is the key. Most misuse comes from treating these as interchangeable because all of them take bytes and return bytes.

Primitive Promises Typical How it fails
Hash One-way, fixed-length digest; collision resistance SHA-256, SHA-3, BLAKE3 Fast by design — never for passwords. MD5/SHA-1 are collision-broken.
MAC Integrity and authenticity under a shared key HMAC-SHA256, KMAC, Poly1305 Secret-prefix constructions are length-extendable; no MAC means no integrity.
Password hash Deliberately slow, per-password salt Argon2id, scrypt, bcrypt, PBKDF2 Tuned too cheap; a fast hash dressed up with a salt.
Block cipher A pseudorandom permutation on one fixed-size block AES, ChaCha20 (stream) A block cipher alone is not encryption — the mode decides everything.
AEAD Confidentiality and integrity in one construction AES-GCM, ChaCha20-Poly1305, AES-SIV Nonce reuse. Truncated tags. Tag not checked before plaintext is used.
Signature Authenticity from a public key Ed25519, ECDSA P-256, RSA-PSS Verifying with the wrong key type; accepting alg from the token.

Hashing is not encryption

Encryption is reversible with a key; a hash is one-way by construction. That asymmetry is why a hash cannot protect a password the way a cipher protects a file, and why "encrypting" a password with a static key is worse than hashing it — the key is in the application, so the plaintext is one config read away.

Three properties matter when reviewing: a salt must be unique per stored value (defeating precomputation and cross-account comparison), a pepper is a single secret added to every hash (useful only if it lives outside the database), and the function must be expensive. The last one is the test that catches most real findings: SHA-256 computes billions of guesses per second on a GPU, while an Argon2id hash configured to OWASP's current minimum takes tens of milliseconds — the difference between a leaked table being plaintext and being a weekend of work.

Hash plus salt is not a password KDF

A salted SHA-256 is still a fast hash. The salt stops rainbow tables and identical-password detection; it does nothing about the speed. If the review finding is "passwords use SHA-256", the fix is a password KDF — see Key Management for the current parameters.

Block modes decide everything

AES is a permutation over 128-bit blocks. A mode turns that into a message encryption scheme, and each mode makes different assumptions about its IV. Get the IV wrong in CBC and an attacker flips bits; get it wrong in CTR or GCM and the same nonce across two messages hands over the XOR of the plaintexts, or worse.

Mode IV / nonce Integrity Failure when misused
ECB None None Identical plaintext blocks encrypt identically — structure leaks
CBC Random, unpredictable, per message None — needs a MAC Padding oracle; bit-flipping if the recipient acts before MAC verification
CTR Unique nonce, never reused None — needs a MAC Nonce reuse reveals P1 XOR P2 directly; malleable
GCM Unique nonce, 96-bit preferred Tag (verify before use) Nonce reuse recovers the authentication key — catastrophic, not degraded
ChaCha20-Poly1305 Unique nonce Tag Same nonce discipline as GCM; no hardware requirement

Nonce reuse in GCM is not a degradation

Reusing a GCM nonce under the same key lets an attacker recover the authentication subkey, after which they can forge ciphertexts for any message under that key. It is not "weakened confidentiality" — it is forged authenticated encryption. The same nonce problem in CTR only reveals `P1 XOR P2`; the GCM case is worse because the integrity guarantee is gone too.

AEAD, and encrypt-then-MAC

If a construction must provide both confidentiality and integrity, either use an AEAD mode (AES-GCM, ChaCha20-Poly1305, AES-SIV) or compose it yourself as encrypt-then-MAC: MAC the ciphertext, not the plaintext, and verify the MAC before decrypting anything. Encrypt-and-MAC and MAC-then-encrypt both leave oracle surfaces that have been exploited repeatedly in protocols — MAC-then-encrypt is exactly how POODLE and Lucky13 work.

The implementation detail that trips audits is ordering. A handler that decrypts, inspects the plaintext, and then checks the tag still leaks through the inspection. In code, the tag check must be the first thing that happens to the ciphertext.

Key sizes, with dates

"Use strong crypto" is not a finding. Key-size guidance is time-bounded: NIST's SP 800-57 Part 1 Rev. 5 keys the table off security strength, not modulus length, and the practical consequence is a date. Quote the date in the report and the finding stops being arguable.

Algorithm Strength Guidance
AES-128 / AES-256 128 / 256 bits Both fine; 256 for data that must outlive the 2030 horizon
RSA-2048 112 bits Acceptable through 2030, disallowed from 2031 (NIST SP 800-57 Pt 1 Rev. 5)
RSA-3072 128 bits Required for data that must stay protected past 2030
RSA-1024 ≤80 bits Already disallowed for protecting data — a finding on any payment or health codebase
ECDSA / ECDH P-256 128 bits Modern default; prefer Ed25519 for signatures where the library allows
SHA-256 / SHA-512 128 / 256 bits collision Fine. SHA-1 is collision-broken; MD5 is trivially so.

What you can tell from a ciphertext

Two signals are reliable and one is folklore. Reliable: length alignment (CBC output is a multiple of the block size because of padding; a stream mode is not) and block repetition (identical 16-byte blocks in ECB). Folklore: entropy. Compressed data, encrypted data and a JPEG all look equally random to `ent`, so a high entropy score tells you nothing about whether the bytes are encrypted, and a low score only tells you they are not compressed.

Where a format is known, decode it before guessing. Base64 that decodes to a length divisible by 16 is a signal; a JWT whose payload is readable is a signal that the token is signed but not encrypted, which is a different finding from a weak algorithm.

crypto-usage-scan.sh
# 1. Which crypto libraries are even in play
grep -rEn "crypto|Cipher|encrypt|decrypt|hashlib|bcrypt|argon2|jose|jwt" \
  --include="*.{py,js,ts,java,cs,go,rb,php}" . | head -50

# 2. Escalated modes: ECB, and CBC without an IV argument
grep -rEn "AES/ECB|ECB|Cipher.getInstance|createCipheriv|aes-256-ecb|MODE_ECB" \
  --include="*.{py,js,ts,java,cs,go,rb,php}" .

# 3. Randomness feeding a key, IV or token
grep -rEn "Math.random|random.random|rand\(|mt_rand|new Random\(|rand\.Intn|shuffle" \
  --include="*.{py,js,ts,java,cs,go,rb,php}" .

# 4. Passwords going through a general-purpose hash
grep -rEn "sha1|sha256|md5" --include="*.{py,js,ts,java,cs,go,rb,php}" . \
  | grep -Ei "password|passwd|pwd|secret"

# 5. Static keys and IVs
grep -rEn "(key|iv|secret|salt)\s*[:=]\s*["'][^"']{8,}" \
  --include="*.{py,js,ts,java,cs,go,rb,php}" .
Check Expected Where to look
The primitive is identified, not guessed from the word "encrypt" AES-GCM, AES-CBC, ChaCha20-Poly1305, HMAC-SHA256, bcrypt, or a bare block cipher call The import list and the call site
Mode is explicit, never a library default No implicit ECB; a default that resolves to CBC without a random IV is a finding The cipher construction
IV or nonce is generated per message from a CSPRNG A counter is acceptable only where the mode defines one and it never resets The encrypt call
Keys are bytes of the declared length, decoded from a real encoding A 32-byte key for AES-256, not a 32-character passphrase used as bytes The key derivation
Integrity is provided by the mode or an explicit MAC AEAD, or encrypt-then-MAC — never CBC with no MAC, never a bare hash appended The decrypt path

Evidence to Capture

A crypto finding is rarely "uses AES". Record the five things that decide whether it is exploitable:

The exact primitive and mode: AES-256-CBC is not AES-256-GCM. Screenshot the construction call, not the import.

Where the IV or nonce comes from: A static IV, a counter that resets on restart, or a timestamp are each a different attack. Quote the line that generates it.

The key source and length: Hardcoded, environment, KMS, or derived from a passphrase. A 32-character string used as an AES-256 key means the key space is the printable-ASCII subset.

Whether integrity is verified before decryption is acted on: CBC with no MAC is malleable; CBC with a MAC checked after the plaintext is used is still an oracle.

The data class the key protects: Session tokens and payment data move a weak-mode finding from medium to critical; a cached public config file does not.

Not Actually a Finding

These look like crypto defects in a scanner report and usually are not:

  • A hash used for a checksum: MD5 for an ETag, a cache key or a content digest is a design choice, not a vulnerability. The word "MD5" alone is not a finding.
  • A random-looking constant: Base64 in a config file may be a public key, a certificate, a brand asset or a test fixture. Decode it before writing it up.
  • A password hash with a salt and a slow KDF: bcrypt cost 10 with a per-user salt is correct. The finding is a fast hash, not the presence of a hash.
  • AES-CBC inside an authenticated envelope: CBC is not broken; unauthenticated CBC is. If the protocol MACs the ciphertext first, the mode is defensible.

Reading Crypto Code

Work through this list on any encrypt/decrypt path before deciding what to report:

  • ☐ The mode is stated explicitly in the code
  • ☐ The mode is not ECB, and the default is not resolving to ECB
  • ☐ Hashes used for passwords are a password KDF, not SHA-256
  • ☐ Any MAC is HMAC or an AEAD tag, not hash(secret || message)
  • ☐ IV or nonce is generated per message from a CSPRNG
  • ☐ Key length matches the declared algorithm
  • ☐ The key is bytes decoded from an encoding, not a passphrase string
  • ☐ RSA key size is 2048 or larger, with the NIST date noted
  • ☐ The decrypt path verifies integrity before acting on plaintext
  • ☐ Error messages do not distinguish padding failure from other failures
  • ☐ Keys are not in source, history, build artefacts or logs

Where to Go Next

This page is vocabulary. The attacks are the rest of the section:

Why the library default is the most common defect

Most of the findings in this section are not written by anyone: they are a default. Java's Cipher.getInstance("AES") resolves to AES/ECB/PKCS5Padding, so a developer who names only the algorithm gets ECB. Node's old crypto.createCipher derived a key from a password with a fixed salt. Python's Crypto.Cipher.AES.new(key, AES.MODE_CBC) requires an IV, which is why the defect there is usually a zero IV rather than a missing one. When you review, read the line that constructs the cipher — not the import — and check whether the mode and the IV are stated or inherited.

Primitives in Practice

Practice that turns the vocabulary on this page into recognition speed.

Cryptopals — Set 1 Custom Lab easy
Detect AES in ECB mode from a list of ciphertextsBreak repeating-key XOR to see why nonce reuse is fatalImplement ECB and CBC and compare the block structure
Cryptopals — Set 2 Custom Lab medium
Byte-at-a-time ECB decryption (page 7 builds on this)CBC bit-flippingPadding oracle on CBC (page 3 builds on this)
OWASP Juice Shop — crypto challenges Custom Lab medium
Find a weak hash protecting a real valueTrace a JWT with a weak secretCompare the fix with the current OWASP parameters