Image encryption and Decryption based on private-key cryptography
password
password
is encoded in utf-8
followed by generating a sha256 hash
sha256 hash
is converted to hex
hex
values are used to generate an array of integer valueskey tuples
, each containing 4 integers.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 a
be the pixel parameter and b
be the key tuple value. On XORing it, we obtain c
and we store it.
For decryption, XORing c
with key tuple value b
will give us the original pixel value a
.
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 listEncodes 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 pixels arr(ai,aj)
with arr(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 operationgit clone https://github.com/NITDgpOS/SecureSnaps.git
cd SecureSnaps
sudo bash install
ssnaps -e <image_path>
ssnaps -d <image_path>
Check the Contribution Guidelines here