Transposition Ciphers


All the techniques examined so far involve the substitution of a ciphertext symbol for a plaintext symbol. A very different kind of mapping is achieved by performing some sort of permutation on the plaintext letters. This technique is referred to as a transposition cipher.

The simplest such cipher is the rail fence technique, in which the plaintext is written down as a sequence of diagonals and then read off as a sequence of rows. For example, to encipher the message “meet me after the toga party” with a rail fence of depth 2, we write the following:

m     e     m   a    t     r     h     t     g     p     r     y
    e     t     e     f     e     t     e     o     a     a     t

The encrypted message is
MEMATRHTGPRYETEFETEOAAT

This sort of thing would be trivial to cryptanalyze.A more complex scheme is to write the message in a rectangle, row by row, and read the message off, column by column, but permute the order of the columns. The order of the columns then becomes the key to the algorithm. For example,
Thus, in this example, the key is 4312567.To encrypt, start with the column that is labeled 1, in this case column 3.Write down all the letters in that column. Proceed to column 4, which is labeled 2, then column 2, then column 1, then columns 5, 6, and 7.

A pure transposition cipher is easily recognized because it has the same letter frequencies as the original plaintext. For the type of columnar transposition just shown, cryptanalysis is fairly straightforward and involves laying out the ciphertext in a matrix and playing around with column positions. Digram and trigram frequency tables can be useful.

The transposition cipher can be made significantly more secure by performing more than one stage of transposition.The result is a more complex permutation that is not easily reconstructed. Thus, if the foregoing message is reencrypted using the same algorithm,
 

To visualize the resultof this double transposition, designate the letters in the original plaintext message by the numbers designating their position. Thus, with 28 letters in the message, the original sequence of letters is

01 02 03 04 05 06 07 08 09 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28

After the first transposition, we have
03 10 17 24 04 11 18 25 02 09 16 23 01 08
15 22 05 12 19 26 06 13 20 27 07 14 21 28

which has a somewhat regular structure. But after the second transposition, we have
17 09 05 27 24 16 12 07 10 02 22 20 03 25
15 13 04 23 19 14 11 01 26 21 18 08 06 28
This is a much less structured permutation and is much more difficult to cryptanalyze

Example( University Question)
Let's encrypt the phrase "you only live once, but if you do it right, once is enough" using the transposition cipher with the key 3,2,1,4,5.

Steps to Encrypt:


1.Remove Spaces and Punctuation:

Original text: YOU ONLY LIVE ONCE BUT IF YOU DO IT RIGHT ONCE IS ENOUGH

Cleaned text: YOULIVEONCEBUTIFYOUDOITRIGHTONCEISENOUGH

2.Write the Plaintext in Rows:

  • Divide into blocks of size 5 (the length of the key)
Y    O    U    O   N
L    Y     L    I     V
E    O    N    C    E
B    U    T    I     F
Y    O    U   D   O
I     T     R   I     G
H    T    O   N    C
E    I     S    E    N
O   U    G   H    X

3.Rearrange the Columns According to the Key (3,2,1,4,5):
  • Original: 1 2 3 4 5
  • Rearranged: 3 2 1 4 5
U  O   Y   O    N      
L   Y   L    I     V
N   O  E   C     E
T   U   B   I      F
U   O  Y   D    O
R   T    I    I     G
O   T   H  N     C
S    I    E   E    N
G   U   O  H    X

4.Read Down the Columns in the New Order:
  • Column 1: U L N T U R O S G
  • Column 2: O Y O U O T T I U
  • Column 3: Y L E B Y I H E O
  • Column 4: O I C I D I N E  H
  • Column 5: N V E F O G C N X
5.Ciphertext:
  • Combine the columns: U L N T U R O S G O Y O U O T T I U Y L E B Y I H E O O I C I D I N E  H N V E F O G C N X
Steps for Decryption:

1.Determine the Number of Columns:

The key 3,2,1,4,5 indicates that there are 5 columns.

2.Determine the Number of Rows:

The ciphertext length is 40 characters. Dividing this by the number of columns (5), we get 8 rows.

3.Write the Ciphertext into Columns:

We will place the ciphertext into columns based on the rearranged key order (3,2,1,4,5):

  • Column 1 (3rd in order): U L N T U R O S G
  • Column 2 (2nd in order):O Y O U O T T I U
  • Column 3 (1st in order): Y L E B Y I H E O
  • Column 4 (4th in order): O I C I D I N E  H
  • Column 5 (5th in order): N V E F O G C N X

4.Rearrange Columns to Original Order (1,2,3,4,5):

Place the columns back into the original positions as specified by the key order:

5.Read the Rows to Obtain the Plaintext:

Reading across each row, we get:

Row 1: Y O U O N

Row 2: L Y L I V

Row 3: E O N C E

Row 4: B U T I F

Row 5: Y O U D O

Row 6: I T R I G 

Row 7: H T O N C 

Row 8: E I S E N 

Row 9:O U G H  X

6.Combine the Rows to Form the Plaintext:

  • Plaintext: YOUONLYLIVEONCEBUTIFYOUDOITRIGHTONCEISENOUGH

Thus, the original message "you only live once, but if you do it right, once is enough" is restored by reversing the columnar transposition using the specified key.

Python Code for Encryption

def encrypt_transposition_cipher(plaintext, key):
    # Remove spaces and punctuation from the plaintext
    cleaned_text = ''.join(filter(str.isalnum, plaintext))

    # Determine the number of columns and create the grid
    num_columns = len(key)
    num_rows = (len(cleaned_text) + num_columns - 1) // num_columns  # Ceiling division
    grid = [''] * num_columns

    # Fill the grid row-wise
    for i, char in enumerate(cleaned_text):
        column = i % num_columns
        grid[column] += char

    # Create the ciphertext by reading the columns in the order specified by the key
    ciphertext = ''
    for k in key:
        ciphertext += grid[k - 1]

    return ciphertext

# Example usage
plaintext = "you only live once, but if you do it right, once is enough"
key = [3, 2, 1, 4, 5]

encrypted_text = encrypt_transposition_cipher(plaintext, key)
print("Encrypted text:", encrypted_text)

Python Code for Decryption

def decrypt_transposition_cipher(ciphertext, key):
    # Determine the number of columns and rows
    num_columns = len(key)
    num_rows = (len(ciphertext) + num_columns - 1) // num_columns  # Ceiling division

    # Calculate the number of full and short columns
    num_full_columns = len(ciphertext) % num_columns
    num_chars_in_full_columns = num_rows
    num_chars_in_short_columns = num_rows - 1

    # Create an empty grid to hold the characters in columns
    grid = [''] * num_columns

    # Fill the columns with characters from the ciphertext
    index = 0
    for col in range(num_columns):
        current_col_length = num_chars_in_full_columns if key.index(col + 1) < num_full_columns else num_chars_in_short_columns
        grid[col] = ciphertext[index:index + current_col_length+1]
        index += current_col_length+1
       # Reconstruct the plaintext by reading across the rows
    plaintext = ''
    for i in range(num_rows):
        for j in key:
            plaintext += grid[j-1][i]

    return plaintext

# Example usage
ciphertext = "ULNTUROSGOYOUOTTIUYLEBYIHEOOICIDINEHNVEFOGCNX"
key = [3, 2, 1, 4, 5]

decrypted_text = decrypt_transposition_cipher(ciphertext, key)
print("Decrypted text:", decrypted_text)

Comments

Popular posts from this blog

Cryptographic Algorithms CST 393 KTU CS Honour Notes Semester V -Dr Binu V P

Syllabus CST 393 Cryptographic Algorithms

Computer Security Concept- CIA Triad