Transposition cipher

Encryption

The transposition cipher encrypts a message by rearranging its characters based on a key that determines the number of columns in a grid. The message is written in the grid from left to right, and the characters are then read vertically, column by column, to form the encrypted output. The possible key values are determined by the length of the message, as a key longer than the message can introduce unnecessary complexity without enhancing security. At the same time, a key that is too short may not adequately scramble the message. Empty fields at the end of the grid are simply skipped during the reading process. The table below (a "transposition grid") has been generated using the key 6 for the string "This is my secret message."

123456
This/i
s/my/s
ecret/
messag
e.

To encrypt a message using a script, we have to assign an index to each number, following the order of letters in the text.

T
0
h
1
i
2
s
3
/
4
i
5
s
6
/
7
m
8
y
9
/
10
s
11
e
12
c
13
r
14
e
15
t
16
/
17
m
18
e
19
s
20
s
21
a
22
g
23
e
24
.
25

In the table above, we can notice a pattern. Each index equals the row number times the key (when the key is 6, the row numbers are 0, 6, 12, etc.) plus the column number (when the key is 6, the column numbers are 0, 1, 2, 3, 4, and 5). We can use this pattern in our algorithm.

T
0+0=0
h
0+1=1
i
0+2=2
s
0+3=3
/
0+4=4
i
0+5=5
s
6+0=6
/
6+1=7
m
6+2=8
y
6+3=9
/
6+4=10
s
6+5=11
e
12+0=12
c
12+1=13
r
12+2=14
e
12+3=15
t
12+4=16
/
12+5=17
m
18+0=18
e
18+1=19
s
18+2=20
s
18+3=21
a
18+4=22
g
18+5=23
e
24+0=24
.
24+1=25

Decryption

Our message has 26 characters, and we used the key 6, so to get the number of columns in the decryption transposition grid, we divide 26 by 6 and round up to 5. Now we can draw the table for the "Tsemeh ce.imrssyes tais g" encrypted string. The message is also written in the grid from left to right, and the characters are then read vertically, column by column, to form the decrypted output. However, here, we insert the black (empty) fields vertically, and we must calculate how many there are (when encrypting, we just filled the remaining fields at the end, which is not the case here). To find out how many empty fields there are, we can use this formula: (total_columns * key) - message_length.

12345
Tseme
h/ce.
imrs
syes
//ta
is/g

Implementation


import math

def encrypt_transposition(message, key): # the key represents the number of columns
    encrypted = [""] * key # creating an empty transposition grid
    # Iterating through each column
    for col in range(key):
        index = col
        # Looping through the message
        while index < len(message):
            encrypted[col] += message[index] # adding a character to the column
            index += key # moving to the next character in the same column
    return "".join(encrypted)

def decrypt_transposition(message, key):
    total_columns = int(math.ceil(len(message) / float(key))) # the number of rows for the transposition grid
    black_fields = (total_columns * key) - len(message) # the number of black fields in the last column
    decrypted = [""] * total_columns
    col = 0
    row = 0

    # Iterating through each character
    for char in message:
        decrypted[col] += char # adding a character to the current column
        col += 1 # moving to the next column
        # If we have reached the last column or a black field, we reset to the first column and move to the next row.
        if (col == total_columns) or (col == total_columns - 1 and row >= key - black_fields):
            col = 0
            row += 1
    return "".join(decrypted)

text_message = "This is my secret message."
key = 6

encrypted_message = encrypt_transposition(text_message, key)
print(encrypted_message + "|") # the "|" symbol is added so that no space is lost at the end
print(decrypt_transposition(encrypted_message, key) + "|")