Skip to content

pranav-deo/Paillier-HME-filters-for-images

Repository files navigation

Paillier-HME-filters-for-images

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.

Examples:



Sample filter operations Sample filter operations

Here, all the filters were applied on ciphered images and then decrypted.

Setup:

Install all packages from requirements.txt as pip3 install -r requirements.txt

Run the code:

  1. Running sample driver code: python main.py
  2. Running experiments:
    1. Select what function to run by changing the function name in main module
    2. Run python file as python experiments.py

Function documentation:

1. crypt_utils.py:

  1. generate_keypair(num_digit) : Generates a tuple of (private, public) of num_digit number of digits
  2. encrypt(public, plain): Encrypts the plain data using the public key and returns a tuple (encypted mantissa, plain text exponent)
  3. decrypt(private, public, cipher): Decrypts the tuple cipher data using the private and public key and returns plaintext message
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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

2. math_utils.py:

  1. invmod(a, p): Returns b where b is calculated such that a x b = 1(mod p) given a and p
  2. modpow(b, e, m): Returns value b^e(mod m) given b, e and m
  3. 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
  4. gkern(kernlen=3, nsig=3): Returns a numpy matrix of 2D gaussian kernal of dimension(kernlen x kernlen) with sigma as nsig

3. image_utils.py:

  1. 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
  2. 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
  3. Im_encrypt(public, plain_im): Encrypts original image plain_im using public key and returns encrypted numpy array of same shape as image
  4. 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
  5. 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
  6. Negation(public, cipher_im): Adds negation filter pixelwise to ciphered image cipher_im using public key and returns new ciphered numpy array
  7. 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
  8. 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
  9. 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.
  10. 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.
  11. 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

4. main.py:

Sample driver code

5. experiments.py:

Sample code for experiments

Releases

No releases published

Packages

No packages published

Languages