1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Decrypt a text with a given key if in the encryption if was sorted alphabetically

Discussão em 'Python' iniciado por Stack, Setembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    My task is regarding transposition cipher, but with added element of sorting the columns of the matrix alphabetically using sorted() function.

    For example, given the text to encrypt "Flamingo_is_a_bird" using the cipher key "Banner", the process of encrypting goes like this:

    | B | A | N | N | E | R | <== KEY
    -------------------------------------------------------------------
    | F | L | A | M | I | N |
    | G | O | _ | I | S | _ |
    | A | _ | B | I | R | D |


    Key is then sorted alphabetically, and each column moves accordingly. Turns into:

    | A | B | E | N | N | R | <== SORTED KEY ALPHABETICALLY
    -------------------------------------------------------------------
    | L | F | I | A | M | N |
    | O | G | S | _ | I | _ |
    | _ | A | R | B | I | D |


    And then the final cipher text is read by column from left to right, top to bottom. Turns into:

    LO_FGAISRA_BMIIN_D


    Given the original cipher key, how can I revert back the columns to the original position, preferably using methods like sorted(), and without saving the key somewhere in the code or the computer (like a real encryption/decryption)?

    This is my encrypting code. (number_of_columns is len(cipher_key)):

    cipher_key_order = sorted(range(len(cipher_key)) , key=lambda alphabet: cipher_key[alphabet])
    i = 0
    encrypted_matrix = [[" " for column in range(0, number_of_columns)] for row in range(0, number_of_rows)]
    for col in range(number_of_columns):
    for row in range(number_of_rows):
    encrypted_matrix[row][col] = original_matrix[row][cipher_key_order]
    i += 1

    Continue reading...

Compartilhe esta Página