-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.py
More file actions
127 lines (113 loc) · 3.57 KB
/
Copy pathencrypt.py
File metadata and controls
127 lines (113 loc) · 3.57 KB
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
from PIL import Image
from random import randint
import numpy as np
import os
from helper import *
def encrypt(src):
img = Image.open(src)
pix = img.load()
m = img.size[0]
n = img.size[1]
# obtain the RGB matrices
r = []
g = []
b = []
for i in range(m):
r.append([])
g.append([])
b.append([])
for j in range(n):
rgbPerPixel = pix[i, j]
r[i].append(rgbPerPixel[0])
g[i].append(rgbPerPixel[1])
b[i].append(rgbPerPixel[2])
# vectors Kr and Kc
alpha = 8
Kr = [randint(0, pow(2, alpha) - 1) for i in range(m)]
Kc = [randint(0, pow(2, alpha) - 1) for i in range(n)]
ITER_MAX = 1
f = open('keys.txt', 'w+')
f.write('Vector Kr : \n')
for a in Kr:
f.write(str(a) + '\n')
f.write('Vector Kc : \n')
for a in Kc:
f.write(str(a) + '\n')
f.write('ITER_MAX : \n')
f.write(str(ITER_MAX) + '\n')
# encrypt
for iterations in range(ITER_MAX):
# for each row
for i in range(m):
rTotalSum = sum(r[i])
gTotalSum = sum(g[i])
bTotalSum = sum(b[i])
rModulus = rTotalSum % 2
gModulus = gTotalSum % 2
bModulus = bTotalSum % 2
if rModulus == 0:
r[i] = np.roll(r[i], Kr[i])
else:
r[i] = np.roll(r[i], -Kr[i])
if gModulus == 0:
g[i] = np.roll(g[i], Kr[i])
else:
g[i] = np.roll(g[i], -Kr[i])
if bModulus == 0:
b[i] = np.roll(b[i], Kr[i])
else:
b[i] = np.roll(b[i], -Kr[i])
# for each column
for i in range(n):
rTotalSum = 0
gTotalSum = 0
bTotalSum = 0
for j in range(m):
rTotalSum += r[j][i]
gTotalSum += g[j][i]
bTotalSum += b[j][i]
rModulus = rTotalSum % 2
gModulus = gTotalSum % 2
bModulus = bTotalSum % 2
if rModulus == 0:
upshift(r, i, Kc[i])
else:
downshift(r, i, Kc[i])
if gModulus == 0:
upshift(g, i, Kc[i])
else:
downshift(g, i, Kc[i])
if bModulus == 0:
upshift(b, i, Kc[i])
else:
downshift(b, i, Kc[i])
# for each row
for i in range(m):
for j in range(n):
if i % 2 == 1:
r[i][j] = r[i][j] ^ Kc[j]
g[i][j] = g[i][j] ^ Kc[j]
b[i][j] = b[i][j] ^ Kc[j]
else:
r[i][j] = r[i][j] ^ rotate180(Kc[j])
g[i][j] = g[i][j] ^ rotate180(Kc[j])
b[i][j] = b[i][j] ^ rotate180(Kc[j])
# for each column
for j in range(n):
for i in range(m):
if j % 2 == 0:
r[i][j] = r[i][j] ^ Kr[i]
g[i][j] = g[i][j] ^ Kr[i]
b[i][j] = b[i][j] ^ Kr[i]
else:
r[i][j] = r[i][j] ^ rotate180(Kr[i])
g[i][j] = g[i][j] ^ rotate180(Kr[i])
b[i][j] = b[i][j] ^ rotate180(Kr[i])
for i in range(m):
for j in range(n):
pix[i, j] = (r[i][j], g[i][j], b[i][j])
#output
if not os.path.exists('encrypted_images'):
os.makedirs('encrypted_images')
img.save('encrypted_images/' + os.path.basename(src))
return 'encrypted_images/' + os.path.basename(src)