Skip to content

Caesar Cipher (Cifra de César)

The Caesar cipher is one of the simplest and most well-known encryption techniques in history. Named after Julius Caesar, who used it for military communications around 58 BC, this substitution cipher shifts each letter by a fixed number of positions down the alphabet.

Mathematical Foundation

Shift Operation

Given: - A plaintext message $M$ - A shift key $k$ (typically an integer between 1 and 25)

The encryption function is:

$$E_k(x) = (x + k) \mod 26$$

Where $x$ represents the position of a letter in the alphabet ($A=0, B=1, ..., Z=25$).

Decryption

Decryption reverses the shift:

$$D_k(x) = (x - k) \mod 26$$

Example

With a shift of $k = 3$:

Plaintext A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Ciphertext D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

Plaintext: HELLO Ciphertext: KHOOR (H→K, E→H, L→O, L→O, O→R)

Java Implementation

public class CaesarCipher {
    private static final int ALPHABET_SIZE = 26;

    /**
     * Encrypts a message using the Caesar cipher.
     * @param plaintext The original message
     * @param shift The number of positions to shift (1-25)
     * @return The encrypted message
     */
    public static String encrypt(String plaintext, int shift) {
        StringBuilder ciphertext = new StringBuilder();

        for (char c : plaintext.toCharArray()) {
            if (Character.isLetter(c)) {
                char base = Character.isUpperCase(c) ? 'A' : 'a';
                int position = c - base;
                int shiftedPosition = (position + shift) % ALPHABET_SIZE;
                ciphertext.append((char)(base + shiftedPosition));
            } else {
                ciphertext.append(c); // Preserve non-alphabetic characters
            }
        }

        return ciphertext.toString();
    }

    /**
     * Decrypts a message encrypted with the Caesar cipher.
     * @param ciphertext The encrypted message
     * @param shift The number of positions that were shifted
     * @return The decrypted message
     */
    public static String decrypt(String ciphertext, int shift) {
        StringBuilder plaintext = new StringBuilder();

        for (char c : ciphertext.toCharArray()) {
            if (Character.isLetter(c)) {
                char base = Character.isUpperCase(c) ? 'A' : 'a';
                int position = c - base;
                int originalPosition = (position - shift + ALPHABET_SIZE) % ALPHABET_SIZE;
                plaintext.append((char)(base + originalPosition));
            } else {
                plaintext.append(c); // Preserve non-alphabetic characters
            }
        }

        return plaintext.toString();
    }

    public static void main(String[] args) {
        String message = "HELLO WORLD";
        int shift = 3;

        String encrypted = encrypt(message, shift);
        System.out.println("Plaintext:  " + message);
        System.out.println("Ciphertext: " + encrypted);

        String decrypted = decrypt(encrypted, shift);
        System.out.println("Decrypted:  " + decrypted);
    }
}

Cryptanalysis: Frequency Analysis

The Caesar cipher is vulnerable to frequency analysis, a technique that exploits the fact that certain letters appear more frequently in natural language.

English Letter Frequencies (Approximate)

Letter Frequency Letter Frequency
E 12.7% T 9.1%
A 8.2% O 7.5%
I 7.0% N 6.7%
S 6.3% H 6.1%
R 6.0% D 4.3%

Breaking the Cipher

To break a Caesar cipher:

  1. Count the frequency of each letter in the ciphertext
  2. Find the most frequent letter
  3. Assume it corresponds to 'E' (most common English letter)
  4. Calculate the shift: $k = \text{position}(\text{ciphertext_letter}) - \text{position}(E)$

Example Attack

Ciphertext: KHOOR ZRUOG

  1. Most frequent letter: O (appears 3 times)
  2. Assume O → E
  3. Shift calculation: $O(14) - E(4) = 10$
  4. Decrypt with shift of 10: HELLO WORLD

Security Analysis

Strengths and Weaknesses

Aspect Assessment
Key Space Only 25 possible keys (trivial to brute force)
Complexity O(n) time, O(1) space
Vulnerability Completely breakable with frequency analysis
Modern Use Educational only - never for actual security

Why It's Still Taught

Despite being insecure, the Caesar cipher is valuable for learning:

  1. Substitution Ciphers: Foundation for understanding more complex ciphers
  2. Frequency Analysis: Introduction to cryptanalytic techniques
  3. Modular Arithmetic: Practical application of modular operations in cryptography
  4. Historical Context: Understanding how encryption evolved over time

Extensions and Variations

ROT13

A special case where the shift is 13:

$$E_{13}(x) = (x + 13) \mod 26$$

ROT13 is its own inverse ($D_{13} = E_{13}$), making it useful for hiding spoilers in online forums.

Caesar with Extended Alphabet

Some variations include numbers and symbols, expanding the alphabet size and increasing complexity (though still vulnerable to frequency analysis).

Historical Significance

While Julius Caesar's original implementation used a shift of 3, historical evidence suggests he may have used different shifts depending on the recipient. The cipher was considered secure at the time because:

  1. Encryption methods were not widely known
  2. Messages were short and context-dependent
  3. There were no automated cryptanalysis tools

Modern Cryptographic Principles Demonstrated

The Caesar cipher illustrates several fundamental concepts that remain relevant in modern cryptography:

  • Confusion: The relationship between plaintext and ciphertext should be obscured
  • Diffusion: Changes in plaintext should affect multiple parts of the ciphertext
  • Key Management: Even simple ciphers require secure key handling

However, the Caesar cipher fails to achieve these principles properly. Modern cryptographic algorithms like AES implement confusion and diffusion through complex mathematical operations that make frequency analysis ineffective.

References

  1. Stinson, D. R. (2005). Cryptography: Theory and Practice. CRC Press.
  2. Trappe, W., & Washington, L. C. (2006). Introduction to Cryptography with Coding Theory. Pearson.
  3. Kahn, D. (1967). The Codebreakers: The Story of Secret Writing. Scribner.