break vig with poorly implemented heuristik

breakmono2
Eggert Jung 5 years ago
parent 98a78d3d35
commit 3e44a84ca9

@ -0,0 +1,67 @@
#def probe(key):
# key_hits = {}
# for x in range(ord('a'), ord('z')+1):
# count = 0
# for substr in substrings:
# if re.search(vig.decrypt(key+chr(x), substr[0:len(key)+1]), words):
# count+=1
# key_hits[key+chr(x)] = count
# return ([val for key, val in sorted(key_hits.items(), key = lambda ele: ele[1])][-1], key_hits)
def crack(current_key, max_depth):
key_hits = {}
for x in range(ord('a'), ord('z')+1):
count = 0
for substr in substrings:
if re.search(vig.decrypt(current_key+chr(x), substr[0:len(current_key)+1]), words):
count+=1
key_hits[current_key+chr(x)] = count
best = [key for key, val in sorted(key_hits.items(), key = lambda ele: ele[1])][-1]
print("best = {} ".format(best))
if len(current_key)+1 < max_depth:
crack(best, max_depth)
#bestkeys = []
#for key, val in key_hits.items():
# if val == best:
# bestkeys.append(key)
#print(bestkeys)
#bestprobes = {bestkeys[0]: 0}
#if len(bestkeys) > 1:
# for key in bestkeys:
# print("probing {} ... ".format(key), end='')
# num = probe(key)[0]
# print(num)
# if(num > list(bestprobes.values())[0]):
# bestprobes = {}
# bestprobes[key] = num
# if(num == list(bestprobes.values())[0]):
# bestprobes[key] = num
# print("best probes: {}".format(bestprobes))
#for item in bestprobes.items():
# crack(item[0], max_depth)
#return crack(bestprobe[0], max_depth)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('FILE')
parser.add_argument('--keylen', type=int, metavar="INT", required=True)
args = parser.parse_args()
t = ''.join([x for x in open(args.FILE, "r").read().lower() if x.isalpha()])
substrings = [(t[i:i+args.keylen]) for i in range(0, len(t), args.keylen)]
import os
words = open(os.path.dirname(__file__)+"/words.txt", "r").read()
import vig
import re
crack("", args.keylen)
Loading…
Cancel
Save