A comprehensive Python implementation of classical encryption ciphers and cryptanalysis tools
Features β’ Installation β’ Usage β’ Examples β’ Documentation
- Overview
- Features
- Supported Ciphers
- Installation
- Usage
- Examples
- Technical Details
- Project Structure
- Academic Context
- Security Notice
- Contributing
- License
- Author
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.
- β Caesar Cipher - Classic shift-based substitution
- β Affine Cipher - Mathematical substitution using modular arithmetic
- β Monoalphabetic Substitution - Custom alphabet mapping
- β 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
- β
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
Algorithm: E(x) = (x + k) mod 26
- Key Space: 25 possible keys
- Strength: Trivially breakable with brute force
- Attack Time: < 1 second
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
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)
- Python 3.8 or higher
- No external dependencies required (uses only Python standard library)
git clone https://github.com/memo-13-byte/Classical-Cryptography-Toolkit.git
cd Classical-Cryptography-Toolkitpython ciphers.py --help
python break.py --helppython ciphers.py caesar plaintext.txt e -s 13Parameters:
caesar- Cipher typeplaintext.txt- Input filee- Encrypt mode-s 13- Shift value (0-25)
python ciphers.py affine plaintext.txt e -a 5 -b 8Parameters:
affine- Cipher type-a 5- Multiplicative key (must be coprime with 26)-b 8- Additive key (shift value)
python ciphers.py mono plaintext.txt e -k QWERTYUIOPASDFGHJKLZXCVBNMParameters:
mono- Cipher type-k- Substitution alphabet (26 unique letters)
# 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 QWERTYUIOPASDFGHJKLZXCVBNMBreak 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.txtOutput: Successfully decrypted text saved to break_[cipher].txt
# 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# 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# 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 decryptiondef 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- Try all possible keys
- Validate each attempt against dictionary
- Return first valid plaintext
- Calculate letter frequency in ciphertext
- Compare with English letter distribution
- Map most common ciphertext letters to most common English letters
- Validate mapping with dictionary
- Load English word dictionary
- Check if decrypted text contains valid words
- Threshold: β₯60% valid words = successful decryption
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
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
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!
By studying this project, you will understand:
-
Classical Cryptography Basics
- How substitution ciphers work
- The concept of keys and key space
- Encryption vs. encoding
-
Cryptanalysis Techniques
- Brute force attacks
- Statistical analysis (frequency analysis)
- Dictionary attacks
- Chosen plaintext attacks
-
Security Principles
- Why security through obscurity fails
- The importance of large key spaces
- Perfect secrecy (One-Time Pad)
- Kerckhoffs's principle
-
Python Programming
- Command-line argument parsing
- File I/O operations
- Modular arithmetic
- Algorithm optimization
Contributions are welcome! Here's how you can help:
- Report Bugs - Open an issue describing the problem
- Suggest Features - Share ideas for improvements
- Submit Pull Requests - Fix bugs or add features
- Improve Documentation - Help make explanations clearer
- 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
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.
Mehmet OΔuz Kocadere
- π Computer Engineering Student @ Hacettepe University
- π Focus: Cybersecurity, Cryptography, Network Security
- πΌ LinkedIn
- π§ Email: canmehmetoguz@gmail.com
- π GitHub: @memo-13-byte
- File Integrity Checker - RSA digital signatures
- Secure Flask Auth Portal - 2FA with OTP
- Hybrid Kerberos System - Enterprise authentication
- 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)
β Star this repository if you found it helpful!
Made with β€οΈ for cybersecurity education