-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHacking.py
More file actions
181 lines (140 loc) · 7.72 KB
/
Hacking.py
File metadata and controls
181 lines (140 loc) · 7.72 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
import Decryption
from collections import Counter
import Global_variables
import string
# Attempts every possible key to decode the
# cipher text and writes all guessed outputs into a file for analysis
def brute_force_method(cipher_text):
guess_message = ''
with open("Brute_Force_Output.txt", "w", encoding= "utf-8") as f:
pass
for guess_key in range(1, Global_variables.ALL_SIZE-1):
guess_message = Decryption.decode_message(guess_key,cipher_text)
with open("Brute_Force_Output.txt", "a", encoding= "utf-8") as f:
f.write("="*100+"\n")
f.write("For guess key = "+ str(guess_key)+"\n")
f.write(guess_message)
f.write("="*100+"\n")
print("Please open the file Brute_Force_Output.txt file generated"+"\n")
# Performs frequency-based cryptanalysis by analyzing the most common unigrams, bigrams,
# and trigrams in the cipher text and replacing them with the most frequent patterns
# of the selected language
def math_analysis_method(cipher_text):
if Global_variables.LANGUAGE == 1:
# for unigram frequency analysis
with open("Math_Analysis_Method_Output.txt", "w", encoding= "utf-8") as f:
pass
excludes = " "+"\t"+"\n"+string.punctuation+string.digits
counts = Counter(cipher_text)
character_counts = [ch for ch,_ in counts.most_common() if ch not in excludes]
top_unigrams = ("etaoinsrhldcumfpgwyb")
if len(character_counts) > len(top_unigrams):
for each_top,x in zip(top_unigrams,range(0,len(top_unigrams))):
cipher_text = cipher_text.replace(character_counts[x],each_top)
else:
for each_top,x in zip(top_unigrams,range(0,len(character_counts))):
cipher_text = cipher_text.replace(character_counts[x],each_top)
with open("Math_Analysis_Method_Output.txt", "a", encoding= "utf-8") as f:
f.write("\n"+"="*100+"\n")
f.write("Unigram Analysis")
f.write("\n"+"="*100+"\n")
f.write(cipher_text)
f.write("\n"+"="*100+"\n")
# for bigram frequency analysis
bigrams = [cipher_text[ch]+cipher_text[ch+1] for ch in range(0,len(cipher_text)-2)
if cipher_text[ch] not in (" ","\t","\n") and
cipher_text[ch+1] not in (" ","\t","\n")]
bigram_counts = Counter(bigrams)
top_bigrams = ["th","he","in","er","an","re","es","on","st","nt","en","at","ed","nd","to","or","ea","ti","ar","te"]
if len(bigrams) > len(top_bigrams):
for each_topbigram,x in zip(top_bigrams,range(0,len(top_bigrams))):
cipher_text = cipher_text.replace(bigram_counts.most_common()[x][0],each_topbigram)
else:
for each_topbigram,x in zip(top_bigrams,range(0,len(bigram_counts.most_common()))):
cipher_text = cipher_text.replace(bigram_counts.most_common()[x][0],each_topbigram)
with open("Math_Analysis_Method_Output.txt", "a", encoding= "utf-8") as f:
f.write("Bigram Analysis")
f.write("\n"+"="*100+"\n")
f.write(cipher_text)
f.write("\n"+"="*100+"\n")
# for trigram frequency analysis
trigrams = [cipher_text[ch]+cipher_text[ch+1]+cipher_text[ch+2] for ch in range(0,len(cipher_text)-3)
if cipher_text[ch] not in (" ","\t","\n") and
cipher_text[ch+1] not in (" ","\t","\n") and
cipher_text[ch+2] not in (" ","\t","\n")]
trigram_counts = Counter(trigrams)
top_trigrams = ["the","and","tha","ent","ion","tio","for","nde","has","nce","tis","oft","men"]
if len(trigrams) > len(top_trigrams):
for each_toptrigram,x in zip(top_trigrams,range(0,len(top_trigrams))):
cipher_text = cipher_text.replace(trigram_counts.most_common()[x][0],each_toptrigram)
else:
for each_toptrigram,x in zip(top_trigrams,range(0,len(trigram_counts.most_common()))):
cipher_text = cipher_text.replace(trigram_counts.most_common()[x][0],each_toptrigram)
with open("Math_Analysis_Method_Output.txt", "a", encoding= "utf-8") as f:
f.write("Trigram Analysis")
f.write("\n"+"="*100+"\n")
f.write(cipher_text)
f.write("\n"+"="*100+"\n")
#FOR GERMAN LANGUAGE
elif Global_variables.LANGUAGE == 2:
with open("Math_Analysis_Method_Output.txt", "w", encoding= "utf-8") as f:
pass
excludes = " "+"\t"+"\n"+string.punctuation+string.digits
# for unigram frequency analysis
counts = Counter(cipher_text)
character_counts = [ch for ch,_ in counts.most_common() if ch not in excludes]
top_unigrams = ("enisratdhulcgmobwfkz")
if len(character_counts) > len(top_unigrams):
for each_top,x in zip(top_unigrams,range(0, len(top_unigrams))):
cipher_text = cipher_text.replace(character_counts[x],each_top)
else:
for each_top,x in zip(top_unigrams,range(0,len(character_counts))):
cipher_text = cipher_text.replace(character_counts[x],each_top)
with open("Math_Analysis_Method_Output.txt", "a", encoding= "utf-8") as f:
f.write("\n"+"="*100+"\n")
f.write("Unigram Analysis - German")
f.write("\n"+"="*100+"\n")
f.write(cipher_text)
f.write("\n"+"="*100+"\n")
# for bigram frequency analysis
counts = Counter(cipher_text)
character_counts = [ch for ch,_ in counts.most_common() if ch not in excludes]
bigrams = [cipher_text[ch]+cipher_text[ch+1] for ch in range(0,len(cipher_text)-2)
if cipher_text[ch] not in (" ","\t","\n") and
cipher_text[ch+1] not in (" ","\t","\n")]
bigram_counts = Counter(bigrams)
top_bigrams = ["in","zu","im","es","an","er","um"]
if len(bigrams) > len(top_bigrams):
for each_topbigram,x in zip(top_bigrams,range(0,len(top_bigrams))):
cipher_text = cipher_text.replace(bigram_counts.most_common()[x][0],each_topbigram)
else:
for each_topbigram,x in zip(top_bigrams,range(0,len(bigram_counts.most_common()))):
cipher_text = cipher_text.replace(bigram_counts.most_common()[x][0],each_topbigram)
with open("Math_Analysis_Method_Output.txt", "a", encoding= "utf-8") as f:
f.write("Bigram Analysis - German")
f.write("\n"+"="*100+"\n")
f.write(cipher_text)
f.write("\n"+"="*100+"\n")
# for trigram frequency analysis
counts = Counter(cipher_text)
character_counts = [ch for ch,_ in counts.most_common() if ch not in excludes]
trigrams = [cipher_text[ch]+cipher_text[ch+1]+cipher_text[ch+2] for ch in range(0,len(cipher_text)-3)
if cipher_text[ch] not in (" ","\t","\n") and
cipher_text[ch+1] not in (" ","\t","\n") and
cipher_text[ch+2] not in (" ","\t","\n")]
trigram_counts = Counter(trigrams)
top_trigrams = ["der","die","und","den","von","das","mit"]
if len(trigrams) > len(top_trigrams):
for each_toptrigram,x in zip(top_trigrams,range(0,len(top_trigrams))):
cipher_text = cipher_text.replace(trigram_counts.most_common()[x][0],each_toptrigram)
else:
for each_toptrigram,x in zip(top_trigrams,range(0,len(trigram_counts.most_common()))):
cipher_text = cipher_text.replace(trigram_counts.most_common()[x][0],each_toptrigram)
with open("Math_Analysis_Method_Output.txt", "a", encoding= "utf-8") as f:
f.write("Trigram Analysis - German")
f.write("\n"+"="*100+"\n")
f.write(cipher_text)
f.write("\n"+"="*100+"\n")
else:
pass
print("Please open the file Math_Analysis_Method_Output.txt file generated"+"\n")