Clear Code Encryption Implementation

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to implement a simple encryption and decryption algorithm called "Clear Code Encryption". This encryption method was used in China's early telegraph communication system during the late Qing Dynasty.

👀 Preview

## Example 1
numb = 1530; encryption_text = "0383"
## Example 2
numb = 0; encryption_text = "9853"
## Example 3
numb = 12345; encryption_text = None

ðŸŽŊ Tasks

In this project, you will learn:

  • How to understand the encryption method and its underlying principles
  • How to implement the plain_code_encryption(numb) function to perform the encryption
  • How to test the encryption function with various inputs
  • How to handle user input and call the encryption function

🏆 Achievements

After completing this project, you will be able to:

  • Explain the "Clear Code Encryption" method and its historical context
  • Implement a basic encryption and decryption algorithm using Python
  • Test and validate the encryption function with different input scenarios
  • Integrate user input and output into the encryption script

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/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") 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-302703{{"`Clear Code Encryption Implementation`"}} python/numeric_types -.-> lab-302703{{"`Clear Code Encryption Implementation`"}} python/strings -.-> lab-302703{{"`Clear Code Encryption Implementation`"}} python/type_conversion -.-> lab-302703{{"`Clear Code Encryption Implementation`"}} python/conditional_statements -.-> lab-302703{{"`Clear Code Encryption Implementation`"}} python/for_loops -.-> lab-302703{{"`Clear Code Encryption Implementation`"}} python/tuples -.-> lab-302703{{"`Clear Code Encryption Implementation`"}} python/function_definition -.-> lab-302703{{"`Clear Code Encryption Implementation`"}} python/build_in_functions -.-> lab-302703{{"`Clear Code Encryption Implementation`"}} end

Understand the Encryption Method

In this step, you will learn about the encryption method used in the "Clear Code Encryption" project.

The encryption method works as follows:

  1. The four digits of the clear code are respectively added to the four digits of the encryption key.
  2. For each digit addition, if the sum is greater than or equal to 10, omit 10 and only keep the digit in the units place.
  3. When decrypting, use the password subtracted by the four digits of the decryption key to obtain the clear code and find the corresponding text.

For example, let's say the clear code is 1530 and the encryption key is 9853.

  1. Adding the first digit: 1 + 9 = 10, treat 10 as 0.
  2. Adding the second digit: 5 + 8 = 13, omit 10 and keep 3.
  3. Adding the third digit: 3 + 5 = 8.
  4. Adding the fourth digit (10 + 3 = 13, omit 10 and keep 3).

The final encrypted text is "0383".

Implement the plain_code_encryption(numb) Function

In this step, you will implement the plain_code_encryption(numb) function in the plain_code.py file.

  1. Open the plain_code.py file in your code editor.

  2. Locate the plain_code_encryption(numb) function.

  3. Implement the function to perform the following tasks:

    • Input an arbitrary number between 0 and 9999 (including 0 and 9999), add 9853 to it, and obtain a 4-digit number (in the format of str) after addition, and return it.
    • If the input number is less than 4 digits, fill in the missing positions with 0. For example, if the input number is 32, it needs to be treated as 0032.
    • Add the numbers in the same position. If the sum is greater than or equal to 10, omit 10 and only keep the digit in the units place.
    • If the input number is not between 0 and 9999, return None.

Here's the completed code for the plain_code_encryption(numb) function:

def plain_code_encryption(numb: int) -> str:
    if numb < 0 or numb > 9999:
        return None

    numb_str = str(numb).zfill(4)
    key_str = "9853"
    encryption_text = "".join(
        str((int(n) + int(k)) % 10) for n, k in zip(numb_str, key_str)
    )

    return encryption_text

Test the plain_code_encryption(numb) Function

In this step, you will test the plain_code_encryption(numb) function with the provided examples.

  1. Open the plain_code.py file in your code editor.
  2. Add the following code at the end of the file:
if __name__ == "__main__":
    print(plain_code_encryption(1530))  ## Output: "0383"
    print(plain_code_encryption(0))     ## Output: "9853"
    print(plain_code_encryption(12345)) ## Output: None
  1. Save the file and run the script using the command python3 plain_code.py.
  2. Verify that the output matches the expected results.

Handle User Input

In this step, you will modify the script to accept user input and call the plain_code_encryption(numb) function with the user-provided number.

  1. Open the plain_code.py file in your code editor.
  2. Locate the code you added in the previous step and replace it with the following:
if __name__ == "__main__":
    txt = int(input())
    print(plain_code_encryption(txt))
  1. Save the file and run the script using the command python3 plain_code.py.
  2. When prompted, enter a number between 0 and 9999, and the script will print the encrypted text.

Congratulations! You have completed the "Clear Code Encryption" project. You have learned how to implement the encryption method and test it with various inputs.

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