Encryption and Decryption of String according to given technique | Function to encrypt the let ; Matrix to generate the Encrypted let ; Fill the matrix row - wise ; Loop to generate encrypted let ; Function to decrypt the let ; Matrix to generate the Encrypted let ; Fill the matrix column - wise ; Loop to generate decrypted let ; Driver Code ; Encryption of let ; Decryption of let
function encryption ( s ) { let l = s . length ; let b = Math . ceil ( Math . sqrt ( l ) ) ; let a = Math . floor ( Math . sqrt ( l ) ) ; let encrypted = ' ' ; if ( b * a < l ) { if ( Math . min ( b , a ) == b ) { b = b + 1 ; } else { a = a + 1 ; } } let arr = new Array ( ) ; for ( let i = 0 ; i < a ; i ++ ) { let temp = [ ] ; for ( let j = 0 ; j < b ; j ++ ) { temp . push ( [ ] ) } arr . push ( temp ) } for ( let i = 0 ; i < a ; i ++ ) { for ( let j = 0 ; j < b ; j ++ ) { arr [ i ] [ j ] = " " } } let k = 0 ; for ( let j = 0 ; j < a ; j ++ ) { for ( let i = 0 ; i < b ; i ++ ) { if ( k < l ) { arr [ j ] [ i ] = s [ k ] ; } k ++ ; } } for ( let j = 0 ; j < b ; j ++ ) { for ( let i = 0 ; i < a ; i ++ ) { encrypted = encrypted + arr [ i ] [ j ] ; } } return encrypted ; } function decryption ( s ) { let l = s . length ; let b = Math . ceil ( Math . sqrt ( l ) ) ; let a = Math . floor ( Math . sqrt ( l ) ) ; let decrypted = ' ' ; let arr = new Array ( ) ; for ( let i = 0 ; i < a ; i ++ ) { let temp = [ ] ; for ( let j = 0 ; j < b ; j ++ ) { temp . push ( [ ] ) } arr . push ( temp ) } for ( let i = 0 ; i < a ; i ++ ) { for ( let j = 0 ; j < b ; j ++ ) { arr [ i ] [ j ] = " " } } let k = 0 ; for ( let j = 0 ; j < b ; j ++ ) { for ( let i = 0 ; i < a ; i ++ ) { if ( k < l ) { arr [ j ] [ i ] = s [ k ] ; } k ++ ; } } for ( let j = 0 ; j < a ; j ++ ) { for ( let i = 0 ; i < b ; i ++ ) { decrypted = decrypted + arr [ i ] [ j ] ; } } return decrypted ; } let s = " " ; let encrypted ; let decrypted ; encrypted = encryption ( s ) ; document . write ( encrypted + " " ) ; decrypted = decryption ( encrypted ) ; document . write ( decrypted ) ;