Introduction
In this project, you will learn how to implement the Column Permutation Encryption algorithm in Python. Column Permutation Encryption is a method of encrypting plaintext by rearranging the columns of a matrix formed from the input text, based on a key word.
👀 Preview
## Sample 1
text = "welcometolq"; encryption_text = "ct,emlwooleq"
## Sample 2
text = "welcometolq "; encryption_text = "ct emlwooleq"
## Sample 3
text = "w"; encryption_text = ",,w,"
## Sample 4
text = None; encryption_text = None
🎯 Tasks
In this project, you will learn:
- The basic concept of Column Permutation Encryption
- How to implement the Column Permutation Encryption algorithm in Python
- How to test the encryption algorithm with sample inputs
🏆 Achievements
After completing this project, you will be able to:
- Understand the principles of Column Permutation Encryption
- Implement the Column Permutation Encryption algorithm in Python
- Encrypt and decrypt text using the Column Permutation Encryption method
Understand the Column Permutation Encryption
In this step, you will learn the basic concept of Column Permutation Encryption and how it works.
Column Permutation Encryption is a method of encrypting plaintext by writing it down line by line with a fixed number of characters per line (the number of unique letters in the key, which is usually a word with no repeated letters). If the number of characters in the last line is less than the fixed number of characters per line, special symbols are used to pad the line, forming a matrix. Then, the columns of the matrix are rearranged according to the alphabetical order of the letters in the key. Finally, the rearranged columns are read out one by one, resulting in the ciphertext.
For example, let's consider the following plaintext, key, and padding character:
Plaintext: welcometolq
Key: qiao
Padding character: ,
- The length of the key
qiaois 4. The key is then assigned numbers in the alphabetical order of the 26 English letters, with lower numbers assigned to letters that appear earlier in the order. The assigned numbers forqiaoare 4-2-1-3. - The plaintext is divided into lines, each consisting of 4 (the length of the key) letters, resulting in 4 columns:
| 1 | 2 | 3 | 4 |
|---|---|---|---|
| w | e | l | c |
| o | m | e | t |
| o | l | q | , |
- Since the last line is missing a letter, the padding character is used to fill it.
- The order of the columns in the matrix is rearranged according to the alphabetical order of the key:
| 4 | 2 | 1 | 3 |
|---|---|---|---|
| c | e | w | l |
| t | m | o | e |
| , | l | o | q |
- The content is then read out one column at a time, resulting in the ciphertext:
ct,emlwooleq.
Implement the Column Permutation Encryption
In this step, you will implement the Column Permutation Encryption algorithm in Python.
Open the column_permutation.py file and locate the column_permutation_encryption(text) function. This function takes a piece of text as input, uses the key qiao and the padding character , (comma) to perform column permutation encryption on the content, and returns the ciphertext.
Here's the code you need to implement:
def column_permutation_encryption(text: str) -> str:
if not text:
return None
key = "qiao"
padding_char = ","
key_order = []
sorted_key = sorted(key)
for i in range(0, len(key)):
key_order.append(sorted_key.index(key[i]))
padded_text = text.ljust(len(key) * ((len(text) - 1) // len(key) + 1), padding_char)
matrix = [
padded_text[i : i + len(key)] for i in range(0, len(padded_text), len(key))
]
encrypted_matrix = [[row[i] for i in key_order] for row in matrix]
encryption_text: str = "".join(
["".join(column) for column in zip(*encrypted_matrix)]
)
return encryption_text
Let's go through the code step by step:
- First, we check if the input
textis empty. If it is, we returnNone. - We define the
keyas"qiao"and thepadding_charas",". - We create a
key_orderlist to store the alphabetical order of the letters in the key. We do this by first sorting the key and then finding the index of each letter in the sorted key. - We pad the input
textwith thepadding_charto ensure that the length of the text is a multiple of the length of the key. - We create a matrix from the padded text, where each row represents a line of the text, and each column represents a character in the line.
- We rearrange the columns of the matrix according to the
key_orderwe calculated earlier. - Finally, we read the rearranged columns one by one and concatenate them to form the ciphertext.
Test the Column Permutation Encryption
Now that you have implemented the Column Permutation Encryption algorithm, let's test it with some sample inputs.
Add the following code at the end of the column_permutation.py file:
if __name__ == "__main__":
txt = input()
print(column_permutation_encryption(txt))
This code will prompt the user to enter a piece of text, and then it will call the column_permutation_encryption() function with the input text and print the resulting ciphertext.
Save the file and run the following command in the terminal:
python3 column_permutation.py
Enter some sample texts and observe the resulting ciphertexts. You can use the examples provided in the original challenge as a reference:
## Sample 1
text = "welcometolq"
encryption_text = "ct,emlwooleq"
## Sample 2
text = "welcometolq "
encryption_text = "ct emlwooleq"
## Sample 3
text = "w"
encryption_text = ",,w,"
## Sample 4
text = None
encryption_text = None
Congratulations! You have successfully implemented the Column Permutation Encryption algorithm in Python.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



