Course project for CS-416 (M) at IIT Bombay.
This package performs Paillier Homomorphic encryption [read more here] on images. Complete python implementation of this paper from scratch. This code contains encryption and decryption functions to be applied on images. It also contains basic image filters which can be applied to ciphered images to get desired changes in decrypted images.
Here, all the filters were applied on ciphered images and then decrypted.
Install all packages from requirements.txt as pip3 install -r requirements.txt
- Running sample driver code:
python main.py
- Running experiments:
- Select what function to run by changing the function name in main module
- Run python file as
python experiments.py
- generate_keypair(num_digit) : Generates a tuple of (private, public) of num_digit number of digits
- encrypt(public, plain): Encrypts the plain data using the public key and returns a tuple (encypted mantissa, plain text exponent)
- decrypt(private, public, cipher): Decrypts the tuple cipher data using the private and public key and returns plaintext message
- paillier_add(public, c1, c2): Add c1 cipher with c2 cipher where c1 and c2 are ciphers of integers using public key and return resulting cipher
- paillier_sub(public, c1, c2): Subtract c1 cipher from c2 cipher where c1 and c2 are ciphers of integers using public key and return resulting cipher
- paillier_mul(public, c1, m1): Multiply c1 cipher with m1 constant where c1 is ciphers of integer and m1 is an integer using public key and return resulting cipher
- new_paillier_add(public, c1, c2): Modified Paillier addition of c1 cipher with c2 cipher where c1 and c2 are ciphers of floating point real numbers using public key and return resulting cipher
- new_paillier_sub(public, c1, c2): Modified Paillier subtraction of c1 cipher from c2 cipher where c1 and c2 are ciphers of floating point real numbers using public key and return resulting cipher
- new_paillier_mul(public, c1, m1): Modified Paillier muultiplication of c1 cipher with m1 constant where c1 is ciphers of floating point real number and m1 is an floating point real numbers using public key and return resulting cipher
- invmod(a, p): Returns b where b is calculated such that a x b = 1(mod p) given a and p
- modpow(b, e, m): Returns value b^e(mod m) given b, e and m
- custom_frexp(num): Returns the tuple (mantissa, exponent) where num = mantissa x 2 ^ exponent where mantissa is an integer and exponent is a non-positive integer
- gkern(kernlen=3, nsig=3): Returns a numpy matrix of 2D gaussian kernal of dimension(kernlen x kernlen) with sigma as nsig
- merge_m_e(tup): Merge tuple tup of (encypted mantissa, plain text exponent) into one number and return it so that it can be used as an image pixel
- unmerge_m_e(pixel): Unmerge the number pixel into tuple of (encypted mantissa, plain text exponent) return it so that it can be used for cryptic operations
- Im_encrypt(public, plain_im): Encrypts original image plain_im using public key and returns encrypted numpy array of same shape as image
- Im_decrypt(private, public, cipher_im): Decrypts ciphered image cipher_im using private and public key and returns plaintext numpy array of same shape as image
- Brighten(public, cipher_im, factor): Adds brightness equal to factor pixelwise to ciphered image cipher_im using public key and returns new ciphered numpy array
- Negation(public, cipher_im): Adds negation filter pixelwise to ciphered image cipher_im using public key and returns new ciphered numpy array
- LPF(public, cipher_im, filter_type='linear', kernal_size=3): Adds low pass filter of type ['linear' or 'gaussian'] and kernal size kernal_size to ciphered image cipher_im using public key and returns new ciphered numpy array
- Sharpen(public, cipher_im, k): Adds sharpening equal to k pixelwise to ciphered image cipher_im using public key and returns new ciphered numpy array
- Dilation(public, cipher_im, kernal_size=3): Adds dilation of kernal size kernal_size to ciphered image cipher_im using public key and returns new ciphered numpy array. This filter should have a binary image as input to get better results.
- Edge(public, cipher_im): Shows the edges of the image from ciphered image cipher_im using public key and returns two new ciphered numpy arrays Gradient X and Gradient Y. User has to decrypt and take root of squared sum to obtain gradient in both directions.
- Hist_equal(public, H, img_size): Takes in pixel binned Histogram of original image H and image size img_size and returns pixel mapping function (a list) as output using the public key
Sample driver code
Sample code for experiments