Skip to content

About

Educational text and image encryption project using Genetic Algorithms to evolve XOR keys with tournament selection, two-point crossover, and bit-flip mutation.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Encryption and Decryption of Text and Images Using Genetic Algorithms

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.

Project Objective

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

How It Works

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

Genetic Algorithm

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.

XOR Encryption

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

Repository Structure

.
├── 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/

Installation

git clone <your-repository-url>
cd <repository-folder>

python3 -m venv .venv
source .venv/bin/activate

pip install -r requirements.txt

On Windows:

.venv\Scripts\activate

Run the Text Demo

python main.py

Or 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.

Run the Image Demo

python main.py --image path/to/image.png

The 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.

Run Tests

python -m unittest discover tests

The tests verify:

  1. text encryption/decryption round-trip
  2. image encryption/decryption round-trip

Evaluation

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

Differences From the Original Notebook

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

Technologies

  • Python
  • NumPy
  • OpenCV
  • DEAP
  • Matplotlib
  • Genetic Algorithms
  • XOR operations

Limitations

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.

Author

Meghana Baladari

M.S. Data Science, Analytics and Engineering
Arizona State University

About

Educational text and image encryption project using Genetic Algorithms to evolve XOR keys with tournament selection, two-point crossover, and bit-flip mutation.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages