paillier image encryption in python

845 views Asked by At

I have tried to encrypt an image using paillier. But I can't decrypt it. Please help me to findout.

from phe import paillier
from PIL import Image
import cv2
import PIL
import numpy
openfilename = "greyscale.png"
img2 = cv2.imread(openfilename,0)
public_key, private_key = paillier.generate_paillier_keypair()
encrypted_number_list = [[public_key.encrypt(int(x)) for x in row] for row 
in img2]
encrypted_number_array = numpy.array(encrypted_number_list)
print(encrypted_number_array)
decrypted_number_list =[private_key.decrypt(x) for x in 
encrypted_number_array]
decrypted_number_array = numpy.array(decrypted_number_list)
print(decrypted_number_array)

Here is the image I am using:

enter image description here

1

There are 1 answers

3
Alderven On BEST ANSWER

Image encryption/decryption with pailer:

import cv2
from phe import paillier
from scipy.misc import toimage

img = cv2.imread('image.png', 0)
public_key, private_key = paillier.generate_paillier_keypair()
data_encrypted = [[public_key.encrypt(int(x)) for x in row] for row in img]

data_decrypted = [[private_key.decrypt(x) for x in row] for row in data_encrypted]
toimage(data_decrypted).save('result.png')