Skip to content

Classical cryptography toolkit with cipher implementations (Caesar, Affine, Monoalphabetic) and automated cryptanalysis using brute force attacks and statistical frequency analysis for breaking encryption.

License

Notifications You must be signed in to change notification settings

memo-13-byte/Classical-Cryptography-Toolkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” Classical Cryptography Toolkit

Python License Security Status

A comprehensive Python implementation of classical encryption ciphers and cryptanalysis tools

Features β€’ Installation β€’ Usage β€’ Examples β€’ Documentation


πŸ“‹ Table of Contents


🎯 Overview

This project demonstrates both the implementation of historical cryptographic algorithms and the techniques used to break them. It serves as an educational tool for understanding:

  • How classical encryption schemes work
  • Why simple substitution ciphers are insecure
  • Modern cryptanalysis techniques
  • The importance of key space and randomness

Perfect for: Security students, cryptography enthusiasts, and anyone learning about information security fundamentals.


✨ Features

πŸ”’ Cipher Implementations

  • βœ… Caesar Cipher - Classic shift-based substitution
  • βœ… Affine Cipher - Mathematical substitution using modular arithmetic
  • βœ… Monoalphabetic Substitution - Custom alphabet mapping

πŸ”“ Cryptanalysis Tools

  • βœ… Brute Force Attack - Exhaustive key search for Caesar and Affine ciphers
  • βœ… Frequency Analysis - Statistical attack on monoalphabetic ciphers
  • βœ… Dictionary Validation - Automated plaintext verification
  • βœ… Performance Optimized - Efficient algorithms for rapid analysis

πŸ’» Additional Features

  • βœ… Command-line interface with argparse
  • βœ… Support for both encryption and decryption
  • βœ… Comprehensive error handling
  • βœ… Pure Python implementation (no external crypto libraries)
  • βœ… Detailed logging of cryptanalysis results

πŸ” Supported Ciphers

1. Caesar Cipher

Algorithm: E(x) = (x + k) mod 26

  • Key Space: 25 possible keys
  • Strength: Trivially breakable with brute force
  • Attack Time: < 1 second

2. Affine Cipher

Algorithm: E(x) = (ax + b) mod 26

  • Requirements: gcd(a, 26) = 1 (a must be coprime with 26)
  • Key Space: 12 Γ— 26 = 312 possible keys
  • Strength: Weak, vulnerable to frequency analysis
  • Attack Time: < 2 seconds

3. Monoalphabetic Substitution Cipher

Algorithm: Direct letter-to-letter substitution with custom alphabet

  • Key Space: 26! β‰ˆ 4 Γ— 10²⁢ possible keys
  • Strength: Moderate, but vulnerable to frequency analysis
  • Attack Time: 5-30 seconds (depending on text length)

πŸš€ Installation

Prerequisites

  • Python 3.8 or higher
  • No external dependencies required (uses only Python standard library)

Clone the Repository

git clone https://github.com/memo-13-byte/Classical-Cryptography-Toolkit.git
cd Classical-Cryptography-Toolkit

Verify Installation

python ciphers.py --help
python break.py --help

πŸ’‘ Usage

Encryption

Caesar Cipher

python ciphers.py caesar plaintext.txt e -s 13

Parameters:

  • caesar - Cipher type
  • plaintext.txt - Input file
  • e - Encrypt mode
  • -s 13 - Shift value (0-25)

Affine Cipher

python ciphers.py affine plaintext.txt e -a 5 -b 8

Parameters:

  • affine - Cipher type
  • -a 5 - Multiplicative key (must be coprime with 26)
  • -b 8 - Additive key (shift value)

Monoalphabetic Cipher

python ciphers.py mono plaintext.txt e -k QWERTYUIOPASDFGHJKLZXCVBNM

Parameters:

  • mono - Cipher type
  • -k - Substitution alphabet (26 unique letters)

Decryption

# Caesar
python ciphers.py caesar ciphertext.txt d -s 13

# Affine
python ciphers.py affine ciphertext.txt d -a 5 -b 8

# Monoalphabetic
python ciphers.py mono ciphertext.txt d -k QWERTYUIOPASDFGHJKLZXCVBNM

Cryptanalysis

Break encrypted messages automatically:

# Break Caesar Cipher
python break.py caesar encrypted.txt

# Break Affine Cipher
python break.py affine encrypted.txt

# Break Monoalphabetic Cipher (using frequency analysis)
python break.py mono encrypted.txt

Output: Successfully decrypted text saved to break_[cipher].txt


πŸ“š Examples

Example 1: Caesar Cipher

# Create plaintext file
echo "HELLO WORLD" > message.txt

# Encrypt with shift 3
python ciphers.py caesar message.txt e -s 3
# Output: KHOOR ZRUOG

# Decrypt
python ciphers.py caesar cipher_caesar.txt d -s 3
# Output: HELLO WORLD

# Break (without knowing the key)
python break.py caesar cipher_caesar.txt
# Automatically finds shift=3 and outputs: HELLO WORLD

Example 2: Affine Cipher

