VOOZH about

URL: https://www.geeksforgeeks.org/dsa/encryption-and-decryption-of-string-according-to-given-technique/

⇱ Encryption and Decryption of String according to given technique - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Encryption and Decryption of String according to given technique

Last Updated : 27 Dec, 2022

Given a string S, the task is to encrypt the string and decrypt the string again to the original form. 
Encryption Technique: If L is the length of the string, then take two values, one the ceil of ?L (say b), and the other floor of ?L (say a), and make a two-dimensional matrix having rows = a, and columns = b. 
If rows*columns < L, then increase the value of a or b, whichever is minimum. Fill the matrix with the characters of the original string sequentially. After obtaining the matrix, read the matrix column-wise and print the obtained string. 
 

👁 Image


Decryption Technique: If L is the length of the encrypted string, then again find the two values a and b, where a is the ceil value of the ?L and b is the floor value of the ?L. Similarly create a 2D Matrix in which store the string column-wise and read the matrix row-wise to get the string in the original form. 
 

👁 Image


 


Encryption Approach: 
 

  • Find the length L of the string.
  • Find the ceil and floor values of ?Length and assign them to the variables.
  • Check if the product of the two variables >= Length, if not then increments the variable having a lesser value by 1.
  • Create a 2D matrix and fill the characters of the string row-wise.
  • Read the matrix column-wise to get the encrypted string.


Decryption Approach: 
 

  • Find the length L of the string.
  • Find the ceil and floor values of ?Length and assign them to the variables.
  • Create a 2D matrix and fill the matrix by characters of string column-wise.
  • Read the matrix row-wise to get the decrypted string.


Below is the implementation of the above approach:
 


Output: 
Gsree keFGskoe 
Geeks For Geeks

 

Time Complexity: O(?n * ?n) ? O(n)
Auxiliary Space: O(n), where n is the length of the given string.

Comment
Article Tags:
Article Tags: