Cryptography using the Transposition Technique can be done by using Transposition ciphers which uses the letters of the plaintext message and after that, they permute the order of the letters. The Columnar Transposition Cipher is a form of transposition cipher just like Rail Fence Cipher. Columnar Transposition involves writing the plaintext out in rows, and then reading the ciphertext off in columns one by one.
It should be easy to spot a transposition cipher because the letter frequencies should mimic the usual frequencies for English – high frequencies for a,e, i, n, o r, s, t.  But, cryptanalysis of a transposition cipher might be difficult. The essential technique is anagramming which is rearranging the ciphertext letters to “make sense. The key to the cipher is the pattern of rearrangement. In mathematically terms it can simply be assumed that a bijective function is used on the characters’ positions to encrypt and an inverse function to decrypt.
Illustration:
Example 1
Input : Plaintext : how are you Output : Ciphertext : a e oowu hyrExample 2
Input : Plaintext : you are a champion Output : Ciphertext : h aa ep o nuc i yaorm
Implementation: Given a plain-text message and a numeric key, cipher/de-cipher the given text using Columnar Transposition Cipher. ‘Hello Geek’ is custom input taken as plain text!Â
Example
Java
| // Java Program to Perform Cryptography// using Transposition Technique Â// Importing all classes from// java.util package// Importing input output classesimportjava.io.*;importjava.util.*; Â// Class// For transposition cipherpublicclassGFG { Â    // Member variables of this class    publicstaticString selectedKey;    publicstaticcharsortedKey[];    publicstaticintsortedKeyPos[]; Â    // Constructor 1 of this class    // Default constructor defining the default key    publicGFG()    {        selectedKey = "megabuck";        sortedKeyPos = newint[selectedKey.length()];        sortedKey = selectedKey.toCharArray();    } Â    // Constructor 2 of this class    // Parameterized constructor defining the custom key    publicGFG(String GeeksForGeeks)    {        selectedKey = GeeksForGeeks;        sortedKeyPos = newint[selectedKey.length()];        sortedKey = selectedKey.toCharArray();    } Â    // Method 1 - doProcessOnKey()    // To reorder data do the sorting on selected key    publicstaticvoiddoProcessOnKey()    {        // Find position of each character in selected key        // and arranging it in alphabetical order        intmin, i, j;        charorginalKey[] = selectedKey.toCharArray();        chartemp; Â        // Step 1: Sorting the array of selected key        // using nested for loops        for(i = 0; i < selectedKey.length(); i++) {            min = i;            for(j = i; j < selectedKey.length(); j++) {                if(sortedKey[min] > sortedKey[j]) {                    min = j;                }            } Â            if(min != i) {                temp = sortedKey[i];                sortedKey[i] = sortedKey[min];                sortedKey[min] = temp;            }        } Â        // Step 2: Filling the position of array        // according to alphabetical order        // using nested for loops        for(i = 0; i < selectedKey.length(); i++) {            for(j = 0; j < selectedKey.length(); j++) {                if(orginalKey[i] == sortedKey[j])                    sortedKeyPos[i] = j;            }        }    } Â    // Method 2 - doEncryption()    // To encrypt the targeted string    publicstaticString doEncryption(String plainText)    {        intmin, i, j;        charorginalKey[] = selectedKey.toCharArray();        chartemp;        doProcessOnKey(); Â        // Step 3: Generating the encrypted message by        // doing encryption using Transpotion Cipher        introw = plainText.length() / selectedKey.length();        intextrabit            = plainText.length() % selectedKey.length();        intexrow = (extrabit == 0) ? 0: 1;        introwtemp = -1, coltemp = -1;        inttotallen = (row + exrow) * selectedKey.length();        charpmat[][] = newchar[(row + exrow)]                                [(selectedKey.length())];        charencry[] = newchar[totallen]; Â        inttempcnt = -1;        row = 0; Â        for(i = 0; i < totallen; i++) {            coltemp++;            if(i < plainText.length()) {                if(coltemp == (selectedKey.length())) {                    row++;                    coltemp = 0;                }                pmat[row][coltemp] = plainText.charAt(i);            } Â            else{ Â                // Padding can be added between two                // consecutive alphabets or a group of                // alphabets of the resultant cipher text                pmat[row][coltemp] = '-';            }        } Â        intlen = -1, k; Â        for(i = 0; i < selectedKey.length(); i++) {            for(k = 0; k < selectedKey.length(); k++) {                if(i == sortedKeyPos[k]) {                    break;                }            }            for(j = 0; j <= row; j++) {                len++;                encry[len] = pmat[j][k];            }        } Â        String p1 = newString(encry);        return(newString(p1));    } Â    // Method 3 - doEncryption()    // To decrypt the targeted string    publicstaticString doDecryption(String s)    {        intmin, i, j, k;        charkey[] = selectedKey.toCharArray();        charencry[] = s.toCharArray();        chartemp; Â        doProcessOnKey(); Â        // Step 4: Generating a plain message        introw = s.length();        selectedKey.length();        charpmat[][]            = newchar[row][(selectedKey.length())];        inttempcnt = -1; Â        for(i = 0; i < selectedKey.length(); i++) {            for(k = 0; k < selectedKey.length(); k++) {                if(i == sortedKeyPos[k]) {                    break;                }            } Â            for(j = 0; j < row; j++) {                tempcnt++;                pmat[j][k] = encry[tempcnt];            }        } Â        // Step 5: Storing matrix character in        // to a single string        charp1[] = newchar[row * selectedKey.length()]; Â        k = 0;        for(i = 0; i < row; i++) {            for(j = 0; j < selectedKey.length(); j++) {                if(pmat[i][j] != '*') {                    p1[k++] = pmat[i][j];                }            }        } Â        p1[k++] = '\0';        return(newString(p1));    } Â    @SuppressWarnings("static-access") Â    // Method 4 - main()    // Main driver method    publicstaticvoidmain(String[] args)    {        // Creating object of class in main method        GFG tc = newGFG(); Â        // Printing the ciphere text        // Custom input - Hello Geek        System.out.println("Cipher Text : "                           + tc.doEncryption("Hello Geek"));    }} | 
Cipher Text : l-o-G-ekl-e-He -


 
                                    








