Encryption and Decryption of String according to given technique | C ++ implementation for Custom Encryption and Decryption of String ; Function to encrypt the string ; Matrix to generate the Encrypted String ; Fill the matrix row - wise ; Loop to generate encrypted string ; Function to decrypt the string ; Matrix to generate the Encrypted String ; Fill the matrix column - wise ; Loop to generate decrypted string ; Driver Code ; Encryption of String ; Decryption of String
#include <bits/stdc++.h> NEW_LINE using namespace std ; string encryption ( string s ) { int l = s . length ( ) ; int b = ceil ( sqrt ( l ) ) ; int a = floor ( sqrt ( l ) ) ; string encrypted ; if ( b * a < l ) { if ( min ( b , a ) == b ) { b = b + 1 ; } else { a = a + 1 ; } } char arr [ a ] [ b ] ; memset ( arr , ' ▁ ' , sizeof ( arr ) ) ; int k = 0 ; for ( int j = 0 ; j < a ; j ++ ) { for ( int i = 0 ; i < b ; i ++ ) { if ( k < l ) { arr [ j ] [ i ] = s [ k ] ; } k ++ ; } } for ( int j = 0 ; j < b ; j ++ ) { for ( int i = 0 ; i < a ; i ++ ) { encrypted = encrypted + arr [ i ] [ j ] ; } } return encrypted ; } string decryption ( string s ) { int l = s . length ( ) ; int b = ceil ( sqrt ( l ) ) ; int a = floor ( sqrt ( l ) ) ; string decrypted ; char arr [ a ] [ b ] ; memset ( arr , ' ▁ ' , sizeof ( arr ) ) ; int k = 0 ; for ( int j = 0 ; j < b ; j ++ ) { for ( int i = 0 ; i < a ; i ++ ) { if ( k < l ) { arr [ j ] [ i ] = s [ k ] ; } k ++ ; } } for ( int j = 0 ; j < a ; j ++ ) { for ( int i = 0 ; i < b ; i ++ ) { decrypted = decrypted + arr [ i ] [ j ] ; } } return decrypted ; } int main ( ) { string s = " Geeks ▁ For ▁ Geeks " ; string encrypted ; string decrypted ; encrypted = encryption ( s ) ; cout << encrypted << endl ; decrypted = decryption ( encrypted ) ; cout << decrypted ; return 0 ; }