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.
Cryptography - Transposition Techniques
Transposition ciphers, also known as permutation ciphers, are a type of classical cipher in which the positions of the characters in the plaintext are shifted according to a certain system, but the actual characters themselves remain unchanged. These techniques provide a method of rearranging the letters of a message, making it unreadable to anyone who does not know the permutation or system used to shuffle the characters.
Unlike substitution ciphers (where each letter is replaced by another letter), transposition ciphers keep the original characters intact but alter their positions in the message.
How Transposition Ciphers Work
In a transposition cipher, the order of characters in the plaintext is scrambled using a specific algorithm or key. The main idea is that even though the individual letters of the message remain the same, the reordering makes the ciphertext appear random.
General Process of Transposition:
- The plaintext is written in a grid or matrix form (or a similar structure).
- The positions of the characters in the grid are rearranged based on a particular pattern or key.
- The resulting ciphertext is extracted by reading the letters in a specific order determined by the transposition.
Types of Transposition Ciphers
1. Rail Fence Cipher
The Rail Fence Cipher is one of the simplest transposition ciphers. It involves writing the message in a zigzag pattern across multiple "rails" (rows), and then reading the ciphertext horizontally.
How Rail Fence Cipher Works:
- Write the plaintext in a zigzag pattern across multiple rows.
- Read off the rows one by one to form the ciphertext.
Example:
Let's encrypt the message "HELLO WORLD" using 3 rails.
Rail 1: H . . . O . . . R . .
Rail 2: . E . L . W . L . D .
Rail 3: . . L . . O . . . .
Reading the rows left to right:
Rail 1: HOR
Rail 2: ELWL D
Rail 3: LO
Ciphertext: "HOR ELWL DLO"
Python Code for Rail Fence Cipher:
def rail_fence_encrypt(plaintext, rails):
# Create a matrix with rails rows and columns equal to the length of the message
fence = [['' for _ in range(len(plaintext))] for _ in range(rails)]
# Variable to track the direction of writing (down or up)
row, step = 0, 1
for col in range(len(plaintext)):
fence[row][col] = plaintext[col]
if row == 0:
step = 1
elif row == rails - 1:
step = -1
row += step
# Read the matrix row by row to generate the ciphertext
ciphertext = ''.join([''.join(r) for r in fence])
return ciphertext
def rail_fence_decrypt(ciphertext, rails):
# Create a matrix to store the decrypted message
fence = [['' for _ in range(len(ciphertext))] for _ in range(rails)]
index = 0
# Fill the matrix with characters from the ciphertext
for row in range(rails):
position = 0
for col in range(len(ciphertext)):
if fence[row][col] == '':
fence[row][col] = ciphertext[index]
index += 1
# Read the matrix to decrypt the message
plaintext = ''.join([fence[row][col] for col in range(len(ciphertext)) for row in range(rails)])
return plaintext.strip()
# Example usage:
plaintext = "HELLO WORLD"
rails = 3
ciphertext = rail_fence_encrypt(plaintext, rails)
print(f"Ciphertext: {ciphertext}")
decrypted_text = rail_fence_decrypt(ciphertext, rails)
print(f"Decrypted Text: {decrypted_text}")
2. Columnar Transposition
In the Columnar Transposition cipher, the plaintext is arranged in a grid (like a table) and then read column by column based on a key that defines the column order.
How Columnar Transposition Works:
- Write the plaintext in rows of fixed length (depending on the key).
- The columns are rearranged according to the key.
- The ciphertext is obtained by reading the columns in the order specified by the key.
Example:
Let’s encrypt the message "HELLO WORLD" with the key "4312".
Plaintext: HELLO WORLD
Key: 4 3 1 2
Write the message into a grid:
H E L L O
W O R L D
Rearranging the columns according to the key (4, 3, 1, 2):
Column 4: O D
Column 3: R L
Column 1: H W
Column 2: E O
Ciphertext: "OD RL HW EO"
Python Code for Columnar Transposition Cipher:
def columnar_transposition_encrypt(plaintext, key):
# Calculate the number of columns based on the length of the key
key_length = len(key)
grid = ['' for _ in range(key_length)]
# Sort the key in ascending order
sorted_key = sorted(range(len(key)), key=lambda k: key[k])
# Fill the grid column by column
for i, char in enumerate(plaintext):
grid[i % key_length] += char
# Read columns based on the sorted key
ciphertext = ''.join([grid[i] for i in sorted_key])
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
grid = ['' for _ in range(num_columns)]
# Sort the key and distribute ciphertext into columns
sorted_key = sorted(range(len(key)), key=lambda k: key[k])
index = 0
for i in sorted_key:
grid[i] = ciphertext[index:index + num_rows]
index += num_rows
# Read the grid row by row
plaintext = ''.join([''.join([grid[i][j] for i in range(num_columns)]) for j in range(num_rows)])
return plaintext
# 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}")
3. Scytale Cipher
The Scytale Cipher is one of the oldest known transposition ciphers. It involves wrapping the message around a rod (or stick) of a particular diameter and then reading the message along the rod.
How Scytale Cipher Works:
- Write the plaintext in rows of a fixed length (determined by the size of the rod).
- Read the columns to get the ciphertext.
For example, if the rod has a circumference of 4 characters, we write the message in a grid with 4 columns and then read it down the columns to form the ciphertext.
Advantages of Transposition Ciphers
- Simplicity: Transposition ciphers are easy to implement and require only basic operations.
- Increased Security Over Substitution Ciphers: While they don't provide as strong security as modern algorithms, transposition ciphers offer a higher level of security compared to simple substitution ciphers.
Disadvantages of Transposition Ciphers
- Vulnerable to Frequency Analysis: If a transposition cipher is used alone (without additional encryption), an attacker can sometimes analyze patterns in the ciphertext to decrypt it.
- Key Management: Like any cipher, the key must be securely shared between the sender and the receiver, and its secrecy must be maintained.
Conclusion
Transposition ciphers are a simple yet effective way of encoding messages by rearranging the letters. Techniques like the Rail Fence Cipher, Columnar Transposition, and Scytale Cipher all manipulate the order of the letters to create ciphertext that seems random. While they provide a level of security by themselves, modern cryptographic systems often use these techniques in combination with other methods (such as substitution ciphers or symmetric encryption) to ensure strong protection against attacks.