X Xerobit

Caesar Cipher Explained — ROT13, ROT47, and Shift Ciphers

The Caesar cipher shifts letters by a fixed number of positions. ROT13 is a Caesar cipher with shift 13. Learn how Caesar ciphers work, ROT13 vs ROT47, implementations in...

Mian Ali Khalid · · 4 min read
Use the tool
ROT13 / Caesar Cipher
Encode and decode ROT13 and arbitrary Caesar shifts. Letter frequency analysis. 100% client-side.
Open ROT13 / Caesar Cipher →

The Caesar cipher is the simplest substitution cipher — shift every letter by a fixed number. ROT13 is Caesar with shift 13, popular in programming because applying it twice restores the original.

Use the ROT13 Cipher Tool to encode and decode text instantly.

How Caesar cipher works

Shift = 3 (original Caesar cipher):
A → D    B → E    C → F    ...    Z → C
Plaintext:  HELLO WORLD
Ciphertext: KHOOR ZRUOG

ROT13 uses shift = 13. Since the alphabet has 26 letters, ROT13 is self-inverse: encoding twice returns the original.

Caesar cipher implementations

JavaScript

function caesarCipher(text, shift) {
  shift = ((shift % 26) + 26) % 26; // Normalize to 0-25
  
  return text.replace(/[A-Za-z]/g, char => {
    const base = char >= 'a' ? 97 : 65;
    return String.fromCharCode(((char.charCodeAt(0) - base + shift) % 26) + base);
  });
}

caesarCipher('Hello World', 3)   // 'Khoor Zruog'
caesarCipher('Khoor Zruog', -3)  // 'Hello World'
caesarCipher('Khoor Zruog', 23)  // 'Hello World' (26-3=23)

// ROT13 is shift 13:
caesarCipher('Hello World', 13)  // 'Uryyb Jbeyq'
caesarCipher('Uryyb Jbeyq', 13)  // 'Hello World'

Python

def caesar_cipher(text: str, shift: int) -> str:
    shift = shift % 26
    result = []
    for char in text:
        if char.isalpha():
            base = ord('A') if char.isupper() else ord('a')
            shifted = (ord(char) - base + shift) % 26 + base
            result.append(chr(shifted))
        else:
            result.append(char)
    return ''.join(result)

caesar_cipher('Hello World', 3)   # 'Khoor Zruog'
caesar_cipher('Khoor Zruog', -3)  # 'Hello World'

# Built-in ROT13 in Python:
import codecs
codecs.encode('Hello World', 'rot_13')  # 'Uryyb Jbeyq'

Go

func caesarCipher(text string, shift int) string {
    shift = ((shift % 26) + 26) % 26
    result := make([]byte, len(text))
    for i, b := range []byte(text) {
        switch {
        case b >= 'A' && b <= 'Z':
            result[i] = 'A' + (b-'A'+byte(shift))%26
        case b >= 'a' && b <= 'z':
            result[i] = 'a' + (b-'a'+byte(shift))%26
        default:
            result[i] = b
        }
    }
    return string(result)
}

ROT47 — extends to printable ASCII

ROT47 rotates all printable ASCII characters (codes 33–126), not just letters:

function rot47(text) {
  return text.replace(/[\x21-\x7E]/g, char => {
    return String.fromCharCode(((char.charCodeAt(0) - 33 + 47) % 94) + 33);
  });
}

rot47('Hello World!')  // '96==@ (@C=5P'
rot47('96==@ (@C=5P')  // 'Hello World!'

ROT47 encodes numbers, punctuation, and symbols too — useful when you need to obfuscate non-letter characters.

Brute-force Caesar decoder

With only 25 possible shifts, brute force finds the plaintext:

function bruteForce(ciphertext) {
  return Array.from({ length: 26 }, (_, shift) => ({
    shift,
    text: caesarCipher(ciphertext, -shift),
  }));
}

bruteForce('Khoor Zruog').forEach(({ shift, text }) => {
  console.log(`Shift ${shift}: ${text}`);
});
// ...
// Shift 3: Hello World
// ...

Frequency analysis

For longer texts, identify the cipher by letter frequency:

function letterFrequency(text) {
  const freq = {};
  for (const char of text.toUpperCase()) {
    if (char >= 'A' && char <= 'Z') {
      freq[char] = (freq[char] || 0) + 1;
    }
  }
  return Object.entries(freq).sort((a, b) => b[1] - a[1]);
}

// Most common letter in English is 'E'
// If ciphertext's most common letter is 'H', shift is likely 3
const freq = letterFrequency('Khoor zruog');
// [['R', 2], ['O', 2], ...] — 'R' and 'O' most frequent, suggests shift ~3

Security

The Caesar cipher is not secure — it’s trivially broken by:

  • Brute force (only 25 shifts)
  • Frequency analysis on longer text
  • Known-plaintext attack

Use it only for:

  • Obfuscating spoilers or puzzle solutions
  • Teaching cryptography concepts
  • Fun / recreational purposes (puzzle boxes, escape rooms)

Related posts

Related tool

ROT13 / Caesar Cipher

Encode and decode ROT13 and arbitrary Caesar shifts. Letter frequency analysis. 100% client-side.

Written by Mian Ali Khalid. Part of the Dev Productivity pillar.