Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 40 ยท Projects

Project: Break Weak Crypto

Play the attacker in a safe lab: crack Caesar and Vigenere with statistics, exploit a reused keystream, factor a tiny RSA key and crack weak password hashes.

The fastest way to understand why cryptographic rules exist is to break something that ignores them. In this project you play the attacker against four deliberately weak systems that you built or met earlier: a classical cipher, a reused one-time pad, an undersized RSA key and a list of badly stored passwords. Every target is a made-up example created inside the editor, so nothing here touches a real system.

Each section follows the same pattern. First you see the mistake, then the attack that exploits it, then the fix. The attacks are short because the weaknesses are fundamental, which is exactly the point. If you need a refresher, the related lessons are classical ciphers, XOR and the one-time pad, RSA from scratch and password hashing.

Break only systems you own or have written permission to test. These exercises use fictional data. The goal is to learn what to avoid, not to attack anyone.

Lab 1: Caesar and Vigenere fall to statistics

A Caesar cipher has only 25 useful keys, so brute force works, but you can do better than reading 25 candidates. English text has a recognizable letter profile (E, T, A and O are frequent, Q and Z are rare). For each possible shift you decrypt, measure how far the result is from English with a chi-squared score, and keep the lowest score.

Vigenere hides this profile by using several shifts. Its weakness: if the key has length L, every L-th letter was shifted by the same amount, so that column is just a Caesar cipher. The attack has two steps.

  1. Find the key length. The index of coincidence (IC) is the chance that two random letters in a text are equal. For English it is about 0.066; for uniformly random letters it is about 0.038. Split the ciphertext into L columns; when L is the true key length (or a multiple), each column is a shifted English text and has a high IC.
  2. Solve each column as a Caesar cipher using chi-squared.
flowchart LR
    A["Ciphertext"] --> B["Try key lengths 1, 2, 3, ..."]
    B --> C{"Average IC of the columns above 0.06?"}
    C -- "no" --> B
    C -- "yes" --> D["Split into L columns"]
    D --> E["Caesar attack on each column"]
    E --> F["Key letters, then plaintext"]

A longer key does not help against this attack, only against short ciphertexts. With enough text it still works.

Python Cracking Caesar and Vigenere

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 attack needs a few hundred letters to be reliable. Short messages, a tricky key or non-English text need more care, but the principle stays. Fix: do not use classical ciphers for anything real. Modern ciphers such as AES have no statistical fingerprint that survives encryption (block ciphers and AES).

Lab 2: The two-time pad

XOR with a random key as long as the message is perfectly secret, but only for one message. If the same keystream encrypts two messages, XORing the two ciphertexts removes the key entirely and leaves the XOR of the two plaintexts. That is much weaker than it sounds, because text has structure: spaces, common words and letter patterns.

The standard attack is crib dragging. You guess a word that probably appears in one message (a crib), XOR it with the mixed stream at every position, and look for positions where the result looks like text. Wrong guesses give garbage, so a readable result usually means you guessed right, and it shows you a piece of the other message. Each recovered piece gives you more cribs to try, and recovered plaintext bytes also give you keystream bytes.

Stream ciphers and CTR modes are secure only if the (key, nonce) pair never repeats; see stream ciphers and randomness and entropy.

Python Crib dragging a reused keystream

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 the false positives. A position can print letters by accident (ds to, odher), and the human reading the output decides which ones make sense. Also notice that the match at position 41 shows text of message 1, because the crib the actually occurs in message 2 there. Either message can reveal the other. Fix: never reuse a (key, nonce) pair. Use a fresh random nonce for every message, or an authenticated mode that makes the mistake harder (authenticated encryption).

Lab 3: Small and careless RSA

RSA is secure only because factoring the modulus n = p * q is hard. Break that assumption and the private key falls out of the public one. Here are two ways to break it.

A modulus that is too small. The lab key has a 60-bit modulus. Pollard's rho algorithm finds a factor in roughly the square root of the smallest factor's size, so tens of thousands of steps for a 30-bit prime (13,433 in the run below). After factoring, the attacker computes phi = (p - 1)(q - 1) and d = e^-1 mod phi exactly like the key owner. Real keys use at least 2048 bits, which puts the same attack astronomically out of reach.

