Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 40 ยท Practice and Review

Cryptography Exercises with Solutions

Practice cryptography with 15 graded exercises on modular math, hashing, classical and XOR ciphers and RSA. Every solution runs live in your browser.

Reading about cryptography teaches you the vocabulary; solving small problems makes it stick. This page has 15 exercises in five groups: modular math, hashing, classical ciphers, XOR and stream mistakes, and RSA. Each one states a task and then gives a runnable solution in an editor. The solutions use assert statements, so any wrong edit makes the run fail loudly, and they print the values they computed so you can compare with your own.

How to use this page

Do not run the solution first. Read the task, then open the editor, delete the solution body and write your own. Compare your output with the printed values, and only then read the shown approach. If an exercise takes more than ten minutes, peek at the solution, close it, and redo it from memory the next day.

Four habits solve most of these problems:

  1. Work the toy size by hand first. If the numbers are small, you can check the answer on paper.
  2. Use Python's built-ins. pow(a, b, m), pow(a, -1, m), math.gcd and hashlib already do the heavy lifting.
  3. Check both directions. Decrypt what you encrypted, verify what you signed, and make sure a tampered input fails.
  4. Keep bytes and text apart. Convert with .encode() and .decode() deliberately (encoding vs encryption).
flowchart TD
    A["Read the task"] --> B["Identify the primitive:<br/>math, hash, cipher or RSA"]
    B --> C["Try a tiny example by hand"]
    C --> D["Write the code"]
    D --> E["Assert the round trip<br/>and one failure case"]

A routine that works for every exercise below.

Part 1: Modular math

Related lessons: modular arithmetic, GCD and Euclid, primes and Euler's theorem and the CRT.

Exercise 1. Compute 7^222 mod 13 without calculating 7^222, and find the modular inverse of 3 modulo 11. Check that 3 * inverse mod 11 == 1.

Python Exercise 1: fast powers and inverses

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.

Exercise 2. Write egcd(a, b) that returns (g, x, y) with a*x + b*y == g. Use it on 252 and 198, and use it to find the inverse of 17 modulo 3120 without pow(a, -1, m).

Python Exercise 2: extended Euclid

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.

Exercise 3. Find the smallest prime greater than 10,000 using trial division up to the square root. Then confirm that 561 is not prime even though pow(2, 560, 561) == 1.

Python Exercise 3: next prime and a Fermat liar

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.

Exercise 4. Solve the system x = 2 (mod 3), x = 3 (mod 5), x = 2 (mod 7) with the Chinese Remainder Theorem. Write a general function for pairwise coprime moduli.

Python Exercise 4: Chinese Remainder Theorem

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.

Part 2: Hashing

Related lessons: hash functions, hash uses and MACs and HMAC.

Exercise 5. Hash b"abc" and b"abd" with SHA-256 and count how many of the 256 output bits differ. Is the number close to what you would expect from the avalanche effect?

Python Exercise 5: measuring the avalanche

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.

Exercise 6. Find the smallest integer nonce such that sha256(b"crypto" + str(nonce).encode()) starts with 000 in hex. About how many tries do you expect on average, and why?

Python Exercise 6: a tiny proof of work

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.

Exercise 7. A publisher lists the SHA-256 digests of four files. One downloaded copy was corrupted. Write a function that names the bad file.

Python Exercise 7: finding the corrupted file

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.

Exercise 8. Compute an HMAC-SHA256 tag for the message b"amount=100&to=alice" with the key b"s3cret". Verify it with a constant-time comparison, and show that a changed amount fails.

Python Exercise 8: tag and verify

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.

Part 3: Classical ciphers

Related lessons: classical ciphers and Break Weak Crypto.

Exercise 9. The ciphertext TLLA TL HA AOL IYPKNL was made with a Caesar shift. Try all 25 shifts and print the one that contains the word THE.

Python Exercise 9: Caesar brute force

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.

Exercise 10. A single-byte XOR key was used on English text. The ciphertext in hex is below. Try all 256 keys and pick the one whose output has the most letters and spaces.

Python Exercise 10: single-byte XOR

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.

Exercise 11. Two messages of equal length were encrypted with the same keystream. You know that message 1 is attack at dawn on friday. Recover message 2 without knowing the key.

Python Exercise 11: reusing a 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.

Part 4: RSA

Related lessons: RSA from scratch, RSA padding and pitfalls and digital signatures.

Exercise 12. With p = 61, q = 53 and e = 17, compute n, phi, the private exponent d, then encrypt the message 65 and decrypt it again.

Python Exercise 12: textbook RSA by hand

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.

Exercise 13. Using the same toy key, sign a message by raising sha256(message) mod n to the power d. Verify the signature and show that a different message does not verify.

Python Exercise 13: a toy signature

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.

Exercise 14. The public key is n = 10403, e = 7, and the ciphertext is 10378. Break the key by factoring n, then decrypt.

Python Exercise 14: breaking a small key

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.

Exercise 15. A sender encrypts a short message with e = 3 and no padding. The message cubed is smaller than the modulus, so no reduction ever happened. Recover the message with an integer cube root.

Python Exercise 15: the small-exponent attack

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.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does pow(7, 222, 13) work even though 7**222 is a huge number?

    Modular exponentiation (square and multiply) reduces at every step, so intermediate values never exceed the modulus squared.
  2. 2. In Exercise 11, why is the key not needed to recover message 2?

    c1 xor c2 = p1 xor p2. With p1 known, XORing again yields p2.
  3. 3. What makes the small-exponent attack in Exercise 15 possible?

    When m^3 < n, no modular reduction happens, and an integer cube root recovers m. Padding randomizes and enlarges the message.
  4. 4. What should hmac.compare_digest be used to compare?

    Comparing tags with == can leak how many leading bytes match through timing. compare_digest takes the same time regardless.
  5. 5. In Exercise 13 the toy signature is easy to forge. Why?

    A hash reduced modulo a small n loses collision resistance. Real systems use 2048-bit or larger moduli with a proper encoding.

Summary

  • Python's pow(a, b, m) and pow(a, -1, m) cover fast exponentiation and modular inverses; the extended Euclid algorithm and the CRT are short to write yourself.
  • Hash exercises show the avalanche effect, the cost of proof of work (16 times more per hex zero) and integrity checks with digests and HMAC.
  • Classical and XOR ciphers fall to brute force, statistics, or a reused keystream.
  • Textbook RSA works on paper, but tiny moduli can be factored and unpadded small exponents leak the message.
  • Always test the round trip and at least one failure case.

Next: Common Cryptography Mistakes.