Caesar cipher: encoder and decoder with steps

Academy

Encrypt or decrypt with any shift and see every step, or paste a message and let brute force find the key.

Covered inAKTU KCS074 Unit 1Anna University CB3491 Unit 1GTU 2170709 Unit 1NPTEL Cryptography and Network Security

C = (p + 3) mod 26

PlainABCDEFGHIJKLMNOPQRSTUVWXYZ
CipherDEFGHIJKLMNOPQRSTUVWXYZABC
Show your work, letter by letter
Letterpp ± kmod 26Result
M1212 + 3 = 1515 mod 26 = 15P
E44 + 3 = 77 mod 26 = 7H
T1919 + 3 = 2222 mod 26 = 22W
A00 + 3 = 33 mod 26 = 3D
F55 + 3 = 88 mod 26 = 8I
R1717 + 3 = 2020 mod 26 = 20U
H77 + 3 = 1010 mod 26 = 10K
O1414 + 3 = 1717 mod 26 = 17R
G66 + 3 = 99 mod 26 = 9J
P1515 + 3 = 1818 mod 26 = 18S
Y2424 + 3 = 2727 mod 26 = 1B

Rows marked in yellow wrap around: past Z the count starts again at A.

How it works

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.

PlainABCDEFGHIJKLMNOPQRSTUVWXYZ
CipherDEFGHIJKLMNOPQRSTUVWXYZABC

To calculate it, number the letters from A = 0 to Z = 25 and use modular arithmetic:

EncryptionC = (p + k) mod 26
Decryptionp = (C − k) mod 26

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.

Worked example

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.

Letterpp + kmod 26C
M1212 + 3 = 1515 mod 26 = 15P
E44 + 3 = 77 mod 26 = 7H
T1919 + 3 = 2222 mod 26 = 22W
A00 + 3 = 33 mod 26 = 3D
F55 + 3 = 88 mod 26 = 8I
R1717 + 3 = 2020 mod 26 = 20U
H77 + 3 = 1010 mod 26 = 10K
O1414 + 3 = 1717 mod 26 = 17R
G66 + 3 = 99 mod 26 = 9J
P1515 + 3 = 1818 mod 26 = 18S
Y2424 + 3 = 2727 mod 26 = 1B

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.

Exam corner

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

  1. Definition and type: monoalphabetic substitution, symmetric key k.
  2. Encryption and decryption formulas with A = 0 … Z = 25.
  3. Worked example with a table (plaintext letter, p, p + k, mod 26, ciphertext).
  4. Cryptanalysis: brute force over 25 keys; frequency analysis.
  5. Weaknesses: small key space, letter frequencies preserved; ROT13 as special case.
Lab program

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.

C · caesar.c
#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;
}
Java · Caesar.java
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));
    }
}
Python · caesar.py
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))
Expected output
Plaintext : MEET ME AFTER THE TOGA PARTY
Ciphertext: PHHW PH DIWHU WKH WRJD SDUWB
Decrypted : MEET ME AFTER THE TOGA PARTY
Practice

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

Cryptanalysis

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
1OGGV OG CHVGT VJG VQIC RCTVA
2NFFU NF BGUFS UIF UPHB QBSUZ
3MEET ME AFTER THE TOGA PARTY
4LDDS LD ZESDQ SGD SNFZ OZQSX
5KCCR KC YDRCP RFC RMEY NYPRW
6JBBQ JB XCQBO QEB QLDX MXOQV
7IAAP IA WBPAN PDA PKCW LWNPU
8HZZO HZ VAOZM OCZ OJBV KVMOT
9GYYN GY UZNYL NBY NIAU JULNS
10FXXM FX TYMXK MAX MHZT ITKMR
11EWWL EW SXLWJ LZW LGYS HSJLQ
12DVVK DV RWKVI KYV KFXR GRIKP
13CUUJ CU QVJUH JXU JEWQ FQHJO
14BTTI BT PUITG IWT IDVP EPGIN
15ASSH AS OTHSF HVS HCUO DOFHM
16ZRRG ZR NSGRE GUR GBTN CNEGL
17YQQF YQ MRFQD FTQ FASM BMDFK
18XPPE XP LQEPC ESP EZRL ALCEJ
19WOOD WO KPDOB DRO DYQK ZKBDI
20VNNC VN JOCNA CQN CXPJ YJACH
21UMMB UM INBMZ BPM BWOI XIZBG
22TLLA TL HMALY AOL AVNH WHYAF
23SKKZ SK GLZKX ZNK ZUMG VGXZE
24RJJY RJ FKYJW YMJ YTLF UFWYD
25QIIX QI EJXIV XLI XSKE TEVXC
FAQ

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.

Searches the list of pages on this site. What you type stays in your browser.