-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecryption.py
More file actions
107 lines (85 loc) · 2.37 KB
/
decryption.py
File metadata and controls
107 lines (85 loc) · 2.37 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
from __future__ import division
from PIL import Image
from random import randint
from collections import deque
def rubics_operation(image, k):
j = 0
while j < len(image):
sum_pix = sum(image[j])
x = deque(image[j])
if sum_pix % 2 == 0:
x.rotate(-k)
else:
x.rotate(k)
image[j] = list(x)
j +=1
return image
def row_col_inversion(image):
other = list()
temp = list()
i = 0
while i < len(image[0]):
temp = []
for row in image:
temp.append(row[i])
other.append(temp)
i += 1
return other
def xor_operation(image, k):
i = 1
for row in image:
if i % 2 == 0:
j = 0
temp_k = 255 - k
for pix in row:
row[j] = temp_k ^ pix
j += 1
else:
j = 0
for pix in row:
row[j] = k ^ pix
j += 1
i += 1
return image
def imgDecrypt(file_name, iterations, kr, kc):
#file_name = input("enter the file name to be decrypt:\t")
im = Image.open("enc_result/"+file_name)
pixels = list(im.getdata())
grey_image1 = list()
for pix in pixels:
grey_image1.append(int(pix[0]))
grey_image2 = list()
sp = 0
for rows in range(im.size[1]):
fp = sp + im.size[0]
x = grey_image1[sp:fp]
sp = fp
grey_image2.append(x)
#itr = int(input("enter the no of iterations :\t"))
itr = iterations
i = 0
#kr = int(input("kr : "))
#kc = int(input("kc : "))
while i < itr:
i += 1
grey_image2 = xor_operation(grey_image2, kc)
grey_image2 = row_col_inversion(grey_image2)
grey_image2 = xor_operation(grey_image2, kr)
grey_image2 = row_col_inversion(grey_image2)
grey_image2 = rubics_operation(grey_image2, kc)
grey_image2 = row_col_inversion(grey_image2)
grey_image2 = rubics_operation(grey_image2, kr)
grey_image3 = list()
for row in grey_image2:
for pix in row:
grey_image3.append(pix)
grey_image4 = list()
for pix in grey_image3:
x = (pix, pix, pix)
grey_image4.append(x)
# generating the output
x, y = im.size
im2 = Image.new("RGB", (x, y))
im.putdata(grey_image4)
im.save("dec_result/" + file_name)
print("decryption done")