-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencryption.py
More file actions
141 lines (100 loc) · 3.21 KB
/
encryption.py
File metadata and controls
141 lines (100 loc) · 3.21 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from __future__ import division # change the / operator to mean true division throughout the module. always floating point result
from PIL import Image
from random import randint
from collections import deque
import smtplib
def rubics_operation(image, k):
j = 0
while j < len(image):
sum_pix = sum(image[j])
x = deque(image[j]) #adding and removing elements from either end
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 send_email(email, kr, kc, itr):
email_adderss = 'abc@gmail.com'
email_password = 'password'
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(email_adderss, email_password)
subject = "Image Encryption"
body = 'Image encrypted successfully! kr = ', kr, ' kc = ', kc, 'Number of Iterations = ', itr
msg = f'Subject: {subject}\n\n{body}'
smtp.sendmail(email_adderss, email, msg)
def inputImg(file_name, itr, email):
#file_name = input("enter the file name to be encrypt:\t")
im = Image.open(file_name) #image details
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] #length or size of list
x = grey_image1[sp:fp]
sp = fp
grey_image2.append(x)
#itr = int(input("enter the no of iterations :\t"))
kr = randint(1, 255)
kc = randint(1, 255)
print("random integers are " + str(kr) + " , " + str(kc))
#email = input("Enter email: ")
send_email(email, kr, kc, itr)
i = 0
while i < itr:
i += 1
grey_image2 = rubics_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 = xor_operation(grey_image2 , kr)
grey_image2 = row_col_inversion(grey_image2)
grey_image2 = xor_operation(grey_image2 , kc)
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))
im2.putdata(grey_image4)
im2.save("enc_result/" + file_name)
print("encryption done")