コード例 #1
0
def analyse(cypher):
    # Figure out the most common character in cypher text. Since this is just
    # a statistical analysis, there is no guarantee, that the most common
    # character in the cypher matches the most common character in the language
    # of the plain text. So let's assume that the correct match is somewhere
    # among the 3 most common characters. Return those and let the caller
    # decide what to do with this problem.
    most_common = Counter(cypher).most_common(3)

    # But calculate the key as distance of common characters
    possible_keys = list()
    for m in most_common:
        distance = ALPHABET.index(PLAIN_MOST_COMMON) - ALPHABET.index(m[0])
        possible_keys.append(distance)

    return possible_keys
コード例 #2
0
def decrypt_character(c, key):
        # Calculate the new index for the character lookup
        plain_index = ALPHABET.index(c) + key

        # Make sure we can substitute in both directions
        if plain_index < 0:
            plain_index += len(ALPHABET)
        elif plain_index >= len(ALPHABET):
            plain_index -= len(ALPHABET)

        return ALPHABET[plain_index]