|
|
|
|
@ -1,4 +1,36 @@
|
|
|
|
|
from random import randint as rand
|
|
|
|
|
from math import log10
|
|
|
|
|
|
|
|
|
|
class Scoring:
|
|
|
|
|
floor = 0
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.trigrams = {}
|
|
|
|
|
# read trigrams from file
|
|
|
|
|
with open(os.path.dirname(os.path.realpath(__file__)) + '/english_trigrams.txt', 'r') as trigram_file:
|
|
|
|
|
for line in trigram_file:
|
|
|
|
|
trigram, score = line.split(' ')
|
|
|
|
|
self.trigrams[trigram.lower()] = int(score)
|
|
|
|
|
|
|
|
|
|
score_sum = sum(self.trigrams.values())
|
|
|
|
|
self.floor = log10(0.1 / score_sum)
|
|
|
|
|
|
|
|
|
|
for i in list(self.trigrams.keys()):
|
|
|
|
|
self.trigrams[i] = log10(float(self.trigrams[i]) / score_sum)
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def get_score(self, text):
|
|
|
|
|
score = 0
|
|
|
|
|
|
|
|
|
|
for k in range(len(text) - 3):
|
|
|
|
|
trigram = text[k:k + 3]
|
|
|
|
|
if trigram in self.trigrams:
|
|
|
|
|
score += self.trigrams[trigram]
|
|
|
|
|
else:
|
|
|
|
|
score += self.floor
|
|
|
|
|
return score
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def randomize_key(key):
|
|
|
|
|
a_index = rand(0, 25)
|
|
|
|
|
b_index = rand(0, 25)
|
|
|
|
|
@ -34,17 +66,21 @@ if __name__ == "__main__":
|
|
|
|
|
import re
|
|
|
|
|
import mono
|
|
|
|
|
|
|
|
|
|
best_score = 0
|
|
|
|
|
best_score = -10000
|
|
|
|
|
best_key = key
|
|
|
|
|
|
|
|
|
|
scoring = Scoring()
|
|
|
|
|
|
|
|
|
|
trys = 0
|
|
|
|
|
while trys < 1000:
|
|
|
|
|
while trys < 20000:
|
|
|
|
|
score = 0
|
|
|
|
|
tmp = randomize_key(key)
|
|
|
|
|
plain = mono.mono_decrypt(t, tmp)
|
|
|
|
|
for word in words:
|
|
|
|
|
if re.search(word, plain):
|
|
|
|
|
score += 1
|
|
|
|
|
#for word in words:
|
|
|
|
|
# search_res = re.search(word, plain, re.IGNORECASE)
|
|
|
|
|
# if search_res:
|
|
|
|
|
# score += len(word)
|
|
|
|
|
score = scoring.get_score(plain)
|
|
|
|
|
if score > best_score:
|
|
|
|
|
trys = 0
|
|
|
|
|
best_score = score
|
|
|
|
|
|