Skip to content

rishibaba018/smart-caesar-cipher

Repository files navigation

Smart Caesar Cipher Converter

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.

Tech Stack

  • Frontend: HTML, CSS, JavaScript
  • Backend (primary): Python + Flask + Pillow
  • Reference implementation: Java class (SmartCaesarCipher.java) with the same text and pixel-channel logic

Cipher Logic

Text & Numbers

For each character:

  • Uppercase letters A-Z are mapped into [0, 25] and shifted modulo 26.
  • Lowercase letters a-z are mapped into [0, 25] and shifted modulo 26.
  • Digits 0-9 are 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.py in /api/text) as a backend demonstration
  • Java (SmartCaesarCipher.java) as a reference class

Image Pixels (RGB)

Images are processed with Pillow in Python:

  1. Convert to RGBA.
  2. For each pixel (r, g, b, a):
    • We update each channel using modulo arithmetic:

[ \text{newChannel} = (\text{channel} + \text{shift}) \bmod 256 ]

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

Running the App

  1. Install dependencies (already listed in requirements.txt):

    pip install -r requirements.txt

    Or directly:

    pip install flask pillow
  2. Start the Flask server:

    python app.py
  3. Open the app in your browser:

    • Visit http://localhost:5000
  4. 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 .txt file.
    • 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.

Java Reference

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.

Educational Notes

  • 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 — Smart Caesar Cipher Converter

Text · Numbers · Images | Educational Cryptography Tool


Overview

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.


Project Structure

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)

How the "Smart" Cipher Logic Works

Text & Numbers

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)

Image Pixels

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

Installation & Running

Prerequisites

  • Python 3.9+
  • pip

Steps

# 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:5000

API Reference

POST /api/cipher/text

Encrypts or decrypts a text string.

Request Body (JSON):

{
  "text":  "Hello World 2025",
  "shift": 7,
  "mode":  "encrypt"
}

Response:

{
  "result": "Olssv Dvysk 9792"
}

POST /api/cipher/image

Encrypts or decrypts an image file at the pixel level.

Request: multipart/form-data

  • file — JPG or PNG image
  • shift — Integer shift key
  • mode"encrypt" or "decrypt"

Response:

{
  "image":  "<base64-encoded PNG>",
  "format": "png"
}

Java Reference Implementation

SmartCaesarCipher.java is a self-contained Java file that mirrors the Python logic exactly.

# Compile
javac SmartCaesarCipher.java

# Run demo
java SmartCaesarCipher

It implements:

  • cipherText(String text, int shift, boolean encrypt) — text/number cipher
  • cipherImage(String inputPath, String outputPath, int shift, boolean encrypt) — pixel cipher

Correctness Verification

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.


Security Disclaimer

⚠️ 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.


Tech Stack

Layer Technology
Backend Python 3 + Flask
Images Pillow (PIL)
Frontend HTML5 + CSS3 + Vanilla JS
Java Ref Java SE (AWT/ImageIO)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors