A complete file encryption/decryption system using a 3-layer cryptographic pipeline:
- Playfair Cipher (substitution)
- Columnar Transposition (transposition)
- DES Block Cipher (block encryption)
pip install -r requirements.txtRun this ONCE to generate and save the system keys to .keys.json:
python -m backend.keys --initImportant: Keep .keys.json safe! It's in .gitignore but contains all encryption keys.
pip install pytest pytest-asyncio pytest-covpython app.pyThe application will start on http://localhost:8000
API Documentation:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
┌─────────────────────────────────────────────────────┐
│ ENCRYPTION FLOW │
├─────────────────────────────────────────────────────┤
│ │
│ Plaintext │
│ ↓ │
│ [Layer 1: Playfair Cipher] │
│ • 5×5 Polybius square │
│ • Digraph substitution │
│ • I/J merged (25 letters) │
│ ↓ │
│ [Layer 2: Columnar Transposition] │
│ • Column reordering via key │
│ • Row-write, column-read │
│ • Aligned with Playfair output │
│ ↓ │
│ [Layer 3: DES Block Cipher] │
│ • 8-byte blocks (64-bit key) │
│ • ECB mode operation │
│ • PKCS7 padding │
│ ↓ │
│ Ciphertext (Hex-encoded) │
│ │
└─────────────────────────────────────────────────────┘
Reverses all steps in order:
- DES decryption (hex → text)
- Columnar transposition reversal
- Playfair substitution reversal
Upload and encrypt a file.
Request:
multipart/form-data:
file: <binary data>
Response:
- Status 200: Encrypted file download
- Status 400/500: JSON error message
Allowed file types: .txt, .log, .csv, .json, .md
Example (curl):
curl -F "file=@plaintext.txt" http://localhost:5000/api/encrypt -o plaintext.txt.encryptedUpload and decrypt a file.
Request:
multipart/form-data:
file: <encrypted file>
Response:
- Status 200: Decrypted file download
- Status 400/500: JSON error message
Example (curl):
curl -F "file=@plaintext.txt.encrypted" http://localhost:5000/api/decrypt -o plaintext_decrypted.txtEncrypt a text string (useful for testing).
Request:
{
"text": "HELLO WORLD"
}Response:
{
"status": "success",
"plaintext": "HELLO WORLD",
"ciphertext": "hex-encoded-output...",
"size_increase": "2.5x"
}Decrypt a hex-encoded ciphertext.
Request:
{
"ciphertext": "hex-encoded-input..."
}Response:
{
"status": "success",
"plaintext": "HELLO WORLD",
"ciphertext": "hex-encoded-input..."
}Check system status and key information.
Response:
{
"status": "ready",
"pipeline": "3-layer (Playfair → Columnar → DES)",
"keys_loaded": true,
"playfair_key_length": 12,
"columnar_key_length": 9,
"des_key_length": 16,
"max_file_size_mb": 10.0
}from backend import pipeline
plaintext = "HELLO WORLD"
ciphertext = pipeline.encrypt_text(plaintext)
print(f"Encrypted: {ciphertext}")from backend import pipeline
ciphertext = "..." # from above
plaintext = pipeline.decrypt_text(ciphertext)
print(f"Decrypted: {plaintext}")from backend import file_io
# Encrypt with metadata
result = file_io.encrypt_file_with_metadata("input.txt", "outputs")
print(result)
# Decrypt with metadata
result = file_io.decrypt_file_with_metadata(result['output_file'])
print(result)Using Web Interface (FastAPI + Swagger UI):
- Start the server:
python app.py - Open
http://localhost:8000for the web interface - Or go to
http://localhost:8000/docsfor interactive API docs - Choose a file to encrypt or decrypt
- Downloaded encrypted/decrypted file
Or test with text:
- Enter text in "Test Text Encryption" section
- Click Encrypt/Decrypt
- Results shown immediately
File_Encrypt/
├── app.py # FastAPI web application
├── requirements.txt # Python dependencies
├── pytest.ini # Pytest configuration
├── .keys.json # Encryption keys (auto-generated, git-ignored)
│
├── backend/
│ ├── __init__.py
│ ├── keys.py # Key generation & loading
│ ├── playfair.py # Layer 1: Playfair cipher
│ ├── columnar.py # Layer 2: Columnar transposition
│ ├── des_cipher.py # Layer 3: DES block cipher
│ ├── pipeline.py # 3-layer pipeline orchestration
│ ├── file_io.py # File operations with metadata
│ └── format_preserving_encryption.py # Formatting preservation utility
│
├── frontend/
│ ├── static/
│ │ ├── app.js # JavaScript UI logic
│ │ └── style.css # CSS styling
│ └── templates/
│ └── index.html # Web interface
│
├── tests/ # Automated test suite (20 test files)
│ ├── test_api.py
│ ├── test_playfair_*.py # Playfair cipher tests
│ ├── test_encryption_flow.py
│ ├── test_decrypt_*.py
│ ├── test_formatting_preservation.py
│ ├── conftest.py # Pytest configuration & fixtures
│ ├── examples.py # Annotated test examples
│ └── ... (15 other test files)
│
├── debug/ # Debug & diagnostic scripts
│ ├── debug_pipeline.py
│ ├── debug_detailed.py
│ ├── debug_corruption.py
│ └── debug_playfair_issue.py
│
├── uploads/ # Temporary upload storage
├── outputs/ # Encrypted/decrypted files
├── REPOSITORY_STRUCTURE.md # Detailed folder organization guide
└── README.md # This file
---
## Testing
FastAPI provides excellent built-in testing support. All tests use the `TestClient` from FastAPI, requiring no live server.
### Running Tests
**Quick Start:**
```bash
# Run all tests
pytest
# Run with verbose output
pytest -v
# Run with coverage report
pytest --cov=backend --cov=app --cov-report=html
# Run tests matching pattern
pytest -k "encrypt" -v
# Run specific test file
pytest tests/test_playfair_known.py
Location: tests/ folder (20 automated test files)
Core Test Suites:
test_api.py- FastAPI endpoint tests (38 tests)test_playfair_*.py- Playfair cipher algorithm tests (6 files)test_playfair_known.py- Known plaintext-ciphertext pairstest_playfair_monarchy.py- Classic "MONARCHY" exampletest_playfair_roundtrip.py- Encryption/decryption round tripstest_playfair_direct.py- Direct cipher teststest_playfair_wrl.py- Word example tests
test_encryption_flow.py- End-to-end pipeline teststest_decrypt_*.py- Decryption-specific tests (3 files)test_formatting_preservation.py- Text formatting teststest_frontend_workflow.py- Frontend integration tests- Other specialized tests (13 total files)
Shared Fixtures:
conftest.py- Pytest configuration and shared test fixturesexamples.py- Annotated examples for learning
Location: debug/ folder (for manual investigation)
Debug scripts are NOT automated tests but tools for developers to investigate issues:
debug_pipeline.py- Trace encryption pipeline executiondebug_detailed.py- Detailed step-by-step debuggingdebug_corruption.py- Diagnose file corruption issuesdebug_playfair_issue.py- Playfair cipher debugging
Usage:
python debug/debug_pipeline.py
python debug/debug_detailed.py- 20+ automated test files
- All 5 API endpoints
- All 3 encryption layers (Playfair, Columnar, DES)
- Error handling & validation
- Round-trip encryption/decryption verification
- File operations & edge cases
- Frontend workflow integration
See REPOSITORY_STRUCTURE.md for complete folder organization and guidelines on when to use tests vs debug scripts.
How it works:
- Builds a 5×5 grid (Polybius square) from the key
- Processes plaintext as digraphs (letter pairs)
- Rules for each digraph:
- Same row: Shift right (wrap around)
- Same column: Shift down (wrap around)
- Rectangle: Swap columns
- Duplicates in pair: Insert 'X' filler
- Odd-length text: Pad with 'X'
Key: 12 random uppercase letters (no repeats)
Example:
Key: MONARCHY
Square:
M O N A R
C H Y B D
E F G I K
L P Q S T
U V W X Z
Plaintext: INSTRUMENTS
Encrypted: GFHFXGDGJIS
How it works:
- Takes Playfair output (uppercase letters)
- Determines column order from key alphabetical rank
- Writes plaintext row-by-row
- Reads columns in key-determined order
Key: 9 random uppercase letters
Example:
Key: SECRET
Column order: S(4) E(1) C(2) R(3) E(1) T(5)
→ sorted: E=1, C=2, R=3, S=4, T=5
Original (row-write):
S E C R E T
-----------
[row 1]
[row 2]
Read by column order (1,2,3,4,5):
Column 1, Column 2, Column 3, Column 4, Column 5
How it works:
- Industry-standard block cipher (DES)
- 64-bit blocks (8 bytes)
- 56-bit effective key (from 64-bit input)
- ECB mode (Electronic Codebook) for academic purposes
- PKCS7 padding for block alignment
Key: 16-character hex string (8 bytes)
Note: DES is legacy and only included for academic requirements. For production, use AES-256.
This system is designed for educational purposes and demonstrates:
- How symmetric encryption works
- Multi-layer cipher composition
- Block cipher operation
Not suitable for production due to:
- Playfair: Vulnerable to frequency analysis
- Columnar: Can be broken with large ciphertexts
- DES: 56-bit key is too small (breakable in hours with modern hardware)
For actual sensitive data, use:
- AES-256 instead of DES
- Modern block modes like CBC or GCM (not ECB)
- Key derivation functions (PBKDF2, Argon2)
- Message authentication (HMAC or authenticated encryption)
Solution: Run python -m backend.keys --init first
Solution: Maximum file size is 10 MB (configurable in app.py)
Possible causes:
- Wrong key file (regenerate with
--init) - File corrupted during transmission
- Unsupported file encoding
Check:
- Port 5000 is available:
netstat -an | findstr 5000 - Dependencies installed:
pip list | grep -E "Flask|pycryptodome" - Python version 3.7+ installed:
python --version
Encrypt file:
curl -F "file=@document.txt" \
http://localhost:8000/api/encrypt \
-o document.txt.encryptedDecrypt file:
curl -F "file=@document.txt.encrypted" \
http://localhost:8000/api/decrypt \
-o document.txtTest encryption:
curl -X POST http://localhost:8000/api/encrypt-text \
-H "Content-Type: application/json" \
-d '{"text":"HELLO"}' | jqCheck status:
curl http://localhost:8000/api/status | jqimport requests
# Upload file for encryption
with open('plaintext.txt', 'rb') as f:
files = {'file': f}
response = requests.post('http://localhost:8000/api/encrypt', files=files)
with open('plaintext.txt.encrypted', 'wb') as f:
f.write(response.content)
# Test text encryption
response = requests.post('http://localhost:8000/api/encrypt-text',
json={"text": "HELLO WORLD"})
result = response.json()
print(result['ciphertext_full'])python -m backend.pipelinepython -m backend.playfairpython -m backend.columnarpython -m backend.des_cipherpython -m backend.file_ioThis project uses FastAPI's built-in testing capabilities:
TestClient:
- Makes HTTP requests without running a server
- Provides full access to request/response details
- Can inspect headers, body, status codes
Pytest Integration:
@pytest.fixturefor reusable test data- Parametrized testing with
@pytest.mark.parametrize - Class-based test organization
- Setup/teardown with fixtures
Features Demonstrated:
- Testing endpoints with different HTTP methods
- File upload testing
- JSON request/response body testing
- Error status code validation
- Round-trip encryption/decryption verification
- Performance testing
- Fixture dependency injection
- Test cleanup and resources
How to write tests:
- See
tests/conftest.pyfor fixture patterns - See
tests/examples.pyfor annotated examples - See individual test files in
tests/for practical patterns
Using fixtures effectively:
- Fixtures defined in
conftest.pyare automatically available - Use
@pytest.fixturefor reusable test data - Dependency injection with fixture parameters
Testing patterns and best practices: Check REPOSITORY_STRUCTURE.md for detailed guidelines on:
- When to write automated tests vs debug scripts
- Test organization best practices
- How pytest discovers and runs tests
Parametrized testing:
Use @pytest.mark.parametrize decorator to test multiple inputs. Examples in test files.
Coverage reports:
pytest --cov=backend --cov=app --cov-report=html
# Opens coverage/ folder with detailed reportsCI/CD integration:
Tests are ready for GitHub Actions, GitLab CI, or Jenkins - just run pytest in your pipeline.
| Operation | Time | Notes |
|---|---|---|
| Encrypt 1KB text | 5-10ms | Playfair dominates |
| Encrypt 100KB text | 500-800ms | DES blocks (8 bytes) |
| Encrypt 1MB file | 5-8s | Network included |
| Decrypt 1MB file | 5-8s | Reverse pipeline |
- Playfair: 0-5% (substitution only, same length)
- Columnar: 0% (transposition only, same length)
- DES: ~30-35% (hex encoding: 1 byte → 2 chars + padding)
Final ciphertext is typically 2-3x larger than plaintext (due to hex encoding).
- Add AES-256 as Layer 3 option
- Implement CBC/GCM modes
- Add file compression before encryption
- Multi-file batch processing
- Key management UI (rotate, backup)
- Performance profiling & optimization
- Docker containerization
- REST API authentication (JWT)
- File streaming for large files (>100MB)
- Progress indicators for long operations
This project is for educational purposes. See LICENSE file for details.
For issues or questions:
- Check this README
- Review code comments
- Run diagnostic tests
- Check system status endpoint
- Samudyatha K Bhat
- Deeksha R
- Nicole Tabby
- Spoorthi R