for i in range(2, 41): segments = slicer(raw_string, i, 50) combine = itertools.combinations(segments, 2) hamscore = 0 for s1, s2 in combine: hamscore += hamming_distance(s1, s2) / i hamscore /= 6 hams.append((hamscore, i)) hams.sort(key=itemgetter(0)) # Probable keylength keylength = hams[0][1] chunks = chunker(raw_string, keylength) transposed_blocks = zip(*chunks) i = 0 key = "" for block in transposed_blocks: scores = [] for x in range(0, 255): scores.append((score_string(single_byte_bruteforce(bytearray(block), chr(x))), chr(x))) scores.sort(key=itemgetter(0), reverse=True) print "Probable key for block {0} is {1}".format(i, scores[0][1]) i += 1 key += scores[0][1] print "Full key is {0}".format(key) + "\n" print "Decoded message: \n" + repeating_key_xor(raw_string, key)