-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtesting.py
More file actions
105 lines (102 loc) · 3.83 KB
/
testing.py
File metadata and controls
105 lines (102 loc) · 3.83 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
# This module is mainly for the decryption purpose
# Here we import the required modules
from PIL import Image
from numpy import array
from cryptography.fernet import Fernet
from Utils import Utils
from Population import Population
from Chromosome import Chromosome
from Fitness import Fitness
from GA import GA
from Pit import Pit
# Here we build the pitTable from the generated pit1.csv file
file=open('pit1.csv','r')
a=file.readlines()
cipherLen=len(a)
pitTable=[]
for i in range(cipherLen):
pitTable.append(Pit())
xValue=0
yValue=0
diff=[None]*3
index=0
for i in a:
t=i.split(',')
xValue=int(t[0])
yValue=int(t[1])
diff[0]=int(t[2])
diff[1]=int(t[3])
string=t[4]
string=string[:len(string)-1]
diff[2]=int(string)
pitTable[index]=Pit(xValue,yValue,diff)
index+=1
print('Pit table is complete')
#print('Length of the pit table is:',len(pitTable))
#print('The values in the pitTable is:')
#for i in range(cipherLen):
# print("x:",pitTable[i].getX()," y:",pitTable[i].getY(),"deltas:",pitTable[i].getDeltas())
# Now we have generated the pitTable
# Now we need to read the image for decoding the hidden message
# Here we start the decoding part of the code
print("Reading the image for decoding")
#This part of the code is used httpto extract the pixel values of the image
img=Image.open('final.png')
#This returns the matrix of the pixels of the image
arr1=array(img)
height=len(arr1)
width=len(arr1[0])
#Now we are done with the extraction of image pixels from the given image
print('Successfully extracted the pixels of the image')
dcipherBytes=[None]*len(pitTable)
dcipherIndex=0
count=0
poi=0
toi=0
print('Extracting the details from the pixel index table and extracting the cipher text')
for i in range(len(pitTable)):
for x in range(width):
for y in range(height):
if(pitTable[i].getX()==x and pitTable[i].getY()==y):
count+=1
decodedIndex=pitTable[i].getDecodedValueIndex()
decodedValue=0
pitValue=0
# If the data is hidden inside the red pixel
if(decodedIndex==0):
pitValue=pitTable[i].getDecodeValue()
if(pitValue>=0):
decodedValue=arr1[x][y][0] + pitTable[i].getDecodeValue()
else:
decodedValue=pitTable[i].getDecodeValue() + arr1[x][y][0]
# If the data is hidden inside the green pixel
elif(decodedIndex==1):
pitValue=pitTable[i].getDecodeValue()
if(pitValue>=0):
decodedValue=arr1[x][y][1] + pitTable[i].getDecodeValue()
else:
decodedValue = pitTable[i].getDecodeValue() + arr1[x][y][1]
# If the data is hidden inside the blue pixel
elif(decodedIndex==2):
pitValue = pitTable[i].getDecodeValue()
if(pitValue>=0):
decodedValue = arr1[x][y][2] + pitTable[i].getDecodeValue()
else:
decodedValue = pitTable[i].getDecodeValue() + arr1[x][y][2]
dcipherBytes[dcipherIndex] = decodedValue
dcipherIndex+=1
print('Successfully extracted the cipher text.')
asdf=""
for i in range(len(dcipherBytes)-1):
asdf+=chr(dcipherBytes[i])
print('The encrypted string is:',asdf)
#Now we need to decrypt the message to get the original message
key=input("Enter the key to decrypt the extracted message")
key=key.encode('utf-8')
print("The entered key is:",key)
cipher_suite = Fernet(key)
dcipherBytes=asdf.encode('utf-8')
plain_text = cipher_suite.decrypt(dcipherBytes)
plain_text=plain_text.decode('utf-8')
print("The message hidden in the image is:",plain_text)
print('We have successfully completed the extraction phase.')