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