In: Computer Science
Keyword Columnar Transposition cipher writes plain text in rows and reads ciphertext in columns one by one.
For decryption of the transposition cipher, the following algorithm is used-
For simplicity and inclusive function, we assume all the parameters such as the plainText 2D array, cipherText array, message, and the key are passed as parameters to our decrypt function.
public int[][] decrypt(int cipherText[][], String message, String key)
{
int i,j;
int k=0;
int rowLength = cipherText.length
int colLength = cipherText[0].length
int[][] plainText = new int[rowLength][colLength]
for(i=0; i<colLength; i++)
{
//Find the current column
int curCol= ((int)key.charAt(i)-48)-1;
for(j=0; j<rowLength; j++)
{
plainText[j][curCol] = cipherText[j][i];
}
}
return plainText;
}
Explanation -
This function decrypts the encrypted message which is passed by using cipherText array. The outer and inner loop use number of columns and number of rows in the inner loop, as the message has to be written in form of columns.
The plainText is returned in form of a 2D array which can be traversed row-wise, to get the decrypted message. The code for that would be-
String decrypted = "";
for(int i=0;i<rowLength;i++)
for(int j=0;j<colLength;j++)
decrypted += plainText[i][j]
System.out.println(decrypted);
Code Screenshot -