💻 Computer Science · Topic 77 of 80

The Caesar Cipher

The Caesar cipher shifts each letter by a fixed number of places.

Interactive labJump along the number line

About this topic

The Caesar cipher is one of the oldest encryption methods, named after Julius Caesar, who reportedly used it to protect military messages. It is a substitution cipher in which every letter is shifted a fixed number of places along the alphabet. The shift is the key.

With a shift of 3, A becomes D, B becomes E and Z wraps around to C, so HELLO becomes KHOOR. To decrypt, shift back by 3. Mathematically, encrypted letter equals (letter number plus shift) mod 26. A program can do this with a loop over characters and the modulus operator, using character codes. ROT13 is the same idea with shift 13, which is its own inverse.

The Caesar cipher is very weak because there are only 25 possible shifts, so an attacker can try them all, called brute force. It is also broken by frequency analysis: in English, E is the most common letter, so the most frequent letter in the ciphertext suggests the shift. Modern encryption uses far larger key spaces and better designs. A common misconception is that a secret method is secure, but good security depends on a strong key, not hiding the algorithm. It is still a great first coding exercise.

Key ideas

  1. Shift of 3 turns A into D
  2. Easy to crack by trying all 25 shifts
  3. Modern encryption is far stronger

Quick check

Use cases: where The Caesar Cipher helps

Computing ideas run phones, search engines, games and the wider internet.

Frequently asked questions

What is The Caesar Cipher?

The Caesar cipher shifts each letter by a fixed number of places.

What will I learn in The Caesar Cipher?

Shift of 3 turns A into D. Easy to crack by trying all 25 shifts. Modern encryption is far stronger.

Is The Caesar Cipher part of Computer Science?

Yes. It is one of 80 Computer Science topics on LearnViz HighSchoolEdu, with 3 key ideas, a live animation and a quick check.

Does the The Caesar Cipher lab work on a phone or tablet?

Yes. The animation resizes to fit your screen and supports touch, so you can drag, tap and slide with your finger.

How should I study The Caesar Cipher?

Read the summary and key ideas first, play with the interactive lab to see the idea in motion, then answer the quick check. If you get a question wrong, go back to the key idea it tests.

Do I need an account, and is my progress saved?

No account is needed. Marking a topic as learned is saved in your own browser, so it stays on that device and is not sent to a server.

More in Computer Science