A shared prime. If a faulty random generator produces two keys that share one prime, nobody needs to factor anything: gcd(n1, n2) returns the shared prime in microseconds, and both keys are broken. This has happened in real life with devices that generated keys right after boot, before they had collected enough entropy.

Python Factoring a tiny RSA key and a shared prime

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 last two lines put the numbers in perspective. Trial division on this toy key would take about a billion divisions, which a laptop handles quickly. A 2048-bit modulus has 617 decimal digits, and the best known algorithms are far from feasible at that size. Fix: use 2048-bit or larger RSA (or elliptic curves), generate primes with a secure random source, and check RSA padding and pitfalls for the other ways to misuse it.

Lab 4: Weak password hashes

A site stores SHA-256(password) and then leaks its table. Attackers do not invert the hash; they guess. They hash a wordlist plus common tweaks (capital first letter, digits at the end, a trailing !) and look for matches. With unsalted hashes one precomputed table cracks every user at once, and identical passwords produce identical hashes, so duplicates stand out.

Two defenses change the economics:

  • A per-user random salt means a precomputed table is useless; the attacker has to start again for every account.
  • A slow hash multiplies the cost of each guess. Real systems use PBKDF2, scrypt or Argon2 (password hashing).

The lab shows both effects. The slow_hash function below is a simplified stand-in (salt plus 2,000 repeated SHA-256 steps) so it runs in the browser; in real code you would call a vetted password hashing function.

Python Cracking leaked password hashes

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.

Salting and slowing do not rescue a weak password. Five of the six users were still found, because their passwords were in the wordlist, but each guess now cost 2,000 hash steps instead of one. The sixth password, a random 12-character string, was never in the table. Against a truly random password the attacker has to search an astronomically larger space, which is why length and randomness matter more than clever substitutions.

What the labs have in common

Lab The mistake The attack The fix
Classical ciphers Letter patterns survive encryption Frequency and IC statistics Modern ciphers such as AES
Two-time pad Reusing a keystream XOR the ciphertexts, drag cribs Unique nonce per message
Small RSA Modulus too small, shared primes Pollard rho, gcd 2048+ bits, secure randomness
Password hashes Fast unsalted hash, weak passwords Wordlist plus rules Salt, slow hash, strong passwords

In every case the algorithm was not "broken" in the mathematical sense; the way it was used made it breakable. That is the most common pattern in real-world failures, and the topic of common cryptography mistakes.

Extend the lab

  • Make the Vigenere attack work on shorter text by trying every candidate key length up to 12 and printing the best three results.
  • Add a crib-dragging helper that takes a known word list and ranks positions by how many readable characters they produce.
  • Use Fermat's method (look for n = a^2 - b^2) to factor an RSA modulus whose primes are close together.
  • Add a rule to the password cracker that swaps a with @ and o with 0, and measure how many extra guesses it needs.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why can Vigenere be broken by splitting the ciphertext into columns?

    With a key of length L, letters at positions that are multiples of L apart share a shift. A Caesar cipher falls to frequency analysis.
  2. 2. What does XORing two ciphertexts made with the same keystream give you?

    (m1 xor k) xor (m2 xor k) = m1 xor m2. The keystream disappears, and the structure of language lets attackers separate the messages.
  3. 3. Two RSA public keys share one prime factor. What is the fastest way to break them?

    gcd(n1, n2) equals the shared prime, which immediately factors both moduli.
  4. 4. A site salts every password hash with a unique salt. What does this stop?

    Salts force the attacker to redo the guessing work per user, but they do not make a guessable password safe.
  5. 5. In the lab, the random 12-character password survived while the others fell. Why?

    Wordlist attacks only cover predictable choices. A long random password forces a search of the whole space.

Summary

  • Classical ciphers leak statistics: chi-squared finds a Caesar shift, and the index of coincidence reveals a Vigenere key length.
  • Reusing a keystream (a two-time pad) leaks the XOR of the plaintexts, which crib dragging turns into readable text.
  • Small RSA moduli can be factored, and keys that share a prime are broken by a single gcd.
  • Fast, unsalted password hashes fall to wordlist attacks; salts and slow hashes raise the cost but cannot save weak passwords.
  • Most real failures come from misuse of sound primitives, not from broken math.

Next: Cryptography Exercises with Solutions.