-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisual_cryptography.py
More file actions
142 lines (132 loc) · 4.62 KB
/
Copy pathvisual_cryptography.py
File metadata and controls
142 lines (132 loc) · 4.62 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
142
# This module is designed to extract the rgb channels of the image separately and save them into three separate files(using encryption)
from PIL import Image
from numpy import array
from cryptography.fernet import Fernet
class visual_cryptography:
def __init__(self,imageName):
self.imageName=imageName
def encrypt(self):
# We need to open the image and extract the pixel values from it
# This part of the code is used to extract the pixel values of the image
img=Image.open(self.imageName)
#This returns the matrix of the pixels of the image
arr=array(img)
#print("The pixels of the image are:")
#print(arr)
row=len(arr)
column=len(arr[0])
#Now we are done with the extraction of image pixels from the given image
#These three variables are used to store the corresponding channel pixel values
red=""
green=""
blue=""
# Now we need to parse the extracted matrix and extract pixel values of each channels
count=0
i=0
j=0
k=0
while count<(row*column):
#print("Progressing")
#print(count)
if i==row-1 and j==column-1:
red=red+str(arr[i][j][0])
green=green+str(arr[i][j][1])
blue=blue+str(arr[i][j][2])
else:
red=red+str(arr[i][j][0])+','
green=green+str(arr[i][j][1])+','
blue=blue+str(arr[i][j][2])+','
j=j+1
if j>=column:
j=0
i=i+1
count=count+1
print("We have successfully extracted the rgb channels of the given image")
#print("The red channel has:")
#print(red)
red=red.encode('utf-8')
#print()
#print("The green channel has:")
#print(green)
green=green.encode('utf-8')
#print()
#print("The blue channel has:")
#print(blue)
blue=blue.encode('utf-8')
#This is the end of extraction phase of the pixels of the corresponding channels
# Now we need to encrypt these pixel values
key = Fernet.generate_key()
cipher_suite=Fernet(key)
#Encrypting the red pixels
cipher_text_red = cipher_suite.encrypt(red)
test_red=cipher_suite.decrypt(cipher_text_red)
print("The red pixel have been encrypted")
#print("The encrypted text is:")
#print(cipher_text_red)
#print("The plain text is:")
#print(test_red)
#Encrypting the green pixels
cipher_text_green = cipher_suite.encrypt(green)
test_green=cipher_suite.decrypt(cipher_text_green)
print("The green pixel have been encrypted")
#print("The encrypted text is:")
#print(cipher_text_green)
#print("The plain text is:")
#print(test_green)
#Encrypting the blue pixels
cipher_text_blue = cipher_suite.encrypt(blue)
test_blue=cipher_suite.decrypt(cipher_text_blue)
print("The blue pixel have been encrypted")
#print("The encrypted text is:")
#print(cipher_text_blue)
#print("The plain text is:")
#print(test_blue)
#print()
print("Important,")
print(key)
print("The key for the encryption is:",key.decode('utf-8'))
print("Please note this key for decryption")
#This brings us to the end of the encryption phase
# Now the main task is to put these encrypted files into three different files
# Now creating the file to store the red pixel values
file_red=open('red_pixel','w')
file_red.write(cipher_text_red.decode('utf-8'))
file_red.write("\n")
asd=str(row)+","+str(column)
asd=cipher_suite.encrypt(asd.encode('utf-8'))
asd=asd.decode('utf-8')
file_red.write(asd)
file_red.close()
print("A file named red_pixel hsa been created to store the red pixels")
# Now creating a fiel to store the blue pixel values
file_green=open('green_pixel','w')
file_green.write(cipher_text_green.decode('utf-8'))
file_green.write('\n')
file_green.write(asd)
file_green.close()
print("A file names green_pixel has been created to store the green pixels")
#Now creating a file to store the green pixel values
file_blue=open('blue_pixel','w')
file_blue.write(cipher_text_blue.decode('utf-8'))
file_blue.write('\n')
file_blue.write(asd)
file_blue.close()
print("A file named blue_pixel has been created to store the blue pixels")
#Now we have come to the end of the storing the pixel values into the files
#print("The length of the red pixel is:",len(red.decode('utf-8')))
#print("The length of the green pixel is:",len(green.decode('utf-8')))
#print("The length of the blue pixel is:",len(blue.decode('utf-8')))
#TEsting phase of the project
file_red=open('red_pixel','r')
qwerty1=file_red.read()
#print("The lenght of red is:",len(qwerty1))
file_red.close()
file_green=open('green_pixel','r')
qwerty2=file_green.read()
#print("The lenght of green is:",len(qwerty2))
file_green.close()
file_blue=open('blue_pixel','r')
qwerty3=file_blue.read()
#print("The lenght of blue is:",len(qwerty3))
file_blue.close()
return(key.decode('utf-8'))