An educational Python project demonstrating how a Genetic Algorithm (GA) can be used to evolve byte keys for reversible XOR-based encryption and decryption of text and grayscale images.
The project preserves the original academic methodology—population initialization, tournament selection, two-point crossover, mutation, and iterative evolution—while refactoring the implementation into a reproducible and testable repository.
Important: This repository is an academic demonstration of evolutionary computation and reversible XOR operations. It is not intended to replace modern authenticated cryptography such as AES-GCM or ChaCha20-Poly1305.
The original project explores secure handling of text and image data using a Genetic Algorithm to optimize encryption keys. The accompanying synopsis describes:
- grayscale image preprocessing
- flattening image pixels
- tournament selection
- two-point crossover
- mutation
- evolutionary optimization
- XOR-based reversible encryption/decryption
Input text / image
↓
Convert to bytes / grayscale pixels
↓
Initialize population of candidate byte keys
↓
Evaluate candidate keys
↓
Tournament selection
↓
Two-point crossover
↓
Mutation
↓
Repeat for N generations
↓
Select best evolved key
↓
XOR encryption
↓
XOR with same key
↓
Original data recovered
Each individual represents a candidate encryption key containing integer byte values from 0 to 255.
The cleaned implementation uses:
- Selection: tournament selection
- Crossover: two-point crossover
- Mutation: byte-level bit-flip mutation (0.05 probability per gene)
- Default population: 50 individuals
- Default generations: 20
- Default crossover probability: 0.70
- Default mutation probability: 0.20
- Per-gene mutation probability: 0.05
The original notebook used a random fitness score, which meant the GA was not actually optimizing encryption quality. In this refactored version, the fitness function is deterministic and combines:
- ciphertext Shannon entropy
- histogram uniformity
- ciphertext byte diversity
These are useful educational heuristics, but they are not proofs of cryptographic security.
For each input byte:
cipher_byte = plaintext_byte XOR key_byte
Decryption repeats the same operation:
plaintext_byte = cipher_byte XOR key_byte
because:
(A XOR B) XOR B = A
.
├── main.py
├── requirements.txt
├── README.md
├── LICENSE
├── src/
│ ├── __init__.py
│ ├── crypto.py
│ ├── ga.py
│ └── metrics.py
├── tests/
│ └── test_crypto.py
├── docs/
│ ├── Encryption-and-Decryption-Using-Genetic-Algorithms.pdf
│ └── Synopsis.docx
├── original/
│ └── encryption_original.ipynb
└── outputs/
git clone <your-repository-url>
cd <repository-folder>
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtOn Windows:
.venv\Scripts\activatepython main.pyOr provide your own text:
python main.py --text "Sensitive information"Example output contains:
Original : Sensitive information
Encrypted (Base64): <ciphertext>
Decrypted: Sensitive information
Recovered exactly: True
Because the ciphertext is binary data, it is displayed using Base64.
python main.py --image path/to/image.pngThe image is loaded in grayscale and the program writes:
outputs/encrypted_image.png
outputs/decrypted_image.png
The program also verifies that the decrypted image exactly matches the original.
python -m unittest discover testsThe tests verify:
- text encryption/decryption round-trip
- image encryption/decryption round-trip
For reversible decryption, the strongest correctness check is exact recovery:
decrypted == original
For images, the program also reports:
- MSE between original and decrypted image
- PSNR between original and decrypted image
- encrypted-image byte entropy
For exact reconstruction:
MSE = 0
PSNR = infinity
The original notebook is preserved under original/.
The refactored implementation fixes several reproducibility issues:
- removes dependence on a hard-coded
timetable.png - separates GA, encryption, and metrics into modules
- replaces the original notebook's random placeholder fitness with a deterministic ciphertext-quality heuristic
- avoids recreating DEAP classes multiple times
- provides CLI arguments
- adds automated tests
- handles text ciphertext safely as bytes/Base64
- documents limitations honestly
- Python
- NumPy
- OpenCV
- DEAP
- Matplotlib
- Genetic Algorithms
- XOR operations
This project demonstrates concepts rather than production cryptography.
A repeating XOR key can be vulnerable to cryptanalysis, and entropy-oriented optimization does not establish key strength, semantic security, authentication, or resistance to modern attacks.
For real applications, use reviewed cryptographic libraries and modern authenticated encryption.
Meghana Baladari
M.S. Data Science, Analytics and Engineering
Arizona State University