savemyday

Caesar cipher tool

Encrypt and decrypt text using the Caesar cipher - a classical substitution cipher where each letter is shifted by a fixed number of positions in the alphabet. Configure the shift value and switch between encryption and decryption modes.

Enter your text and watch the cipher transform in real time. Adjust the shift and mode to encrypt or decrypt.

Settings

Configure the cipher

Shift (Displacement)
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

Result of Caesar cipher transformation.

What is the Caesar cipher?

The Caesar cipher is one of the earliest known encryption techniques. It works by replacing each letter in the plaintext with a letter a fixed number of positions down the alphabet. For example, with a shift of 3:

  • A becomes D
  • B becomes E
  • C becomes F

Named after Julius Caesar, who reportedly used this method to communicate with his generals, it is technically a monoalphabetic substitution cipher.

How it works: Each letter of the plaintext is shifted by the same number of positions (the “displacement” or “shift”) along the alphabet. The alphabet wraps around, so with a shift of 3, Z becomes C.

Mathematical representation:

For encryption with shift n: E(x) = (x + n) mod 26

For decryption with shift n: D(x) = (x - n) mod 26

Where x is the position of the letter in the alphabet (A=0, B=1, … Z=25).

How to use this tool

  1. Enter your text in the input field
  2. Select the shift value (1-25) - this is the number of positions each letter will be moved
  3. Choose the mode:
    • Encrypt: Transform readable text into cipher text
    • Decrypt: Transform cipher text back into readable text
  4. View the result in the output field below
  5. Copy the output using the copy button

The output updates in real time as you type or change settings.

Common use cases

  • Educational purposes: Understanding basic cryptography concepts
  • Puzzle solving: Cracking simple coded messages
  • CTF challenges: Cybersecurity competitions often use basic ciphers
  • Fun encryption: Hiding simple messages from casual readers
  • Algorithm demonstration: Teaching substitution cipher principles

Limitations

The Caesar cipher provides minimal security by modern standards. It can be easily broken using:

  1. Brute force: Only 25 possible shifts to try
  2. Frequency analysis: Letter patterns remain visible
  3. Known plaintext: Any known word reveals the shift value

For secure communication, use modern encryption like AES or RSA instead.

Pseudo code

/**
 * Caesar Cipher Implementation
 * Parameters: text, shift, mode
 * Returns: encrypted or decrypted text
 */

function caesarCipher(text, shift, mode) {
    const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let result = '';

    // Adjust shift based on mode
    if (mode === 'decrypt') {
        shift = 26 - shift;
    }

    for (let i = 0; i < text.length; i++) {
        let char = text[i];

        if (char.toUpperCase() === char) {
            // Uppercase letter
            let index = alphabet.indexOf(char);
            if (index !== -1) {
                result += alphabet[(index + shift) % 26];
            } else {
                result += char;
            }
        } else if (char.toLowerCase() === char) {
            // Lowercase letter
            let upper = alphabet.indexOf(char.toUpperCase());
            if (upper !== -1) {
                result += alphabet[(upper + shift) % 26].toLowerCase();
            } else {
                result += char;
            }
        } else {
            // Non-alphabetic character
            result += char;
        }
    }

    return result;
}

// --- Example Usage ---

// Encrypt with shift 3
console.log(caesarCipher('Hello World', 3, 'encrypt'));
// Output: Khoor Zruog

// Decrypt with shift 3
console.log(caesarCipher('Khoor Zruog', 3, 'decrypt'));
// Output: Hello World

Historical context

The Caesar cipher has been in use for approximately 2000 years. Despite its age, it remains a fundamental concept in cryptography education. Modern encryption algorithms like AES still use substitution principles, albeit with far more complexity and multiple rounds of transformation.