python中的有效图像加密

时间:2019-02-25 05:35:06

标签: python image encryption paillier

我尝试使用paillier加密图像。但是我无法解密。请帮助我找出答案。

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)

这是我正在使用的图像:

enter image description here

1 个答案:

答案 0 :(得分:1)

使用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')