1

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

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I got the following errors when I run it. TypeError: Expected encrypted_number to be an EncryptedNumber not: – JAMEENA V A Feb 25 '19 at 05:35
  • Can you share `greyscale.png`? – Alderven Feb 25 '19 at 07:37
  • I have shared the greyscale image – JAMEENA V A Feb 25 '19 at 10:05
  • I don't know `paillier` but the line starting `encrypted_number_list = ...` looks wrong to me. I don't believe the image you read in using OpenCV has the concept of `rows` that you are trying to iterate over. I think I would just do a simple `read()` of the entire PNG file rather than `imread()` and pass that data to the encryptor. Then you can do an `imdecode()` after decryption to get a displayable image. – Mark Setchell Feb 25 '19 at 10:19
  • @MarkSetchell OpenCV image can be represented as a list of lists. So you can iterate over it. – Alderven Feb 25 '19 at 13:44
  • @Alderven I was not aware of that, thank you. – Mark Setchell Feb 25 '19 at 14:35

1 Answers1

2

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')
Alderven
  • 7,569
  • 5
  • 26
  • 38