Esempio n. 1
0
def decode(ciphertext, key):
    square = mix_alphabet(key, ALPHABET25)
    digraphs = split_string_blocks(ciphertext, 2)

    plaintext = str()
    for digraph in digraphs:
        #get both chars, their coordinates
        char1, char2 = list(digraph)
        row1, column1 = get_coordinates(square, char1)
        row2, column2 = get_coordinates(square, char2)
        encoded_row1, encoded_column1 = row1, column1
        encoded_row2, encoded_column2 = row2, column2

        #apply transformation rules
        if row1 != row2 and column1 != column2:  # making a rectangle - take the other 2 corners, with the same row respectively
            encoded_row1, encoded_column1 = row1, column2
            encoded_row2, encoded_column2 = row2, column1
        elif row1 != row2 and column1 == column2:  # same column - get the caracter up
            encoded_row1 = (row1 - 1) % 5
            encoded_row2 = (row2 - 1) % 5
        elif row1 == row2 and column1 != column2:  # same row - get the caracter left
            encoded_column1 = (column1 - 1) % 5
            encoded_column2 = (column2 - 1) % 5

        #output resulting chars
        encoded_char1 = get_char(square, encoded_row1, encoded_column1)
        encoded_char2 = get_char(square, encoded_row2, encoded_column2)
        plaintext += encoded_char1 + encoded_char2

    return plaintext
Esempio n. 2
0
def decode(ciphertext, key):
    square = mix_alphabet(key, ALPHABET25)
    digraphs = split_string_blocks(ciphertext, 2)

    plaintext = str()
    for digraph in digraphs:
        #get both chars, their coordinates
        char1, char2 = list(digraph)
        row1, column1 = get_coordinates(square, char1)
        row2, column2 = get_coordinates(square, char2)
        encoded_row1, encoded_column1 = row1, column1
        encoded_row2, encoded_column2 = row2, column2

        #apply transformation rules
        if row1 != row2 and column1 != column2: # making a rectangle - take the other 2 corners, with the same row respectively
            encoded_row1, encoded_column1 = row1, column2
            encoded_row2, encoded_column2 = row2, column1
        elif row1 != row2 and column1 == column2: # same column - get the caracter up
            encoded_row1 = (row1 - 1) % 5
            encoded_row2 = (row2 - 1) % 5
        elif row1 == row2 and column1 != column2: # same row - get the caracter left
            encoded_column1 = (column1 - 1) % 5
            encoded_column2 = (column2 - 1) % 5

        #output resulting chars
        encoded_char1 = get_char(square, encoded_row1, encoded_column1)
        encoded_char2 = get_char(square, encoded_row2, encoded_column2)
        plaintext += encoded_char1 + encoded_char2

    return plaintext
Esempio n. 3
0
def second_sifra(data, key, alphabet_key):
    #now the substitution alphabet is generated from a passphrase

    consonants = mix_alphabet(alphabet_key, "bcdfghlmnpqrstxyz")
    #then we'll work on the vowels, but we'll insert them every 3 character
    #let's split the consonants string
    consonants_blocks = split_string_blocks(consonants, 3)
    #and parse the key for its used vowels
    vowels = mix_alphabet(alphabet_key, "aeiou")
    #let's merge the consonants and vowels data

    alphabet = "".join(i + j
                       for i, j in zip_extend(consonants_blocks, list(vowels)))

    #assert alphabet == "rmqacntupsbidfgehlxoyz"
    constant_part, rotated_part = alphabet[:11], Str(alphabet[11:])

    #now to generate the pairs, we'll do the same, but merge the vowel every char blocks
    consonants_blocks = split_string_blocks(consonants, 1)
    pairsString = Str("".join(
        i + j for i, j in zip_extend(consonants_blocks, list(vowels))))
    pairs = pairsString.splitblock(2)

    #now we have the pairs, the initial substitution alphabet
    #let's generate the 'rotated' alphabet for each pair
    alphabets = dict()
    for index, pair in enumerate(pairs):
        #let's get the pair by its first char in the order
        alphabets[pair] = constant_part + (rotated_part >> index)

    # now the actual encryption
    # for this cipher, the xth character of the key is used to decrypt the xth word (space separated) of the plaintext
    output = str()
    key_index = 0
    for char in data:
        pair = [p for p in alphabets if key[key_index] in p]
        if pair:
            char = substitute(char, alphabets[pair[0]])
        if char == ' ':
            key_index += 1
        output += char
    return output
Esempio n. 4
0
def second_sifra(data, key, alphabet_key):
    #now the substitution alphabet is generated from a passphrase

    consonants = mix_alphabet(alphabet_key, "bcdfghlmnpqrstxyz")
    #then we'll work on the vowels, but we'll insert them every 3 character
    #let's split the consonants string
    consonants_blocks = split_string_blocks(consonants, 3)
    #and parse the key for its used vowels
    vowels = mix_alphabet(alphabet_key, "aeiou")
    #let's merge the consonants and vowels data

    alphabet = "".join(i + j for i, j in zip_extend(consonants_blocks, list(vowels)))

    #assert alphabet == "rmqacntupsbidfgehlxoyz"
    constant_part, rotated_part = alphabet[:11], Str(alphabet[11:])

    #now to generate the pairs, we'll do the same, but merge the vowel every char blocks
    consonants_blocks = split_string_blocks(consonants, 1)
    pairsString = Str("".join(i + j for i, j in zip_extend(consonants_blocks, list(vowels))))
    pairs = pairsString.splitblock(2)

    #now we have the pairs, the initial substitution alphabet
    #let's generate the 'rotated' alphabet for each pair
    alphabets = dict()
    for index, pair in enumerate(pairs):
        #let's get the pair by its first char in the order
        alphabets[pair] = constant_part + (rotated_part >> index)

    # now the actual encryption
    # for this cipher, the xth character of the key is used to decrypt the xth word (space separated) of the plaintext
    output = str()
    key_index = 0
    for char  in data:
        pair = [p for p in alphabets if key[key_index] in p]
        if pair:
            char = substitute(char, alphabets[pair[0]])
        if char == ' ':
            key_index += 1
        output += char
    return output