Image encryption and Decryption based on private-key cryptography
- Step 1: User enters a password:
password - Step 2: The
passwordis encoded inutf-8followed by generating asha256 hash - Step 3: The
sha256 hashis converted tohex - Step 4: This
hexvalues are used to generate an array of integer values - Step 5: The array is divided into four
key tuples, each containing 4 integers. - Step 6: Each key tuple is used to encode/ decode the image. The number of times, the recursive encryption/decryption takes place for a key tuple is called the degree of the key tuple.
- Step 7: Using the key tuples, the color is also encoded using a XOR cipher
Let's say we have a key tuple [a, b, c, d] , codec function f(x) and degree = n
| ith degree | Key tuple | Process |
|---|---|---|
| 0 | [a, b, c, d] | nothing |
| 1 | [f(a), f(b), f(c), f(d)] | Swap Pixel f(a),f(b) with Pixel f(c),f(d) |
| 2 | [f(f(a)), f(f(b)), f(f(c)), f(f(d))] | Swap Pixel f(f(a)),f(f(b)) with Pixel f(f(c)),f(f(d)) |
| ... | ... | ... |
| n | [fn(a), fn(b), fn(c), fn(d)] | Swap pixel Pixel fn(a),fn(b) with Pixel fn(c),fn(d) |
Note: Decryption algorithm is simply the reversal of Encryption process.
-
Extract 3 values from a key tuple [a, b, c]
-
A single pixel consists of three values - (RED, GREEN, BLUE)
-
Here, we use XOR logical operation between each pixel parameter and key tuple value
-
NEW_RED = RED ^ a
-
NEW_GREEN = GREEN ^ b
-
NEW_BLUE = BLUE ^ c, where ^ is XOR
-
-
We replace the old pixel with the newly formed pixel (NEW_RED, NEW_GREEN, NEW_BLUE)
-
XOR has a special property which enables us to trace back the original pixel values without loss of data
-
a^b = c
-
c^b = a
-
-
Here, let
abe the pixel parameter andbbe the key tuple value. On XORing it, we obtaincand we store it. -
For decryption, XORing
cwith key tuple valuebwill give us the original pixel valuea. -
Each pixel value ranges from 0 to 255. Hence the modulus of 256 is applied over final results after XORing.
max_val(ht,wdth): returns max(ht,wdth)get_string_hash(): returns the hashvalue for the entered passwordgenerate_tuples(H,W): generates a list of tuples recursively for the codecyield_chunks(block, iterate_size): returns lists of varying size for the hash list
Encodes the image at image_path as per the entered password
Decodes the image at image_path as per the entered password
fucntion(x): The first function for generating tuplesfunction2(x): The second function for generating tuplesswap(ai,aj,bi,bj, image, arr): swaps two pixelsarr(ai,aj)witharr(bi, bj)efficiency(orig, enco, W, H): Finds the efficiency of the encryption by comparing the original image with encoded image, W and H are width ad height respectivelycascade(xy, N, W, H): creates a recursively cascaded list (of length N) of tuplesautomate_swap(alpha, beta, N, image, arr): swaps pixels automatically for encryptionautomate_swap_dec(alpha, beta, N, image, arr): swaps pixels automatically for decryptioncolor(arr, val, W, H): Encrypts the color of each pixel against key tuple using XOR operation
git clone https://github.com/NITDgpOS/SecureSnaps.git
cd SecureSnaps
sudo bash install
- To encode:
ssnaps -e <image_path> - To decode:
ssnaps -d <image_path>
Check the Contribution Guidelines here