forked from PriyankaSahani/Decryption-using-GA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.py
More file actions
316 lines (247 loc) · 8.38 KB
/
Copy pathdecrypt.py
File metadata and controls
316 lines (247 loc) · 8.38 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from __future__ import print_function
import collections
import re
import random
import matplotlib.pyplot as plt
##############################################################################
# Program parameters
# Path to the text file containing the ciphertext
INFILE = 'ciphertext.txt'
# Path to the text file containing the reference text
REFFILE = 'paradiso.txt'
# Encrypted chars in the ciphertext
CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# Size of the population to use for the genetic algorithm
# POPULATION_SIZE = 50
POPULATION_SIZE = 20
# Size of the population slice of best peforming solutions to keep at each
# iteration
# TOP_POPULATION = 10
TOP_POPULATION = 10
# Number of intervals for which the best score has to be stable before aborting
# the genetic algorithm
# STABILITY_INTERVALS = 20
STABILITY_INTERVALS = 20
# Number of crossovers to execute for each new child in the genetic algorithm
# CROSSOVER_COUNT = 2
CROSSOVER_COUNT = 2
# Number of random mutation to introduce for each new child in the genetic
# algorithm
# MUTATIONS_COUNT = 1
MUTATIONS_COUNT = 1
##############################################################################
# Implementation
def pairwise(iterable):
prev = None
for item in iterable:
if prev is not None:
yield prev + item
prev = item
def bigram(text):
counter = collections.Counter()
words = re.sub('[^{}]'.format(CHARS), ' ', text).split()
for word in words:
for pair in pairwise(word):
counter[pair] += 1
return counter
# sentence = "hello world this is me"
# words = sentence.split(' ')
# words = ['hello', 'world,...]
# for word in words:
# pairs = [word[i,i+3] for i in range(0,len(word)-2,1)]
"""def triplet(word):
pairs = [word[i:i+3] for i in range(0,len(word)-2,1)]
return pairs"""
"""def trigram(text):
counter = collections.Counter()
words = re.sub('[^{}]'.format(CHARS), ' ', text).split()
for word in words:
for pair in triplet(word):
counter[pair] += 1
return counter"""
def decode(ciphertext, key):
cleartext = ''
for char in ciphertext:
cleartext += key.get(char, char)
return cleartext
def init_mapping():
# Generate a randomly initialized solution
repls = set(CHARS)
mapping = {}
for c in CHARS:
if c in mapping:
continue
repl = random.choice(list(repls))
repls.remove(repl)
repls.discard(c)
mapping[c] = repl
mapping[repl] = c
return mapping
def update_mapping(mapping, char, repl):
# Update the solution by switching `char` with `repl`
# and `repl` with `char`.
current_repl = mapping[char]
current_char = mapping[repl]
if current_char == repl:
current_char = current_repl
elif current_repl == char:
current_repl = current_char
mapping[current_char] = current_repl
mapping[current_repl] = current_char
mapping[char] = repl
mapping[repl] = char
##############################################################################
# Genetic algorithm routines
def select(population, ciphertext, ref_bigram):
scores = []
# Compute the score of each solution
for p in population:
scores.append((score(decode(ciphertext, p), ref_bigram), p))
# Sort the solutions by their score
sorted_population = sorted(scores, reverse=True)
# Select only the best TOP_POPULATION solutions
selected_population = sorted_population[:TOP_POPULATION]
return selected_population[0][0], [m for _, m in selected_population]
def generate(population):
new_population = population[:]
while len(new_population) < POPULATION_SIZE:
# Randomly select two parent solutions
x, y = random.choice(population), random.choice(population)
# Create the child solution
child = x.copy()
# Switch CROSSOVER_COUNT chromosomes between the parents
for i in range(CROSSOVER_COUNT):
char = random.choice(list(CHARS))
update_mapping(child, char, y[char])
# Randomly mutate MUTATIONS_COUNT chromosomes of the the child solution
for i in range(MUTATIONS_COUNT):
char = random.choice(list(CHARS))
repl = random.choice(list(CHARS))
update_mapping(child, char, repl)
# Add the newly obtained child the the current population
new_population.append(child)
return new_population
def score(text, ref_bigram):
text_bigram = bigram(text)
score = 0
# Multiply the number of occurrences of each pair in the decoded
# ciphertext with the number of occurrences of that same pair in the
# reference text, then take the sum of all multiplications.
for pair, occurrences in text_bigram.items():
score += occurrences * ref_bigram[pair]
return score
###############################################################################
# Decryption routine
BestScores = []
Iterations = []
def decrypt():
# Read the reference text into memory
with open(REFFILE) as fh:
reftext = fh.read().upper()
# Analyze the reference text and compute a mapping of each pair of letters
# to the number of occurrences in the reference text
#
# ref_bigram = {
# 'AA': 1,
# 'AB': 64,
# 'AC': 354,
# 'AD': 279,
# 'AE': 26,
# 'AF': 52,
# 'AG': 241,
# 'AH': 2,
# 'AI': 260,
# 'AL': 1141,
# 'AM': 353,
# ...
# 'VE': 958,
# 'VI': 727,
# 'VO': 409,
# 'VR': 43,
# 'VU': 33,
# 'VV': 28,
# 'ZA': 249,
# 'ZE': 35,
# 'ZI': 240,
# 'ZO': 49,
# 'ZZ': 103,
# }
#
ref_bigram = bigram(reftext)
# Read the ciphertext into memory
with open(INFILE) as fh:
ciphertext = fh.read().upper()
# Create an initial population of random possible solutions
population = [init_mapping() for i in range(POPULATION_SIZE)]
print(population[0])
# Set the initial values for the stability checker
last_score = 0
last_score_increase = 0
iterations = -STABILITY_INTERVALS
# Run the genetic algorithm
while last_score_increase < STABILITY_INTERVALS:
# Fill up the population up to POPULATION_SIZE solutions by crossing
# over and mutating the TOP_POPULATION best solutions
population = generate(population)
# Select the TOP_POPULATION best solutions from the current population
best_score, population = select(population, ciphertext, ref_bigram)
# Update the stability check state with the current best score
if best_score > last_score:
last_score_increase = 0
last_score = best_score
else:
last_score_increase += 1
print(iterations+STABILITY_INTERVALS,':',best_score)
BestScores.append(best_score)
Iterations.append(iterations+STABILITY_INTERVALS)
plt.plot(Iterations,BestScores,'r')
plt.xlabel('generation')
plt.ylabel('bestScore')
plt.show(block=False)
plt.pause(0.05)
# plt.close()
iterations += 1
# Print the current (best) solution
#
# best_solution = population[0] = {
# 'A': 'M',
# 'B': 'O',
# 'C': 'Q',
# 'D': 'R',
# 'E': 'P',
# 'F': 'T',
# 'G': 'G', # Wrong, should be 'Z'
# 'H': 'U',
# 'I': 'V',
# 'L': 'S',
# 'M': 'A',
# 'N': 'N',
# 'O': 'B',
# 'P': 'E',
# 'Q': 'C',
# 'R': 'D',
# 'S': 'L',
# 'T': 'F',
# 'U': 'H',
# 'V': 'I',
# 'Z': 'Z', # Wrong, should be 'G'
# }
#
print('Best solution found after {} iterations'.format(iterations))
print('with population :{}'.format(population[0]))
return decode(ciphertext, population[0])
def metrics():
AnswerText = decrypt()
totalLen = len(AnswerText)
accuracyCount = 0
with open('plaintext.txt') as original:
OriginaText = original.read().upper()
for i in range(totalLen):
if OriginaText[i]==AnswerText[i]:
accuracyCount+=1
print('accuracy : {}%'.format(100*accuracyCount/totalLen))
plt.plot(BestScores,Iterations)
metrics()
plt.show()
plt.pause(2)
plt.close()