Skip to content

This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.

6.3 KB
Raw Blame

Cryptography - Columnar Transposition

The Columnar Transposition Cipher is a type of transposition cipher where the plaintext is arranged in a grid (matrix) of a certain number of columns, and the ciphertext is generated by reading the grid column by column, based on a specific key.

In this cipher, the order of the columns is determined by the numerical order of the characters in the key. The key specifies how the columns are reordered, and then the ciphertext is formed by reading the columns in the new order.

How Columnar Transposition Works:

  1. Plaintext Arrangement: The plaintext message is written in rows, with each row containing a fixed number of characters based on the length of the key.
  2. Reordering Columns: The columns of the grid are rearranged according to the numerical order of the key.
  3. Ciphertext Extraction: The ciphertext is obtained by reading the characters column by column in the rearranged grid.

Steps Involved in Columnar Transposition:

  1. Write the plaintext into a grid with a fixed number of columns based on the key length.
  2. Fill in the grid with the message, row by row.
  3. Rearrange the columns based on the numerical order of the key.
  4. Read the columns in the new order to get the ciphertext.

Example of Columnar Transposition

Let’s encrypt the plaintext "HELLO WORLD" using the key "4312".

  1. Step 1: Write the plaintext into a grid. Since the key has 4 digits, the grid will have 4 columns. First, write the message into rows with 4 columns.

    Plaintext: HELLO WORLD
    Key: 4 3 1 2
    

    Arrange the plaintext in the grid:

    H E L L
    O W O R
    L D
    
  2. Step 2: Rearrange the columns according to the key. The key "4312" tells us the order in which the columns should be read. The numbers indicate the column order, so:

    • Column 4 (last): O D
    • Column 3: O R
    • Column 1 (first): H L
    • Column 2: E W
  3. Step 3: Read the columns in the new order to generate the ciphertext. The ciphertext is obtained by reading the columns from left to right in the order given by the key.

    Ciphertext: ODO RO HL EW
    

    The final ciphertext is: "ODORO HL EW"


Python Code for Columnar Transposition Cipher

Below is a Python implementation for the Columnar Transposition Cipher:

def columnar_transposition_encrypt(plaintext, key):
    # Remove spaces from the plaintext
    plaintext = plaintext.replace(" ", "")
    
    # Calculate the number of columns and rows
    num_columns = len(key)
    num_rows = len(plaintext) // num_columns + (1 if len(plaintext) % num_columns != 0 else 0)
    
    # Create a matrix to store the message
    grid = [['' for _ in range(num_columns)] for _ in range(num_rows)]
    
    # Fill the matrix row by row with the plaintext characters
    index = 0
    for row in range(num_rows):
        for col in range(num_columns):
            if index < len(plaintext):
                grid[row][col] = plaintext[index]
                index += 1
    
    # Sort the key to determine the new order of columns
    sorted_key_indices = sorted(range(len(key)), key=lambda k: key[k])
    
    # Read the grid column by column in the new order
    ciphertext = ''.join([grid[row][sorted_key_indices.index(i)] for i in range(num_columns) for row in range(num_rows) if grid[row][sorted_key_indices.index(i)] != ''])
    
    return ciphertext

def columnar_transposition_decrypt(ciphertext, key):
    # Calculate the number of columns and rows
    num_columns = len(key)
    num_rows = len(ciphertext) // num_columns
    
    # Create a matrix to store the decrypted message
    grid = [['' for _ in range(num_columns)] for _ in range(num_rows)]
    
    # Sort the key to get the column order
    sorted_key_indices = sorted(range(len(key)), key=lambda k: key[k])
    
    # Fill the grid with ciphertext characters column by column
    index = 0
    for i in sorted_key_indices:
        for row in range(num_rows):
            if index < len(ciphertext):
                grid[row][i] = ciphertext[index]
                index += 1
    
    # Read the grid row by row to get the plaintext
    plaintext = ''.join([grid[row][col] for row in range(num_rows) for col in range(num_columns)])
    
    return plaintext.strip()

# Example usage
plaintext = "HELLO WORLD"
key = "4312"
ciphertext = columnar_transposition_encrypt(plaintext, key)
print(f"Ciphertext: {ciphertext}")

decrypted_text = columnar_transposition_decrypt(ciphertext, key)
print(f"Decrypted Text: {decrypted_text}")

Explanation of the Code:

  1. columnar_transposition_encrypt:

    • First, we calculate the number of rows and columns needed based on the key length.
    • We fill the grid with the plaintext characters row by row.
    • Then we rearrange the columns based on the order of the key, reading the columns in this new order to generate the ciphertext.
  2. columnar_transposition_decrypt:

    • We calculate the grid size and sort the key to determine the new column order.
    • We place the ciphertext characters back into the grid column by column in the new order.
    • Finally, we read the grid row by row to retrieve the original plaintext.

Example Output:

Ciphertext: ODORO HL EW
Decrypted Text: HELLO WORLD

Key Points About Columnar Transposition Cipher:

  1. Simplicity: The Columnar Transposition Cipher is relatively simple to implement but more secure than simpler ciphers like the Caesar Cipher, as it mixes the order of characters in a more complex way.
  2. Security: It is more secure than basic substitution ciphers, but still vulnerable to cryptanalysis, such as frequency analysis and known-plaintext attacks, especially if the key is too short or predictable.
  3. Key Management: The key plays a crucial role in the security of this cipher. The length of the key determines the number of columns, and the order of the key determines how the columns are rearranged.

Conclusion:

The Columnar Transposition Cipher is a simple and historical encryption technique where the plaintext is rearranged into a matrix based on a key. While it provides an improved level of security compared to simpler ciphers, it is not secure enough by modern standards and should not be used for protecting sensitive information without additional encryption methods.