Text Encryption and Decryption in Python

Princy Prakash
2 min readJul 15, 2023

--

ALGORITHM:

Encrypt Function:

  1. Start the encrypt function with the input parameters text and n.
  2. Check if the text is an empty string or if n is less than or equal to 0. If true, return the original text.
  3. Perform the encryption process n times using a loop:
  • Initialize two empty strings: string and String.
  • Iterate over the characters in text using the range from 0 to the length of text:
  • If the index i is odd (not divisible by 2):
  • Append the character at index i to the string variable.
  • If the index i is even (divisible by 2):
  • Append the character at index i to the String variable.
  • Update the text by concatenating string and String (resulting in the encrypted text for the current iteration).

4. Return the final encrypted text

Decrypt Function

  1. Start the decrypt function with the input parameters text and n.
  2. Check if the text is an empty string or if n is less than or equal to 0. If true, return the original text.
  3. Perform the decryption process n times using a loop:
  • Initialize an empty string variable called text to store the decrypted text for the current iteration.
  • Find the midpoint index (mid) of the input text.
  • Split the text into two halves: the first half from index 0 to mid-1 and the second half from index mid to the end.
  • Iterate over the characters in the second half of the text using the range from 0 to the length of the second half:
  • Append the character at index i from the second half to the text variable.
  • If i is less than the length of the first half, append the character at index i from the first half to the text variable.
  • Update the text with the decrypted text for the current iteration.

4. Return the final decrypted text.

--

--

Princy Prakash
Princy Prakash

Written by Princy Prakash

Hi I Am Princy. I recently discovered my passion for programming and excited to get deeper into the world of technologies also in problem-solving

Responses (1)