Transposition Cipher Method

Submitted by Karthikeyan on

Java program to demonstrate Transposition Cipher Method


import java.io.*;
import java.util.*;
class transposition {
   public static void main(String args[])
 {
     String originalKey = "";
             char[] keyArray;
             int [] keyPosition;
             String plainText;
             char[][] plainTextArray;
             int [] invalidCol;
             String cipherText="";
             Scanner console = new Scanner(System.in);
             System.out.println("Enter a key of length 7 chars");;
             originalKey = console.nextLine();
             keyArray = originalKey.toCharArray();
             Arrays.sort(keyArray);
             

             System.out.println("Key Array :");
             int x;
             for (x=0; x<7; x++){
                 System.out.print("["+keyArray[x]+"]");
             }
             System.out.println();
             
             keyPosition = new int[7];
             int i = 0;
             for (char c:keyArray)
             {
                 keyPosition[i++] = originalKey.indexOf(c);
                 
             }
             

             System.out.println("Key Position Array :");
             
             for (x=0; x < 7; x++){
                 System.out.print("["+keyPosition[x]+"]");
             }
   
             System.out.println();
             
             System.out.println("Enter plain text :");
             plainText = console.nextLine();
             char [] text;
             text = plainText.toCharArray();
             
             System.out.println("Plain Text Array :");
             
             for (x=0; x < text.length; x++){
                 System.out.print("["+text[x]+"]");
             }
             System.out.println("\n-----------------");
             
             
             int cols = 7;
             int rows;
             if(text.length % cols == 0)
                 rows = text.length/cols;
             else
                 rows = text.length/cols + 1;
             plainTextArray = new char[rows][cols];
             invalidCol = new int[cols];
             
             for(i=0;i < cols; i++)
                 invalidCol[i] = -1;
             i = 0;
             int k = 0; 
             int c = 0;
             int j = 0;
             while (i < rows)
             {
                 for(j=0; j < cols; j++)
                     if(k < text.length)
                         plainTextArray[i][j] = text[k++];
                 else
                         invalidCol[c++]=j;
                 i++;
                 
             }
             System.out.println("Invalid Column Array :");
             
             
             for(i=0; i < cols; i++)
                 System.out.print("["+invalidCol[i]+"]");

             System.out.println();
             System.out.println("Plain Text Array :");
             System.out.println();
             for (i=0;i < rows;i++)
             {
                 for (j=0;j < cols; j++)
                     System.out.print("["+plainTextArray[i][j]+"]");
                 System.out.println();
                 
             }
   
             int found = 0;
             int colPos;
             for(j=0; j < cols; j++)
             {
                 colPos = keyPosition[j];
                 Arrays.sort((invalidCol));
                 found = Arrays.binarySearch(invalidCol, colPos);
                 if (found < 0)
                 {
                     for(i=0;i < rows;i++)
                         cipherText += plainTextArray[i][colPos];
                     
                 }
                 else
                 {
                     for(i=0;i < rows-1;i++)
                         cipherText += plainTextArray[i][colPos];
                 }
             }
             System.out.println("The encrypted text is \n"+cipherText);
             
 
}
}
Output

run:
Enter a key of length 7 chars
OLYMPIC
Key Array :
[C][I][L][M][O][P][Y]
Key Position Array :
[6][5][1][3][0][4][2]
Enter plain text :
ATTACKPOSTPONEDUNTILTWOAM
Plain Text Array :
[A][T][T][A][C][K][P][O][S][T][P][O][N][E][D][U][N][T][I][L][T][W][O][A][M]
-----------------
Invalid Column Array :
[4][5][6][-1][-1][-1][-1]
Plain Text Array :

[A][T][T][A][C][K][P]
[O][S][T][P][O][N][E]
[D][U][N][T][I][L][L]
[T][W][O][A][M][ ][ ]
The encrypted text is 
PELKNLTSUWAPTAAODTCOIMTTNO