Caesar cipher: encoder and decoder with steps
AcademyEncrypt or decrypt with any shift and see every step, or paste a message and let brute force find the key.
Covered inAKTU KCS074 Unit 1·Anna University CB3491 Unit 1·GTU 2170709 Unit 1·NPTEL Cryptography and Network Security
C = (p + 3) mod 26
Show your work, letter by letter
| Letter | p | p ± k | mod 26 | Result |
|---|---|---|---|---|
| M | 12 | 12 + 3 = 15 | 15 mod 26 = 15 | P |
| E | 4 | 4 + 3 = 7 | 7 mod 26 = 7 | H |
| T | 19 | 19 + 3 = 22 | 22 mod 26 = 22 | W |
| A | 0 | 0 + 3 = 3 | 3 mod 26 = 3 | D |
| F | 5 | 5 + 3 = 8 | 8 mod 26 = 8 | I |
| R | 17 | 17 + 3 = 20 | 20 mod 26 = 20 | U |
| H | 7 | 7 + 3 = 10 | 10 mod 26 = 10 | K |
| O | 14 | 14 + 3 = 17 | 17 mod 26 = 17 | R |
| G | 6 | 6 + 3 = 9 | 9 mod 26 = 9 | J |
| P | 15 | 15 + 3 = 18 | 18 mod 26 = 18 | S |
| Y | 24 | 24 + 3 = 27 | 27 mod 26 = 1 | B |
Rows marked in yellow wrap around: past Z the count starts again at A.
k = 3 MEET ME AFTER THE TOGA PARTY
| k | Decrypted |
|---|---|
| 1 | OGGV OG CHVGT VJG VQIC RCTVA |
| 2 | NFFU NF BGUFS UIF UPHB QBSUZ |
| 3 | MEET ME AFTER THE TOGA PARTY |
| 4 | LDDS LD ZESDQ SGD SNFZ OZQSX |
| 5 | KCCR KC YDRCP RFC RMEY NYPRW |
| 6 | JBBQ JB XCQBO QEB QLDX MXOQV |
| 7 | IAAP IA WBPAN PDA PKCW LWNPU |
| 8 | HZZO HZ VAOZM OCZ OJBV KVMOT |
| 9 | GYYN GY UZNYL NBY NIAU JULNS |
| 10 | FXXM FX TYMXK MAX MHZT ITKMR |
| 11 | EWWL EW SXLWJ LZW LGYS HSJLQ |
| 12 | DVVK DV RWKVI KYV KFXR GRIKP |
| 13 | CUUJ CU QVJUH JXU JEWQ FQHJO |
| 14 | BTTI BT PUITG IWT IDVP EPGIN |
| 15 | ASSH AS OTHSF HVS HCUO DOFHM |
| 16 | ZRRG ZR NSGRE GUR GBTN CNEGL |
| 17 | YQQF YQ MRFQD FTQ FASM BMDFK |
| 18 | XPPE XP LQEPC ESP EZRL ALCEJ |
| 19 | WOOD WO KPDOB DRO DYQK ZKBDI |
| 20 | VNNC VN JOCNA CQN CXPJ YJACH |
| 21 | UMMB UM INBMZ BPM BWOI XIZBG |
| 22 | TLLA TL HMALY AOL AVNH WHYAF |
| 23 | SKKZ SK GLZKX ZNK ZUMG VGXZE |
| 24 | RJJY RJ FKYJW YMJ YTLF UFWYD |
| 25 | QIIX QI EJXIV XLI XSKE TEVXC |
How the Caesar cipher works
The Caesar cipher replaces every letter with the letter a fixed number of places further down the alphabet. That number is the key k. With k = 3, A becomes D, B becomes E, and at the end of the alphabet the count wraps around: X becomes A, Y becomes B, Z becomes C.
To calculate it, number the letters from A = 0 to Z = 25 and use modular arithmetic:
It is named after Julius Caesar, who according to the Roman historian Suetonius wrote private letters with a shift of three. It is a substitution cipher with a single alphabet, which makes it the simplest example of a classical cipher and the usual first lesson in cryptography.
Encrypt “MEET ME AFTER THE TOGA PARTY” with k = 3
This is the textbook example. Every letter moves three places; spaces stay. The rows marked yellow wrap past Z.
| Letter | p | p + k | mod 26 | C |
|---|---|---|---|---|
| M | 12 | 12 + 3 = 15 | 15 mod 26 = 15 | P |
| E | 4 | 4 + 3 = 7 | 7 mod 26 = 7 | H |
| T | 19 | 19 + 3 = 22 | 22 mod 26 = 22 | W |
| A | 0 | 0 + 3 = 3 | 3 mod 26 = 3 | D |
| F | 5 | 5 + 3 = 8 | 8 mod 26 = 8 | I |
| R | 17 | 17 + 3 = 20 | 20 mod 26 = 20 | U |
| H | 7 | 7 + 3 = 10 | 10 mod 26 = 10 | K |
| O | 14 | 14 + 3 = 17 | 17 mod 26 = 17 | R |
| G | 6 | 6 + 3 = 9 | 9 mod 26 = 9 | J |
| P | 15 | 15 + 3 = 18 | 18 mod 26 = 18 | S |
| Y | 24 | 24 + 3 = 27 | 27 mod 26 = 1 | B |
Result: PHHW PH DIWHU WKH WRJD SDUWB. To decrypt, subtract 3 instead: P (15) − 3 = 12 → M. For A, B and C the result is negative and wraps to the end: A (0) − 3 = −3, and −3 mod 26 = 23 → X.
Caesar cipher in the exam
Short answer
The Caesar cipher is a monoalphabetic substitution cipher in which each letter of the plaintext is replaced by the letter k positions later in the alphabet: C = (p + k) mod 26, p = (C − k) mod 26. With only 25 possible keys it is easily broken by brute force or frequency analysis.
Outline for a long answer
- Definition and type: monoalphabetic substitution, symmetric key k.
- Encryption and decryption formulas with A = 0 … Z = 25.
- Worked example with a table (plaintext letter, p, p + k, mod 26, ciphertext).
- Cryptanalysis: brute force over 25 keys; frequency analysis.
- Weaknesses: small key space, letter frequencies preserved; ROT13 as special case.
Caesar cipher program in C, Java and Python
Each program encrypts the example with k = 3 and decrypts it again, using only the standard library. Compare your output with the expected output below.
#include <stdio.h>
#include <ctype.h>
void caesar(const char *in, char *out, int k) {
k = ((k % 26) + 26) % 26;
for (; *in; in++, out++) {
if (isupper((unsigned char)*in)) *out = 'A' + (*in - 'A' + k) % 26;
else if (islower((unsigned char)*in)) *out = 'a' + (*in - 'a' + k) % 26;
else *out = *in;
}
*out = '\0';
}
int main(void) {
const char *plain = "MEET ME AFTER THE TOGA PARTY";
char cipher[100], back[100];
caesar(plain, cipher, 3);
caesar(cipher, back, -3);
printf("Plaintext : %s\n", plain);
printf("Ciphertext: %s\n", cipher);
printf("Decrypted : %s\n", back);
return 0;
}public class Caesar {
static String caesar(String text, int k) {
k = ((k % 26) + 26) % 26;
StringBuilder out = new StringBuilder();
for (char ch : text.toCharArray()) {
if (Character.isUpperCase(ch)) out.append((char) ('A' + (ch - 'A' + k) % 26));
else if (Character.isLowerCase(ch)) out.append((char) ('a' + (ch - 'a' + k) % 26));
else out.append(ch);
}
return out.toString();
}
public static void main(String[] args) {
String plain = "MEET ME AFTER THE TOGA PARTY";
String cipher = caesar(plain, 3);
System.out.println("Plaintext : " + plain);
System.out.println("Ciphertext: " + cipher);
System.out.println("Decrypted : " + caesar(cipher, -3));
}
}def caesar(text, k):
out = []
for ch in text:
if ch.isupper():
out.append(chr((ord(ch) - 65 + k) % 26 + 65))
elif ch.islower():
out.append(chr((ord(ch) - 97 + k) % 26 + 97))
else:
out.append(ch)
return "".join(out)
plain = "MEET ME AFTER THE TOGA PARTY"
cipher = caesar(plain, 3)
print("Plaintext :", plain)
print("Ciphertext:", cipher)
print("Decrypted :", caesar(cipher, -3))
# Brute force: try all 25 keys
for k in range(1, 26):
print(k, caesar(cipher, -k))Plaintext : MEET ME AFTER THE TOGA PARTY Ciphertext: PHHW PH DIWHU WKH WRJD SDUWB Decrypted : MEET ME AFTER THE TOGA PARTY
Practice: encrypt, decrypt and break
A new task on every click. Work it out on paper, type your answer, then check it and open the solution.
Encrypt “ATTACK AT DAWN” with k = 5.
Show the solution
FYYFHP FY IFBS
Breaking it: brute force
An attacker who does not know k simply tries all 25 keys. One line makes sense, the others are gibberish. The “Decode without the key” tab does this and ranks the lines with a chi-squared test against English letter frequencies, the same idea as frequency analysis.
| k | “PHHW PH DIWHU WKH WRJD SDUWB” decrypted |
|---|---|
| 1 | OGGV OG CHVGT VJG VQIC RCTVA |
| 2 | NFFU NF BGUFS UIF UPHB QBSUZ |
| 3 | MEET ME AFTER THE TOGA PARTY |
| 4 | LDDS LD ZESDQ SGD SNFZ OZQSX |
| 5 | KCCR KC YDRCP RFC RMEY NYPRW |
| 6 | JBBQ JB XCQBO QEB QLDX MXOQV |
| 7 | IAAP IA WBPAN PDA PKCW LWNPU |
| 8 | HZZO HZ VAOZM OCZ OJBV KVMOT |
| 9 | GYYN GY UZNYL NBY NIAU JULNS |
| 10 | FXXM FX TYMXK MAX MHZT ITKMR |
| 11 | EWWL EW SXLWJ LZW LGYS HSJLQ |
| 12 | DVVK DV RWKVI KYV KFXR GRIKP |
| 13 | CUUJ CU QVJUH JXU JEWQ FQHJO |
| 14 | BTTI BT PUITG IWT IDVP EPGIN |
| 15 | ASSH AS OTHSF HVS HCUO DOFHM |
| 16 | ZRRG ZR NSGRE GUR GBTN CNEGL |
| 17 | YQQF YQ MRFQD FTQ FASM BMDFK |
| 18 | XPPE XP LQEPC ESP EZRL ALCEJ |
| 19 | WOOD WO KPDOB DRO DYQK ZKBDI |
| 20 | VNNC VN JOCNA CQN CXPJ YJACH |
| 21 | UMMB UM INBMZ BPM BWOI XIZBG |
| 22 | TLLA TL HMALY AOL AVNH WHYAF |
| 23 | SKKZ SK GLZKX ZNK ZUMG VGXZE |
| 24 | RJJY RJ FKYJW YMJ YTLF UFWYD |
| 25 | QIIX QI EJXIV XLI XSKE TEVXC |
Questions about the Caesar cipher
How do I decode a Caesar cipher without the key?
Use brute force. There are only 25 possible shifts, so the tool above tries all of them and ranks the results by how English the letters look. The correct one is almost always at the top. For very short messages, read the list yourself.
What is the formula of the Caesar cipher?
Number the letters A = 0 to Z = 25. Encryption is C = (p + k) mod 26 and decryption is p = (C − k) mod 26, where p is the plaintext letter, C the ciphertext letter and k the key (the shift).
What is the key of the Caesar cipher?
The key is the number of places each letter is shifted, from 1 to 25. Julius Caesar is said to have used 3. A shift of 0 or 26 leaves the text unchanged.
Is ROT13 a Caesar cipher?
Yes. ROT13 is a Caesar cipher with k = 13. Because 13 is half of 26, the same operation encrypts and decrypts.
Why is the Caesar cipher not secure?
The key space is tiny: 25 keys can be tried by hand in minutes. On top of that, letter frequencies stay the same, so the most common ciphertext letter is usually E shifted by k.
Is the Caesar cipher a substitution or a transposition cipher?
It is a monoalphabetic substitution cipher: every letter is replaced by another letter, and the same plaintext letter always becomes the same ciphertext letter. The order of the letters does not change.
What happens to spaces, digits and punctuation?
In the classic cipher only the letters A to Z are shifted. Spaces, digits and punctuation stay as they are, and so does the case of each letter. Textbooks often remove spaces to hide word lengths; you can do that before encrypting.