Yin Book Encryption Implementation and Testing

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to implement the Yin Book Encryption algorithm, a method of dividing a complete message into three parts and delivering them separately to the recipient. This encryption technique was used in ancient times to prevent information leakage, even if one of the messengers was captured by the enemy.

👀 Preview

## Example 1
>>> text = "Hello, World!"
>>> print(yin_book_encryption(text))
['H', 'ell', 'o, Wor', 'ld!']

## Example 2
>>> text = "!@#$%^&*) Hello, World!"
>>> print(yin_book_encryption(text))
['!', '@#$', '%^&*) ', 'Hello, Wor', 'ld!']

## Example 3
>>> text = "None"
>>> print(yin_book_encryption(text))
["N", "one"]

## Example 4
>>> text = ''
>>> print(yin_book_encryption(text))
None

ðŸŽŊ Tasks

In this project, you will learn:

  • How to implement the yin_book_encryption function to split a given text into multiple parts according to the Yin Book Encryption rule.
  • How to test the yin_book_encryption function with different input examples.
  • How to understand the implementation of the yin_book_encryption function and the helper calculate_length function.

🏆 Achievements

After completing this project, you will be able to:

  • Understand the concept of the Yin Book Encryption algorithm.
  • Implement the yin_book_encryption function to encrypt and decrypt messages.
  • Test the yin_book_encryption function with various input scenarios.
  • Modify the yin_book_encryption function to fit your specific requirements.

Implement the yin_book_encryption Function

In this step, you will implement the yin_book_encryption function in the yin_book.py file to achieve the desired functionality.

  1. Open the yin_book.py file in your preferred code editor.

  2. Implement the yin_book_encryption function according to the requirements:

    • The function should take a string text as input and return a list of encrypted parts.
    • The length of the first part should be 1, the second part should be 1 + 2, the third part should be 1 + 2 + 3, and so on, until the text is completely divided.
    • If the input text is empty, the function should return None.
    • A space counts as one character.

Here's the code you can use to implement the yin_book_encryption function:

def yin_book_encryption(text: str) -> list:
    """
    Encrypts the given text according to the Yin Book encryption rule.

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

    Returns:
        list: The list of encrypted parts of the text.

    """

    def calculate_length(index):
        """
        Calculates the length of each part based on the given index.

        Args:
            index (int): The index of the part.

        Returns:
            int: The length of the part.

        """
        return index * (index + 1) // 2

    if not text:
        return None

    encryption_text = []
    i = 0
    index = 1

    while i < len(text):
        part_length = calculate_length(index)
        sub_text = text[i : i + part_length]
        encryption_text.append(sub_text)
        i += part_length
        index += 1

    return encryption_text

Test the yin_book_encryption Function

In this step, you will test the yin_book_encryption function with the provided examples.

  1. Open the yin_book.py file in your code editor.
  2. Add the following code at the end of the file to test the yin_book_encryption function:
## Example 1
text = "Hello, World!"
print(yin_book_encryption(text))

## Example 2
text = "!@#$%^&*) Hello, World!"
print(yin_book_encryption(text))

## Example 3
text = "None"
print(yin_book_encryption(text))

## Example 4
text = ''
print(yin_book_encryption(text))
  1. Save the yin_book.py file and run the code in the terminal using the python yin_book.py command. The output should be:
['H', 'ell', 'o, Wor', 'ld!']
['!', '@#$', '%^&*) ', 'Hello, Wor', 'ld!']
['N', 'one']
None

The output matches the expected results, which means the yin_book_encryption function is working as expected.

Understand the yin_book_encryption Function

In this step, you will understand the implementation of the yin_book_encryption function.

  1. The yin_book_encryption function takes a string text as input and returns a list of encrypted parts.

  2. The calculate_length function is a helper function that calculates the length of each part based on the given index. The length of the nth part is 1 + 2 + 3 + ... + n.

  3. The function first checks if the input text is empty. If it is, the function returns None.

  4. The function then initializes an empty list encryption_text to store the encrypted parts.

  5. The function uses a while loop to iterate through the text and divide it into parts. In each iteration:

    • The function calculates the length of the current part using the calculate_length function.
    • The function extracts the current part from the text using slicing and appends it to the encryption_text list.
    • The function updates the starting index i and the part index index for the next iteration.
  6. Finally, the function returns the encryption_text list containing the encrypted parts.

By understanding the implementation, you can now use the yin_book_encryption function in your own projects or modify it to fit your specific requirements.

âœĻ 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