Smart Caesar Cipher is an educational web app that demonstrates how a symmetric Caesar-style cipher can be applied to:
- Text (letters + digits, with symbols preserved)
- Numerical strings
- Images (by shifting RGB pixel values)
It lets you both encrypt and decrypt using the same key. A shift of +K followed by a shift of -K returns the original data.
⚠️ Security disclaimer: This is a learning tool, not a secure encryption system. Do not use it to protect real secrets.
- Frontend: HTML, CSS, JavaScript
- Backend (primary): Python + Flask + Pillow
- Reference implementation: Java class (
SmartCaesarCipher.java) with the same text and pixel-channel logic
For each character:
- Uppercase letters
A-Zare mapped into [0, 25] and shifted modulo 26. - Lowercase letters
a-zare mapped into [0, 25] and shifted modulo 26. - Digits
0-9are mapped into [0, 9] and shifted modulo 10. - Symbols and whitespace are left unchanged.
The shift is:
[ \text{newIndex} = (\text{oldIndex} + \text{shift}) \bmod N ]
Where:
- (N = 26) for letters
- (N = 10) for digits
Decrypting simply uses the negative of the key:
- Encrypt:
shift = +key - Decrypt:
shift = -key
This logic is implemented in:
- JavaScript (
app.js) for immediate text transformations in the browser - Python (
app.pyin/api/text) as a backend demonstration - Java (
SmartCaesarCipher.java) as a reference class
Images are processed with Pillow in Python:
- Convert to
RGBA. - For each pixel
(r, g, b, a):- We update each channel using modulo arithmetic:
[ \text{newChannel} = (\text{channel} + \text{shift}) \bmod 256 ]
- Alpha (
a) is kept unchanged.
- Encrypt uses
+key. - Decrypt uses
-key.
Because of the modulo operation, a shift of +K followed by -K recovers the exact original pixel values, satisfying the data integrity requirement.
The same channel logic is mirrored in Java in SmartCaesarCipher.shiftChannel.
-
Install dependencies (already listed in
requirements.txt):pip install -r requirements.txt
Or directly:
pip install flask pillow
-
Start the Flask server:
python app.py
-
Open the app in your browser:
- Visit
http://localhost:5000
- Visit
-
Usage
- Choose a Key (shift value).
- Choose Encrypt or Decrypt.
- Use the Text & Numbers tab to:
- Type/paste input text.
- Run the cipher.
- Download the output as a
.txtfile.
- Use the Image tab to:
- Upload a JPG/PNG.
- Run the cipher to visibly scramble it.
- Download the processed PNG.
- Re-upload the processed image with the same key and opposite mode to restore it.
SmartCaesarCipher.java provides:
transformText(String, int, Mode)— same text/number shifting rules as in the JS/Python code.shiftChannel(int, int, Mode)— the same modulo 256 math used for pixel channels.
This is useful if you want to port the cipher logic into a Java desktop app or backend.
- This is a symmetric cipher: the same key is used for encryption and decryption.
- The algorithm is intentionally simple so you can:
- Inspect the source code.
- Experiment with different keys.
- See how changing the key affects both text and images.
CipherX is a full-stack web application that performs two-way encryption and decryption on text strings, numeric data, and image files using a modified Caesar Cipher algorithm. The "smart" logic adapts to the data type automatically.
caesar_cipher/
├── app.py ← Python/Flask backend (API + server)
├── SmartCaesarCipher.java ← Java reference implementation (standalone)
├── requirements.txt ← Python dependencies
├── README.md ← This file
├── templates/
│ └── index.html ← Main HTML template
└── static/
├── css/style.css ← Stylesheet (retro-terminal aesthetic)
└── js/main.js ← Frontend JavaScript (API calls, UI logic)
| Character Type | Shift Logic | Why |
|---|---|---|
| A–Z / a–z | (char - base + shift) % 26 |
Wraps within its own case boundary |
| 0–9 | (digit + shift) % 10 |
Wraps within the decimal range |
| Symbols/Spaces | Unchanged | Preserves formatting and readability |
Example with shift = 7:
Hello 2025!→Olssv 9792!H→O,e→l,2→9,0→7,!→!(symbol unchanged)
Each pixel's RGB channels (values 0–255) are shifted independently:
new_R = (R + shift) % 256
new_G = (G + shift) % 256
new_B = (B + shift) % 256
Why Modulo 256?
- Pixel values must stay in the 0–255 range
- Modulo wraps around: if R=250 and shift=10, new R = (250+10)%256 = 4
- Decryption uses negative shift:
(R - shift + 256) % 256, restoring the exact original
- Python 3.9+
- pip
# 1. Navigate to project folder
cd caesar_cipher
# 2. Install Python dependencies
pip install -r requirements.txt
# 3. Start the Flask server
python app.py
# 4. Open browser at:
http://localhost:5000Encrypts or decrypts a text string.
Request Body (JSON):
{
"text": "Hello World 2025",
"shift": 7,
"mode": "encrypt"
}Response:
{
"result": "Olssv Dvysk 9792"
}Encrypts or decrypts an image file at the pixel level.
Request: multipart/form-data
file— JPG or PNG imageshift— Integer shift keymode—"encrypt"or"decrypt"
Response:
{
"image": "<base64-encoded PNG>",
"format": "png"
}SmartCaesarCipher.java is a self-contained Java file that mirrors the Python logic exactly.
# Compile
javac SmartCaesarCipher.java
# Run demo
java SmartCaesarCipherIt implements:
cipherText(String text, int shift, boolean encrypt)— text/number ciphercipherImage(String inputPath, String outputPath, int shift, boolean encrypt)— pixel cipher
A round-trip test ensures data integrity:
encrypt(text, +N) → decrypt(result, +N) == original ✓
encrypt(image, +N) → decrypt(result, +N) == original ✓
Both Python and Java implementations include built-in demos that verify this property.
⚠️ CipherX uses a symmetric substitution cipher for educational purposes only. It is NOT cryptographically secure and should NOT be used to protect sensitive data. A shift cipher is vulnerable to frequency analysis and brute-force attacks. For real security, use AES-256 or other modern cryptographic standards.
| Layer | Technology |
|---|---|
| Backend | Python 3 + Flask |
| Images | Pillow (PIL) |
| Frontend | HTML5 + CSS3 + Vanilla JS |
| Java Ref | Java SE (AWT/ImageIO) |