Exemple #1
0
def restore_key(cyphertext, key_len):
    key = ''
    blocks = processing.get_blocks(text=cyphertext, size=key_len)
    columns = processing.get_columns(blocks)
    frequencies = _get_letter_frequencies(text=cyphertext)
    for column in columns:
        key += _find_key_letter(text=column, lf=frequencies)
    return key
Exemple #2
0
def _decypher(cyphertext, key):
    letters = string.ascii_uppercase
    shifts = [letters.index(letter) for letter in key]
    blocks = processing.get_blocks(text=cyphertext, size=len(key))
    cols = processing.get_columns(blocks)
    decyphered_blocks = processing.to_blocks(
        [fa.shift(col, shift) for col, shift in zip(cols, shifts)])
    decyphered = ''.join(decyphered_blocks)
    return decyphered
Exemple #3
0
def find_key_length(cyphertext, max_key_len):
    min_diff = maxsize
    key_len = 0
    for candidate_length in range(1, max_key_len + 1):
        groups = processing.get_blocks(text=cyphertext, size=candidate_length)
        columns = processing.get_columns(groups)
        ics = [_ic(letter_counts=fa.get_letter_counts(text=column)) for column in columns]
        delta_bar_ic = sum(ics) / len(ics)
        if EN_IC-delta_bar_ic < min_diff:
            min_diff = EN_IC-delta_bar_ic
            key_len = candidate_length
        print('KEY_LENGTH: ' + str(candidate_length) + '\n')
        print('IC by column: '+str(ics))
        print('delta bar IC: '+str(delta_bar_ic)+'\n')
    return key_len