-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathImageCryptography.py
93 lines (73 loc) · 2.72 KB
/
ImageCryptography.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from PIL import Image
import numpy as np
import pickle
import Paillier
def ImgEncrypt(public_key, plainimg):
"""
args:
public_key: Paillier PublicKey object
plainimg: PIL Image object
returns:
cipherimg: Encryption of plainimg
Encrypts an image
"""
cipherimg = np.asarray(plainimg)
shape = cipherimg.shape
cipherimg = cipherimg.flatten().tolist()
cipherimg = [Paillier.Encrypt(public_key, pix) for pix in cipherimg]
return np.asarray(cipherimg).reshape(shape)
def ImgDecrypt(public_key, private_key, cipherimg):
"""
args:
public_key: Paillier PublicKey object
private_key: Paillier PrivateKey object
cipherimg: encryption of Image
returns:
Image object which is the decryption of cipherimage
Decrypts ecnrypted image
"""
shape = cipherimg.shape
plainimg = cipherimg.flatten().tolist()
plainimg = [Paillier.Decrypt(public_key, private_key, pix) for pix in plainimg]
plainimg = [pix if pix < 255 else 255 for pix in plainimg]
plainimg = [pix if pix > 0 else 0 for pix in plainimg]
return Image.fromarray(np.asarray(plainimg).reshape(shape).astype(np.uint8))
def homomorphicBrightness(public_key, cipherimg, factor):
"""
args:
public_key: Paillier PublicKey object
cipherimg: n dimensional array containing encryption of image pixels
factor: Amount of brightness to be added (-ve for decreasing brightness)
returns:
n dimensional array containing encryption of image pixels with brightness adjusted
Function to demonstrate homomorphism
Performs brightness adjust operation on the encrypted image
"""
shape = cipherimg.shape
brightimg = cipherimg.flatten().tolist()
brightimg = [Paillier.homomorphic_add_constant(public_key, pix, factor) for pix in brightimg]
return np.asarray(brightimg).reshape(shape)
def saveEncryptedImg(cipherimg, filename):
"""
args:
cipherimg: Encryption of an image
filename: filename to save encryption (saved under encrypted-images directory)
saves Encryption of image int a file
"""
filename = "encrypted-images/" + filename
fstream = open(filename, "wb")
pickle.dump(cipherimg, fstream)
fstream.close()
def loadEncryptedImg(filename):
"""
args:
filename: filename of the Encrypted object under encrypted-images directory
returns:
n-dimensional array containing ecryption of image
loads Encrypted image object from file
"""
filename = "encrypted-images/" + filename
fstream = open(filename, "rb")
cipherimg = pickle.load(fstream)
fstream.close()
return cipherimg