A Python-based image encryption and decryption tool that manipulates pixel values using a mathematical XOR (Bitwise Exclusive OR) operation with a user-defined key.
This project demonstrates basic cryptographic principles applied to computer graphics and digital imagery. By iterating through each pixel in an RGB image and applying a bitwise XOR operation with a 1-byte numeric key (0 to 255), the original image data is scrambled into an unrecognizable encrypted state. Applying the same key again reverses the operation and perfectly restores the original image.
- Symmetric Encryption/Decryption: Uses XOR arithmetic, meaning the exact same process with the same key restores the original image.
- RGB Channel Support: Manipulates all three color channels (Red, Green, Blue) across the whole image dimension.
- Customizable Output Format: Saves processed images to formats like PNG, JPG, JPEG.
- Language: Python 3.x
- Libraries: Pillow (PIL)
Make sure you have Pillow installed:
pip install pillow- Place your input image into the
task2folder (e.g.,original.pngororiginal.jpg). - Run the script:
python main.py- Follow the interactive prompts:
- Select operation:
1for encryption or2for decryption. - Enter a key: An integer between
0and255. - Enter output file name (e.g.,
encrypted.pngordecrypted.png).
- Select operation:
-
Encrypting an Image:
Choose your preference 1. encryption 2. decryption Your choice: 1 Give a key (must be from 0 to 255): 123 Give a name to your image with image type(png,jpg,jpeg): encrypted.pngOutput:
image saved successfully as encrypted.png -
Decrypting the Image:
Choose your preference 1. encryption 2. decryption Your choice: 2 Give a key (must be from 0 to 255): 123 Give a name to your image with image type(png,jpg,jpeg): decrypted.pngOutput:
image saved successfully as decrypted.png
task2/
├── main.py # Python script for image encryption/decryption
├── original.jpg # Sample input image
└── README.md # Documentation
- When saving encrypted images, it is recommended to use lossless formats such as PNG to prevent lossy compression algorithms (like JPEG) from altering pixel values and causing decryption artifacts.