def test(key_length): if args.verbose: print(f"Testing key length {key_length}") groups = [] for n in range(1, key_length + 1): groups.append(subgroup(n, key_length)) a = ord('A') key = "" for n, group in enumerate(groups): coef = utils.coincidence_index(group) if args.all: print(f"Subgroup {n + 1} (IC: {coef})\n{group}") best_subkey = ('A', 0) for i in range(MODULE): shift = (MODULE - i)%MODULE decrypt = caesar.caesar(group, shift) frequencies = utils.most_frequent_chars(decrypt) score = utils.match_score(''.join(map(lambda x: x[0], frequencies))) subkey = chr(a + i) if args.all: print(f"Testing subkey '{subkey}' with match score {round(100 * (score/MAX_SCORE))}%") if best_subkey[1] < score: best_subkey = (subkey, score) if args.all: print(f"Best subkey is '{best_subkey[0]}' with match score {round(100 * (best_subkey[1]/MAX_SCORE))}%") key += best_subkey[0] decrypt = vigenere(text, key) return (key, decrypt) if validator.is_valid(decrypt) else FAILED
def friedman(text, frequencies=None): kp = ENGLISH_IC kr = MIN_ENGLISH_IC ko = utils.coincidence_index(text, frequencies) return (ko, math.ceil((kp - kr)/(ko - kr)))