Java Program to Perform Cryptography using Transposition Technique

This is a java program to implement transposition technique. In cryptography, a transposition cipher is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext. That is, the order of the units is changed (the plaintext is reordered). Mathematically a bijective function is used on the characters’ positions to encrypt and an inverse function to decrypt.

Here is the source code of the Java Program to Perform Cryptography Using Transposition Technique. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.setandstring;
  3.  
  4. public class TranspositionCipher
  5. {
  6.     public static String selectedKey;
  7.     public static char   sortedKey[];
  8.     public static int    sortedKeyPos[];
  9.  
  10.     // default constructor define the default key
  11.     public TranspositionCipher()
  12.     {
  13.         selectedKey = "megabuck";
  14.         sortedKeyPos = new int[selectedKey.length()];
  15.         sortedKey = selectedKey.toCharArray();
  16.     }
  17.  
  18.     // Parameterized constructor define the custom key
  19.     public TranspositionCipher(String myKey)
  20.     {
  21.         selectedKey = myKey;
  22.         sortedKeyPos = new int[selectedKey.length()];
  23.         sortedKey = selectedKey.toCharArray();
  24.     }
  25.  
  26.     // To reorder data do the sorting on selected key
  27.     public static void doProcessOnKey()
  28.     {
  29.         // Find position of each character in selected key and arrange it on
  30.         // alphabetical order
  31.         int min, i, j;
  32.         char orginalKey[] = selectedKey.toCharArray();
  33.         char temp;
  34.         // First Sort the array of selected key
  35.         for (i = 0; i < selectedKey.length(); i++)
  36.         {
  37.             min = i;
  38.             for (j = i; j < selectedKey.length(); j++)
  39.             {
  40.                 if (sortedKey[min] > sortedKey[j])
  41.                 {
  42.                     min = j;
  43.                 }
  44.             }
  45.             if (min != i)
  46.             {
  47.                 temp = sortedKey[i];
  48.                 sortedKey[i] = sortedKey[min];
  49.                 sortedKey[min] = temp;
  50.             }
  51.         }
  52.         // Fill the position of array according to alphabetical order
  53.         for (i = 0; i < selectedKey.length(); i++)
  54.         {
  55.             for (j = 0; j < selectedKey.length(); j++)
  56.             {
  57.                 if (orginalKey[i] == sortedKey[j])
  58.                     sortedKeyPos[i] = j;
  59.             }
  60.         }
  61.     }
  62.  
  63.     // to encrypt the targeted string
  64.     public static String doEncryption(String plainText)
  65.     {
  66.         int min, i, j;
  67.         char orginalKey[] = selectedKey.toCharArray();
  68.         char temp;
  69.         doProcessOnKey();
  70.         // Generate encrypted message by doing encryption using Transpotion
  71.         // Cipher
  72.         int row = plainText.length() / selectedKey.length();
  73.         int extrabit = plainText.length() % selectedKey.length();
  74.         int exrow = (extrabit == 0) ? 0 : 1;
  75.         int rowtemp = -1, coltemp = -1;
  76.         int totallen = (row + exrow) * selectedKey.length();
  77.         char pmat[][] = new char[(row + exrow)][(selectedKey.length())];
  78.         char encry[] = new char[totallen];
  79.         int tempcnt = -1;
  80.         row = 0;
  81.         for (i = 0; i < totallen; i++)
  82.         {
  83.             coltemp++;
  84.             if (i < plainText.length())
  85.             {
  86.                 if (coltemp == (selectedKey.length()))
  87.                 {
  88.                     row++;
  89.                     coltemp = 0;
  90.                 }
  91.                 pmat[row][coltemp] = plainText.charAt(i);
  92.             }
  93.             else
  94.             { // do the padding ...
  95.                 pmat[row][coltemp] = '*';
  96.             }
  97.         }
  98.         int len = -1, k;
  99.         for (i = 0; i < selectedKey.length(); i++)
  100.         {
  101.             for (k = 0; k < selectedKey.length(); k++)
  102.             {
  103.                 if (i == sortedKeyPos[k])
  104.                 {
  105.                     break;
  106.                 }
  107.             }
  108.             for (j = 0; j <= row; j++)
  109.             {
  110.                 len++;
  111.                 encry[len] = pmat[j][k];
  112.             }
  113.         }
  114.         String p1 = new String(encry);
  115.         return (new String(p1));
  116.     }
  117.  
  118.     // to decrypt the targeted string
  119.     public static String doDecryption(String s)
  120.     {
  121.         int min, i, j, k;
  122.         char key[] = selectedKey.toCharArray();
  123.         char encry[] = s.toCharArray();
  124.         char temp;
  125.         doProcessOnKey();
  126.         // Now generating plain message
  127.         int row = s.length() / selectedKey.length();
  128.         char pmat[][] = new char[row][(selectedKey.length())];
  129.         int tempcnt = -1;
  130.         for (i = 0; i < selectedKey.length(); i++)
  131.         {
  132.             for (k = 0; k < selectedKey.length(); k++)
  133.             {
  134.                 if (i == sortedKeyPos[k])
  135.                 {
  136.                     break;
  137.                 }
  138.             }
  139.             for (j = 0; j < row; j++)
  140.             {
  141.                 tempcnt++;
  142.                 pmat[j][k] = encry[tempcnt];
  143.             }
  144.         }
  145.         // store matrix character in to a single string
  146.         char p1[] = new char[row * selectedKey.length()];
  147.         k = 0;
  148.         for (i = 0; i < row; i++)
  149.         {
  150.             for (j = 0; j < selectedKey.length(); j++)
  151.             {
  152.                 if (pmat[i][j] != '*')
  153.                 {
  154.                     p1[k++] = pmat[i][j];
  155.                 }
  156.             }
  157.         }
  158.         p1[k++] = '\0';
  159.         return (new String(p1));
  160.     }
  161.  
  162.     @SuppressWarnings("static-access")
  163.     public static void main(String[] args)
  164.     {
  165.         TranspositionCipher tc = new TranspositionCipher();
  166.         System.out.println("Encrypted Message is: "
  167.                 + tc.doEncryption("Sanfoundry"));
  168.         System.out.println("Decrypted Message is: "
  169.                 + tc.doDecryption(tc.doEncryption("Sanfoundry")));
  170.     }
  171. }

Output:

$ javac TranspositionCipher.java
$ java TranspositionCipher
 
Encrypted Message is: f*o*n*ayn*d*Sru*
Decrypted Message is: Sanfoundry

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.