# Encrypt
python ciphers.py affine secret.txt e -a 5 -b 8
# Uses formula: E(x) = (5x + 8) mod 26

# Break with cryptanalysis
python break.py affine cipher_affine.txt
# Tests all 312 valid (a,b) combinations
# Outputs decrypted message to break_affine.txt

Example 3: Monoalphabetic Substitution

# Encrypt with custom alphabet
python ciphers.py mono document.txt e -k ZEBRASCDFGHIJKLMNOPQTUVWXY

# Attack with frequency analysis
python break.py mono cipher_mono.txt
# Analyzes letter frequencies
# Compares with English language statistics
# Outputs most likely decryption

πŸ”¬ Technical Details

Caesar Cipher Implementation

def encrypt_caesar(plaintext, shift):
    result = ""
    for char in plaintext.upper():
        if char.isalpha():
            result += chr((ord(char) - 65 + shift) % 26 + 65)
        else:
            result += char
    return result

Cryptanalysis Methodology

1. Brute Force (Caesar, Affine)

  • Try all possible keys
  • Validate each attempt against dictionary
  • Return first valid plaintext

2. Frequency Analysis (Monoalphabetic)

  • Calculate letter frequency in ciphertext
  • Compare with English letter distribution
  • Map most common ciphertext letters to most common English letters
  • Validate mapping with dictionary

3. Dictionary Validation

  • Load English word dictionary
  • Check if decrypted text contains valid words
  • Threshold: β‰₯60% valid words = successful decryption

πŸ“ Project Structure

Classical-Cryptography-Toolkit/
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ ciphers.py          # Cipher implementations (encryption/decryption)
β”‚   └── break.py            # Cryptanalysis tools
β”‚
β”œβ”€β”€ BBM465_HW1_2024_Fall.pdf  # Assignment specification
β”œβ”€β”€ report.pdf              # Technical report
β”œβ”€β”€ LICENSE                 # MIT License
β”œβ”€β”€ .gitignore             # Git ignore rules
└── README.md              # This file

πŸŽ“ Academic Context

Course: BBM 465 - Information Security Laboratory
Institution: Hacettepe University, Computer Engineering Department
Semester: Fall 2024
Topics Covered:

  • Classical cryptography
  • Cryptanalysis techniques
  • Security through obscurity (why it fails)
  • Mathematical foundations of cryptography

⚠️ Security Notice

WARNING: These ciphers are for educational purposes only and should NEVER be used for real-world encryption.

Why?

  • ❌ Trivially breakable with modern computing power
  • ❌ No protection against frequency analysis
  • ❌ Small key space (except monoalphabetic)
  • ❌ Deterministic (same plaintext β†’ same ciphertext)
  • ❌ No integrity protection
  • ❌ Vulnerable to known-plaintext attacks

For Production Use:

  • βœ… Use AES (Advanced Encryption Standard)
  • βœ… Use RSA for public key encryption
  • βœ… Use established cryptographic libraries (PyCryptodome, cryptography)
  • βœ… Never roll your own crypto in production!

🎯 Learning Outcomes

By studying this project, you will understand:

  1. Classical Cryptography Basics

    • How substitution ciphers work
    • The concept of keys and key space
    • Encryption vs. encoding
  2. Cryptanalysis Techniques

    • Brute force attacks
    • Statistical analysis (frequency analysis)
    • Dictionary attacks
    • Chosen plaintext attacks
  3. Security Principles

    • Why security through obscurity fails
    • The importance of large key spaces
    • Perfect secrecy (One-Time Pad)
    • Kerckhoffs's principle
  4. Python Programming

    • Command-line argument parsing
    • File I/O operations
    • Modular arithmetic
    • Algorithm optimization

🀝 Contributing

Contributions are welcome! Here's how you can help:

  1. Report Bugs - Open an issue describing the problem
  2. Suggest Features - Share ideas for improvements
  3. Submit Pull Requests - Fix bugs or add features
  4. Improve Documentation - Help make explanations clearer

Contribution Guidelines

  • Follow PEP 8 style guide for Python code
  • Add comments for complex algorithms
  • Include test cases for new features
  • Update README if adding new functionality

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

TL;DR: You can freely use, modify, and distribute this code, even for commercial purposes, as long as you include the original license.


πŸ‘€ Author

Mehmet Oğuz Kocadere

πŸ”— Related Projects


πŸ™ Acknowledgments

  • Hacettepe University - Computer Engineering Department
  • BBM 465 Course - Information Security Laboratory
  • Classical Cryptography - Historical cipher implementations from:
    • Julius Caesar (Caesar Cipher)
    • Al-Kindi (Frequency Analysis, 9th century)
    • Leon Battista Alberti (Polyalphabetic ciphers)

πŸ“Š Statistics

Python Lines of Code Commits Last Commit


⭐ Star this repository if you found it helpful!

Made with ❀️ for cybersecurity education

About

Classical cryptography toolkit with cipher implementations (Caesar, Affine, Monoalphabetic) and automated cryptanalysis using brute force attacks and statistical frequency analysis for breaking encryption.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages