-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageEncryption.py
306 lines (227 loc) · 8.47 KB
/
ImageEncryption.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# -*- coding: utf-8 -*-
from ECC import Curve
from ECC import EccMath
from RSA.RsaMath import OS2IP, I2OSP
from PIL import Image
from ECC import EncodeECC
import KeyGenerate
import sys
import math
import random
import os
import time
import seccure
n = Curve.getCurve()[1]
G = Curve.getCurve()[-1]
def rsa_cipher(key, mess):
n, eORd = key
return pow(mess, eORd, n)
def rsaEncrypt(publickey, data):
"Mã hoá bằng thuật toán RSA với đầu vào là chuỗi bytes đọc từ file ảnh"
cipher_rsa = []
# Đọc 127 bytes một để mã hoá
for i in range(0, len(data), 127):
value = data[i:i+127]
m = OS2IP(value)
c = rsa_cipher(publickey, m)
cipher_rsa.append(I2OSP(c, 128))
# Trả về chuỗi bytes sau khi mã hoá
return b''.join(cipher_rsa)
def rsaDecrypt(privatekey, data):
"Giải mã bằng thuật toán RSA với đầu vào là chuỗi bytes được giải mã từ ECC"
plain_rsa = []
# Đọc 128 bytes một để giải mã
for i in range(0, len(data), 128):
value = data[i:i+128]
c = OS2IP(value)
m = rsa_cipher(privatekey, c)
plain_rsa.append(I2OSP(m, 127))
# Trả về chuỗi bytes sau khi giải mã
return b''.join(plain_rsa)
def elgamalEn(m, P):
"Mã hoá Elgamal tính giá trị C2"
M = EncodeECC.encode(m)
C2 = EccMath.add_Points(M, P)
return C2
def elgamalDe(C, C2):
"Giải mã Elgamal trả về bản rõ m"
M = EccMath.add_Points(C2, C)
m = EncodeECC.decode(M)
return m
def doEccEncrypt(rsa_value, publicKey):
return seccure.encrypt(rsa_value, publicKey)
def eccEncrypt(rsa_value, publicKey):
"Mã hoá ECC với đầu vào là chuỗi bytes đã được mã hoá bằng RSA"
k = random.randint(1, n)
C1 = EccMath.scalar_Point(k, G)
P = EccMath.scalar_Point(k, publicKey)
# start_time = time.time()
cipher_points = []
for i in range(0, len(rsa_value), 31):
plain = rsa_value[i:i+31]
plain = OS2IP(plain)
ecc_enc = elgamalEn(plain, P)
cipher_points.append(ecc_enc)
# print "Nhung: %s seconds" %(time.time() - start_time)
# start_time = time.time()
cipher_bytes = []
for point in cipher_points:
point_to_bytes = I2OSP(point[0], 32) + I2OSP(point[1], 32)
cipher_bytes.append(point_to_bytes)
cipher_bytes = b''.join(cipher_bytes)
c = I2OSP(C1[0], 32) + I2OSP(C1[1], 32)
return c + cipher_bytes
def doEccDecrypt(data_cipher, privatekey):
return seccure.decrypt(data_cipher, privatekey)
def eccDecrypt(data_cipher, privateKey):
"Giải mã ECC với đầu vào là chuỗi bytes từ hình ảnh đã mã hoá"
c = data_cipher[:64]
C1 = (OS2IP(c[:32]), OS2IP(c[-32:]))
C = EccMath.scalar_Point(privateKey, C1)
C = EccMath.negatives_Point(C)
data_cipher = data_cipher[64:]
points = []
for i in range(0, len(data_cipher), 64):
data = data_cipher[i:i+64]
x = OS2IP(data[:32])
y = OS2IP(data[-32:])
points.append((x, y))
plain_data = []
for point in points:
plain_data.append(elgamalDe(C, point))
plain_bytes = []
for j in plain_data:
plain_bytes.append(I2OSP(j, 31))
plain_data = b''.join(plain_bytes)
plain_data = remove_padding(plain_data)
return plain_data
def padding(data, t):
"Thêm bytes \x00 hoặc \x01 vào đầu chuỗi bytes"
if data[0] != "\x00":
while len(data) % t != 0:
data = "\x00" + data
else:
while len(data) % t != 0:
data = "\x01" + data
return data
def remove_padding(data):
if data[0] == "\x00":
return data.lstrip("\x00")
else:
return data.lstrip("\x01")
def imageEncrypt(data, rsa_publickey, ecc_publickey):
"Mã hoá hình ảnh bằng RSA và ECC"
data = padding(data, 127)
# Mã hoá bằng RSA
start_time = time.time()
rsa_encrypted = rsaEncrypt(rsa_publickey, data)
print "rsaEncrypt %s seconds" % (time.time() - start_time)
rsa_encrypted = padding(rsa_encrypted,31)
#ecc_encrypted = eccEncrypt(rsa_encrypted, ecc_publickey)
start_time = time.time()
ecc_encrypted = doEccEncrypt(rsa_encrypted, ecc_publickey)
print "doEccEncrypt %s seconds" % (time.time() - start_time)
return ecc_encrypted
def imageDecrypt(data, rsa_privatekey, ecc_privatekey):
"Giải mã hình ảnh đã được mã hoá bằng RSA và ECC"
#ecc_decrypted = eccDecrypt(data, ecc_privatekey)
start_time = time.time()
ecc_decrypted = doEccDecrypt(data, ecc_privatekey)
print "doEccDecrypt %s seconds" % (time.time() - start_time)
ecc_decrypted = remove_padding(ecc_decrypted)
start_time = time.time()
rsa_decrypted = rsaDecrypt(rsa_privatekey, ecc_decrypted)
print "imageDecrypt %s seconds" % (time.time() - start_time)
rsa_decrypted = remove_padding(rsa_decrypted)
return rsa_decrypted
def creatImage(data, name):
num_pixel = (len(data) + 2) / 3
W = H = int(math.ceil(num_pixel ** 0.5))
if data[0] == "\x00":
data = "\x01" * (W * H * 3 - len(data)) + data
else:
data = "\x00" * (W * H * 3 - len(data)) + data
img = Image.frombytes("RGB", (W, H), data)
img.save("%s_encrypted.png"% name)
print "%s_encrypted.png"% name
print "Mã hoá thành công!"
img.show()
def read_file_to_Encrypt(file_input, file_rsaKey, file_eccKey):
"Đọc file hình ảnh, file chứa khoá công khai rsa và ecc"
with open(file_input, "rb") as infile:
data = infile.read()
# im = Image.open(file_input)
# data = im.tobytes()
with open(file_rsaKey, "r") as rsaKey:
rsa_publickey = rsaKey.read()
n, e = rsa_publickey.split(",")
rsa_publickey = (int(n), int(e))
with open(file_eccKey, "r") as eccKey:
ecc_publickey = eccKey.read()
#x, y = ecc_publickey.split(",")
#ecc_publickey = (int(x), int(y))
return data, rsa_publickey, ecc_publickey
def recoverImage(data, name):
with open("%s_decrypted.png" % name, "wb") as outfile:
outfile.write(data)
im = Image.open("%s_decrypted.png" % name)
im.show()
print "Giải mã thành công!"
def read_file_to_Decrypt(file_input, file_rsaKey, file_eccKey):
"Đọc file hình ảnh, file chứa khoá bí mật rsa và ecc"
im = Image.open(file_input)
data = im.tobytes()
data = remove_padding(data)
with open(file_rsaKey, "r") as rsaKey:
rsa_privatekey = rsaKey.read()
n, d = rsa_privatekey.split(",")
rsa_privatekey = (int(n), (int(d)))
with open(file_eccKey, "r") as eccKey:
ecc_privatekey = eccKey.read()
#ecc_privatekey = int(ecc_privatekey)
return data, rsa_privatekey, ecc_privatekey
def Encryption():
path = str(os.getcwd()) + "/Key/"
image = str(raw_input("Nhập tên ảnh cần mã hoá: "))
name = str(raw_input("Nhập tên user: "))
rsa_filekey = path + "%s_RSA_publickey.txt" % name
ecc_filekey = path + "%s_ECC_publickey.txt" % name
data, rsa_publickey, ecc_publickey = read_file_to_Encrypt(image, rsa_filekey, ecc_filekey)
start_time = time.time()
encrypted = imageEncrypt(data, rsa_publickey, ecc_publickey)
print "%s seconds" % (time.time() - start_time)
return creatImage(encrypted, name)
def Decryption():
path = str(os.getcwd()) + "/Key/"
image = str(raw_input("Nhập tên ảnh cần giải mã: "))
name = str(raw_input("Nhập tên user: "))
rsa_filekey = path + "%s_RSA_privatekey.txt" % name
ecc_filekey = path + "%s_ECC_privatekey.txt" % name
data, rsa_privatekey, ecc_privatekey = read_file_to_Decrypt(image, rsa_filekey, ecc_filekey)
start_time = time.time()
decrypted = imageDecrypt(data, rsa_privatekey, ecc_privatekey)
print "%s seconds" %(time.time() - start_time)
return recoverImage(decrypted, name)
if __name__ == '__main__':
def choise(number):
if number == "1":
return KeyGenerate.generateKey()
if number == "2":
return Encryption()
if number == "3":
return Decryption()
if number == "4":
sys.exit()
else:
print "Bạn nhập sai! Nhập lại."
def Menu():
print '''Mời bạn chọn chức năng!
1: Tạo khoá
2: Mã hoá
3: Giải mã
4: Thoát'''
x = str(raw_input("Bạn chọn: "))
choise(x)
print "Mã hoá hình ảnh bằng RSA & ECC"
while True:
Menu()