Implementing Rail Fence Cipher in Python

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to implement the Rail Fence Cipher, a simple encryption method that rearranges the characters in a text to create a ciphertext.

👀 Preview

Thisatext.issample

ðŸŽŊ Tasks

In this project, you will learn:

  • How to set up a Python project directory and create the necessary files
  • How to implement the Rail Fence Cipher algorithm in Python
  • How to test the implemented encryption function

🏆 Achievements

After completing this project, you will be able to:

  • Understand the basic principles of the Rail Fence Cipher
  • Implement the Rail Fence Cipher encryption algorithm in Python
  • Test and validate the correctness of the implemented encryption function

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-302758{{"`Implementing Rail Fence Cipher in Python`"}} python/strings -.-> lab-302758{{"`Implementing Rail Fence Cipher in Python`"}} python/conditional_statements -.-> lab-302758{{"`Implementing Rail Fence Cipher in Python`"}} python/for_loops -.-> lab-302758{{"`Implementing Rail Fence Cipher in Python`"}} python/list_comprehensions -.-> lab-302758{{"`Implementing Rail Fence Cipher in Python`"}} python/lists -.-> lab-302758{{"`Implementing Rail Fence Cipher in Python`"}} python/tuples -.-> lab-302758{{"`Implementing Rail Fence Cipher in Python`"}} python/function_definition -.-> lab-302758{{"`Implementing Rail Fence Cipher in Python`"}} python/build_in_functions -.-> lab-302758{{"`Implementing Rail Fence Cipher in Python`"}} end

Implement the Rail Fence Cipher

In this step, you will implement the Rail Fence Cipher encryption algorithm in the fence.py file.

  1. Open the fence.py file in a text editor.
  2. Add the following code to the file:
def rail_fence_cipher(text: str) -> str:
    """
    Encrypts the text using the Rail Fence Cipher method.

    Args:
        text (str): The text to be encrypted.

    Returns:
        str: The encrypted text.
    """
    if text is None or len(text.strip()) == 0:
        return None
    group = text.split()
    grouped_list = [group[i : i + 2] for i in range(0, len(group), 2)]

    first_part = [sublist[0] for sublist in grouped_list]
    second_part = [sublist[1] for sublist in grouped_list if len(sublist) > 1]

    encryption_text = "".join(first_part + second_part)
    return encryption_text


if __name__ == "__main__":
    print(rail_fence_cipher("This is a sample text."))
  1. Implement the rail_fence_cipher function by following these steps:

    • Check if the input text is None or an empty string. If so, return None.
    • Split the text into groups of two characters each.
    • Extract the first character from each group and store them in a list.
    • Extract the second character from each group and store them in a list.
    • Concatenate the two lists to form the encrypted text.
    • Return the encrypted text.
  2. Save the fence.py file.

Test the Rail Fence Cipher

In this step, you will test the implemented Rail Fence Cipher by running the fence.py file.

  1. Open a terminal and navigate to the project directory.
  2. Run the fence.py file:
python3 fence.py
  1. The output should be the encrypted text:
Thisatext.issample

Congratulations! You have successfully implemented the Rail Fence Cipher in Python.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like