Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

Science project idea

Python Caesar cipher with a brute-force code breaker

Each letter is shifted by a key along the alphabet, and trying all 25 keys breaks the code. It shows how simple ciphers work and why they are weak.

What it shows

Each letter is shifted by a key along the alphabet, and trying all 25 keys breaks the code. It shows how simple ciphers work and why they are weak.

The science

A Caesar cipher replaces each letter with the letter a fixed number of places later, wrapping from Z to A. The modulo 26 does the wrapping. Because there are only 25 useful keys, a computer can try them all in a moment, which is called brute force. Modern encryption uses keys with so many possibilities that this is not possible.

Materials

Cost: free. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.

  • A computer, laptop or phone with a browser (free)
  • Python 3 with IDLE, or the free Thonny editor, or an online Python runner (all free)
  • A short secret message for friends to try to read
  • A notebook and a pencil

Safety

This project uses safe, everyday materials. Still, read the rules below with a parent before you start.

Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.

All the safety rules

Step by step

8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.

  1. Create caesar.py and define def shift(text, key): with result = "" as its first line.

  2. Add for ch in text: and if ch.isalpha(): on the next level.

  3. Inside set base = ord("A") if ch.isupper() else ord("a").

  4. Add result += chr((ord(ch) - base + key) % 26 + base) and then else: result += ch for spaces and punctuation, and return result after the loop.

  5. Test with message = shift("Hello India", 3) and print it; you should see Khoor Lqgld.

  6. Decode by calling shift with the negative key, shift(message, -3), and check you get the original.

  7. Write the code breaker: for k in range(1, 26): print(k, shift(coded, -k)).

  8. Give a friend a coded sentence and let them find the key by reading the 25 lines.

What you should see

Hello India becomes Khoor Lqgld with key 3 and decodes back. The breaker prints 25 lines, and one of them is the readable original.

Viva questions

A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.

1What does % 26 do?

It wraps the number back to the start of the alphabet.

2Why is this cipher weak?

There are only 25 keys so all can be tried quickly.

3What do ord and chr do?

ord gives the number of a character; chr gives the character of a number.

4How is modern encryption stronger?

It uses keys with astronomically many possibilities.

Free ebook: Viva Questions for Science Projects

Take it further

Count letter frequencies in a long coded message and use the most common letter, usually E, to guess the key without trying all.

Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.

Free ebooks for this project

WhatsApp us