-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayfair-Crack.py
138 lines (121 loc) · 5.4 KB
/
Playfair-Crack.py
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
from collections import Counter
import random
import json
import copy
import math
cipher = "DGYHMVELRQHCMOIHNPPAMVMQHNMRDFREQMDOKZGKAXNOVUZPZNECRLLHYXMELVUQQDNOPVMZPMXYGXVPNOHZFHUDFHGDDHSTZRQRHDUKGHDQCQRQDGRMNIZESLDQMVKHLVPCONDOKZMELVDFOCWRMRDGRMGBFNMBRMPLXZPCPRCIVMKPARXEMVBADQCHDSRWRMYHEMXMQXUAUBQNMQQINORMLRDQWCPACHPNEANVVLQWDGDNMOGYAOAZVNAEUAHDBFPWAMBHMVONQURQAOFDWXWVHAONHYPNCDXYGBFNMBRM"
possibleKeyChars = ['Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','K','L','Z','X','C','V','B','N','M']
maxScore = -100000
maxMatrix = []
with open('data.json', 'r') as json_file:
loaded_data = json.load(json_file)
def generate_playfair_matrix(key):
matrix = []
for char in key:
if char not in matrix:
matrix.append(char)
return [matrix[i:i+5] for i in range(0, len(matrix), 5)]
def find_position(matrix, char):
for i in range(5):
for j in range(5):
if matrix[i][j] == char:
return i, j
def decrypt_playfair_and_display(matrix, iterCount, score):
plaintext = ""
pairs = [cipher[i:i+2] for i in range(0, len(cipher), 2)]
for pair in pairs:
row1, col1 = find_position(matrix, pair[0])
row2, col2 = find_position(matrix, pair[1])
if row1 == row2:
plaintext += matrix[row1][(col1-1)%5] + matrix[row2][(col2-1)%5]
elif col1 == col2:
plaintext += matrix[(row1-1)%5][col1] + matrix[(row2-1)%5][col2]
else:
plaintext += matrix[row1][col2] + matrix[row2][col1]
print("After iteration " + str(iterCount))
print (matrix)
print (plaintext)
print (score)
def decrypt_playfair_and_score(matrix):
# decrypting
plaintext = ""
pairs = [cipher[i:i+2] for i in range(0, len(cipher), 2)]
for pair in pairs:
row1, col1 = find_position(matrix, pair[0])
row2, col2 = find_position(matrix, pair[1])
if row1 == row2:
plaintext += matrix[row1][(col1-1)%5] + matrix[row2][(col2-1)%5]
elif col1 == col2:
plaintext += matrix[(row1-1)%5][col1] + matrix[(row2-1)%5][col2]
else:
plaintext += matrix[row1][col2] + matrix[row2][col1]
# Scoring
substrings = [plaintext[i:i+4] for i in range(len(plaintext)-3)]
frequency_dict = dict(Counter(substrings))
score = 0
for i in frequency_dict:
if (i in loaded_data):
score = score + frequency_dict[i]*loaded_data[i]
else:
score = score + frequency_dict[i]*loaded_data["FLOOR"]
return score
while(True):
parent = random.sample(possibleKeyChars, 25)
parentMatrix = generate_playfair_matrix(parent)
parentScore = decrypt_playfair_and_score(parentMatrix)
Temperature = 28
iteration = 0
while(Temperature > 0):
counter = 0
while(counter < 10000):
random_number =random.randint(1, 50)
childMatrix = copy.deepcopy(parentMatrix)
if (random_number == 1):
# flip matrix across NW-SE axis
childMatrix = [list(reversed(row)) for row in reversed(childMatrix)]
elif (random_number == 2):
# flip matrix top to bottom
childMatrix = list(reversed(childMatrix))
elif (random_number == 3):
# flip matrix left to right
childMatrix = [list(reversed(row)) for row in childMatrix]
elif (random_number == 4):
# swap two rows chosen at random
row1 = random.randint(0, 4)
row2 = random.randint(0, 4)
childMatrix = copy.deepcopy(childMatrix)
childMatrix[row1], childMatrix[row2] = childMatrix[row2], childMatrix[row1]
elif (random_number == 5):
# swap two columns chosen at random
col1 = random.randint(0, 4)
col2 = random.randint(0, 4)
for i in range(len(childMatrix)):
childMatrix[i][col1], childMatrix[i][col2] = childMatrix[i][col2], childMatrix[i][col1]
else:
# swap any two values chosen at random
row1 = random.randint(0, 4)
col1 = random.randint(0, 4)
row2 = random.randint(0, 4)
col2 = random.randint(0, 4)
childMatrix[row1][col1], childMatrix[row2][col2] = childMatrix[row2][col2], childMatrix[row1][col1]
childScore = decrypt_playfair_and_score(childMatrix)
diff = (parentScore - childScore)
if (diff < 0):
parentMatrix = childMatrix
parentScore = childScore
else:
probability = 1 / math.exp(diff/Temperature)
randNum = random.random()
if (probability > randNum):
parentMatrix = childMatrix
parentScore = childScore
else:
if (counter % 1000 == 0):
print(counter, Temperature)
counter = counter + 1
if (childScore > maxScore):
maxScore = childScore
maxMatrix = childMatrix
iteration = iteration + 1
decrypt_playfair_and_display(maxMatrix, iteration, maxScore)
Temperature = Temperature - 0.5