Skip to content

AlaqmarG/genetic-cryptanalysis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧬 Genetic Cryptanalysis Project

Python License GitHub

πŸ“‹ Overview

This project uses a Genetic Algorithm to break Vigenere cipher encryption. It evolves populations of candidate keys to find those that produce decrypted text closest to English language patterns, measured by letter frequency analysis.

πŸ” Key Features:

  • Parallel GA execution with realtime visualization
  • Multiple crossover operators (Uniform & PMX)
  • Live plotting with progress tables
  • Automated result saving and analysis

πŸš€ Getting Started

πŸ“¦ Requirements

  • 🐍 Python 3.7+
  • πŸ“Š matplotlib
  • πŸ”’ numpy

Install dependencies:

pip install matplotlib numpy

🎯 Runnable Files

All commands run from the project root directory.

src/main.py

Command: python src/main.py
Description: Runs parallel GA experiments with realtime plotting. Tests 5 parameter sets across uniform/PMX crossover and both datasets. Opens matplotlib window with live fitness curves and progress tables. Saves results to timestamped folder in results/.
Runtime: ⏱️ ~10-30 minutes depending on hardware.

src/test.py

Command: python src/test.py
Description: Runs a single GA experiment with easily customizable parameters at the top of the file. Shows realtime plotting of fitness evolution (best and average curves) plus a live table of current generation, best, and average fitness. Saves results and plot to results/Tests/single_[timestamp]/.
Runtime: ⏱️ ~1-5 minutes depending on parameters.

πŸ“ Project Structure

genetic-cryptanalysis/
β”œβ”€β”€ πŸ“‚ data/                    # Encrypted datasets
β”‚   β”œβ”€β”€ Data1.txt              # 26-char key ciphertexts
β”‚   └── Data2.txt              # 40-char key ciphertexts
β”œβ”€β”€ πŸ“‚ src/                     # Source code
β”‚   β”œβ”€β”€ main.py                # 🎯 Main experiments
β”‚   └── util/                  # Utility modules
β”‚       β”œβ”€β”€ evaluation.py      # πŸ” Fitness functions
β”‚       β”œβ”€β”€ ga.py              # 🧬 GA implementation
β”‚       └── rt_plot.py         # πŸ“Š Realtime plotting
β”œβ”€β”€ πŸ“‚ results/                 # Output files
β”‚   └── experiment_*/          # Timestamped results
β”œβ”€β”€ πŸ“‚ references/              # Assignment materials
└── .gitignore

πŸ“– Usage

πŸ§ͺ Running Experiments

Execute the main experiment suite:

python src/main.py

What happens:

  • πŸ”„ Loads both datasets
  • 🧬 Runs GA with 5 parameter sets (a-e)
  • πŸ”€ Tests uniform and PMX crossover
  • πŸ“Š Displays realtime plots with fitness evolution
  • πŸ’Ύ Saves results to results/experiment_[timestamp]/

πŸ“Š Results Interpretation

πŸ† Fitness Score

Lower values = better keys (closer to English frequencies)

πŸ“‹ Parameter Sets

Set Crossover Rate Mutation Rate Notes
a 1.0 0.0 High crossover, no mutation
b 1.0 0.1 High crossover, moderate mutation
c 0.9 0.0 Moderate crossover, no mutation
d 0.9 0.1 Moderate crossover, moderate mutation
e 0.6 0.1 ⭐ Optimal (from search)

πŸ”„ Crossover Types

  • Uniform: Random gene swapping
  • PMX: Preserves relative ordering

πŸ“„ Datasets

  • Data1: Shorter 26-character keys
  • Data2: Longer 40-character keys

πŸ“ˆ Sample Output

Experiment results include:

  • πŸ… Best chromosomes (decrypted keys)
  • πŸ“ˆ Fitness evolution over generations
  • πŸ“Š Comparative performance tables
  • πŸ–ΌοΈ Final plot PNGs

Built by Alaqmar Gandhi for Brock University (COSC 3P71)

About

🧬 Genetic Algorithm for Vigenere Cipher Cryptanalysis - Parallel execution with realtime visualization and parameter optimization

Resources

Stars

Watchers

Forks

Contributors

Languages