Implementing Column Permutation Encryption in Python

PythonPythonBeginner
Practice Now

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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-302705{{"`Implementing Column Permutation Encryption in Python`"}} python/strings -.-> lab-302705{{"`Implementing Column Permutation Encryption in Python`"}} python/conditional_statements -.-> lab-302705{{"`Implementing Column Permutation Encryption in Python`"}} python/for_loops -.-> lab-302705{{"`Implementing Column Permutation Encryption in Python`"}} python/list_comprehensions -.-> lab-302705{{"`Implementing Column Permutation Encryption in Python`"}} python/lists -.-> lab-302705{{"`Implementing Column Permutation Encryption in Python`"}} python/tuples -.-> lab-302705{{"`Implementing Column Permutation Encryption in Python`"}} python/function_definition -.-> lab-302705{{"`Implementing Column Permutation Encryption in Python`"}} python/build_in_functions -.-> lab-302705{{"`Implementing Column Permutation Encryption in Python`"}} end

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: ,

  1. The length of the key qiao is 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 for qiao are 4-2-1-3.
  2. 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 ,
  1. Since the last line is missing a letter, the padding character is used to fill it.
  2. 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
  1. 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:

  1. First, we check if the input text is empty. If it is, we return None.
  2. We define the key as "qiao" and the padding_char as ",".
  3. We create a key_order list 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.
  4. We pad the input text with the padding_char to ensure that the length of the text is a multiple of the length of the key.
  5. 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.
  6. We rearrange the columns of the matrix according to the key_order we calculated earlier.
  7. 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.

Other Python Tutorials you may like