Skip to content

Caesar Cipher

CarterPerez-dev edited this page Feb 11, 2026 · 1 revision

Caesar Cipher

Classical encryption tool with brute-force cracking and frequency analysis.

Overview

A CLI tool implementing the Caesar cipher with encrypt, decrypt, and crack commands. The crack feature uses chi-squared frequency analysis to automatically determine the shift key by ranking all 26 possible decryptions against English letter frequency distributions.

Status: Complete | Difficulty: Beginner

Tech Stack

Technology Version Purpose
Python 3.12+ Core language
Typer - CLI framework
Rich - Terminal formatting and tables

Features

Core Functionality

  • Encrypt text with a given shift key
  • Decrypt text when the key is known
  • Brute-force crack by trying all 26 shifts
  • Chi-squared frequency analysis for ranking results
  • File I/O support

Security Relevance

  • ROT13 is still used in forums and email
  • Frequency analysis applies to other substitution ciphers in CTF challenges
  • Brute-forcing small key spaces applies to weak passwords and short PINs
  • Foundation for understanding why simple substitution ciphers fail

Architecture

User Command
    ↓
main.py (Typer CLI: encrypt, decrypt, crack)
    ↓
┌──────────────┬──────────────┬──────────────┐
│  cipher.py   │ analyzer.py  │ constants.py │
│  Encrypt/    │  Frequency   │  English     │
│  decrypt     │  analysis +  │  letter      │
│  logic       │  chi-squared │  frequencies │
└──────────────┴──────────────┴──────────────┘
    ↓
utils.py (File I/O, input validation)

Quick Start

cd PROJECTS/beginner/caesar-cipher

# Install dependencies
uv sync

# Encrypt
caesar-cipher encrypt "HELLO WORLD" --key 3
# Output: KHOOR ZRUOG

# Decrypt
caesar-cipher decrypt "KHOOR ZRUOG" --key 3
# Output: HELLO WORLD

# Crack without knowing the key
caesar-cipher crack "KHOOR ZRUOG"
# Shows ranked table of all 26 possible decryptions

Project Structure

caesar-cipher/
├── src/caesar_cipher/
│   ├── cipher.py         # Core encryption/decryption logic
│   ├── analyzer.py       # Frequency analysis for cracking
│   ├── constants.py      # English letter frequencies, alphabet
│   ├── main.py           # CLI commands
│   └── utils.py          # File I/O and input validation
├── tests/
└── pyproject.toml

Development

# Run tests
uv run pytest tests/ -v

# Linting
uv run ruff check .

# Format
uv run ruff format .

Source Code

View on GitHub

Clone this wiki locally