Java Program to Decrypt Message using Playfair Cipher

This is a java program to implement playfair cipher algorithm. The Playfair cipher or Playfair square is a manual symmetric encryption technique and was the first literal digraph substitution cipher.

Here is the source code of the Java Program to Decode a Message Encoded Using Playfair Cipher. 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. import java.util.Scanner;
  5.  
  6. public class PlayfairCipherDecryption
  7. {
  8.     private String KeyWord        = new String();
  9.     private String Key            = new String();
  10.     private char   matrix_arr[][] = new char[5][5];
  11.  
  12.     public void setKey(String k)
  13.     {
  14.         String K_adjust = new String();
  15.         boolean flag = false;
  16.         K_adjust = K_adjust + k.charAt(0);
  17.         for (int i = 1; i < k.length(); i++)
  18.         {
  19.             for (int j = 0; j < K_adjust.length(); j++)
  20.             {
  21.                 if (k.charAt(i) == K_adjust.charAt(j))
  22.                 {
  23.                     flag = true;
  24.                 }
  25.             }
  26.             if (flag == false)
  27.                 K_adjust = K_adjust + k.charAt(i);
  28.             flag = false;
  29.         }
  30.         KeyWord = K_adjust;
  31.     }
  32.  
  33.     public void KeyGen()
  34.     {
  35.         boolean flag = true;
  36.         char current;
  37.         Key = KeyWord;
  38.         for (int i = 0; i < 26; i++)
  39.         {
  40.             current = (char) (i + 97);
  41.             if (current == 'j')
  42.                 continue;
  43.             for (int j = 0; j < KeyWord.length(); j++)
  44.             {
  45.                 if (current == KeyWord.charAt(j))
  46.                 {
  47.                     flag = false;
  48.                     break;
  49.                 }
  50.             }
  51.             if (flag)
  52.                 Key = Key + current;
  53.             flag = true;
  54.         }
  55.         System.out.println(Key);
  56.         matrix();
  57.     }
  58.  
  59.     private void matrix()
  60.     {
  61.         int counter = 0;
  62.         for (int i = 0; i < 5; i++)
  63.         {
  64.             for (int j = 0; j < 5; j++)
  65.             {
  66.                 matrix_arr[i][j] = Key.charAt(counter);
  67.                 System.out.print(matrix_arr[i][j] + " ");
  68.                 counter++;
  69.             }
  70.             System.out.println();
  71.         }
  72.     }
  73.  
  74.     private String format(String old_text)
  75.     {
  76.         int i = 0;
  77.         int len = 0;
  78.         String text = new String();
  79.         len = old_text.length();
  80.         for (int tmp = 0; tmp < len; tmp++)
  81.         {
  82.             if (old_text.charAt(tmp) == 'j')
  83.             {
  84.                 text = text + 'i';
  85.             }
  86.             else
  87.                 text = text + old_text.charAt(tmp);
  88.         }
  89.         len = text.length();
  90.         for (i = 0; i < len; i = i + 2)
  91.         {
  92.             if (text.charAt(i + 1) == text.charAt(i))
  93.             {
  94.                 text = text.substring(0, i + 1) + 'x' + text.substring(i + 1);
  95.             }
  96.         }
  97.         return text;
  98.     }
  99.  
  100.     private String[] Divid2Pairs(String new_string)
  101.     {
  102.         String Original = format(new_string);
  103.         int size = Original.length();
  104.         if (size % 2 != 0)
  105.         {
  106.             size++;
  107.             Original = Original + 'x';
  108.         }
  109.         String x[] = new String[size / 2];
  110.         int counter = 0;
  111.         for (int i = 0; i < size / 2; i++)
  112.         {
  113.             x[i] = Original.substring(counter, counter + 2);
  114.             counter = counter + 2;
  115.         }
  116.         return x;
  117.     }
  118.  
  119.     public int[] GetDiminsions(char letter)
  120.     {
  121.         int[] key = new int[2];
  122.         if (letter == 'j')
  123.             letter = 'i';
  124.         for (int i = 0; i < 5; i++)
  125.         {
  126.             for (int j = 0; j < 5; j++)
  127.             {
  128.                 if (matrix_arr[i][j] == letter)
  129.                 {
  130.                     key[0] = i;
  131.                     key[1] = j;
  132.                     break;
  133.                 }
  134.             }
  135.         }
  136.         return key;
  137.     }
  138.  
  139.     public String encryptMessage(String Source)
  140.     {
  141.         String src_arr[] = Divid2Pairs(Source);
  142.         String Code = new String();
  143.         char one;
  144.         char two;
  145.         int part1[] = new int[2];
  146.         int part2[] = new int[2];
  147.         for (int i = 0; i < src_arr.length; i++)
  148.         {
  149.             one = src_arr[i].charAt(0);
  150.             two = src_arr[i].charAt(1);
  151.             part1 = GetDiminsions(one);
  152.             part2 = GetDiminsions(two);
  153.             if (part1[0] == part2[0])
  154.             {
  155.                 if (part1[1] < 4)
  156.                     part1[1]++;
  157.                 else
  158.                     part1[1] = 0;
  159.                 if (part2[1] < 4)
  160.                     part2[1]++;
  161.                 else
  162.                     part2[1] = 0;
  163.             }
  164.             else if (part1[1] == part2[1])
  165.             {
  166.                 if (part1[0] < 4)
  167.                     part1[0]++;
  168.                 else
  169.                     part1[0] = 0;
  170.                 if (part2[0] < 4)
  171.                     part2[0]++;
  172.                 else
  173.                     part2[0] = 0;
  174.             }
  175.             else
  176.             {
  177.                 int temp = part1[1];
  178.                 part1[1] = part2[1];
  179.                 part2[1] = temp;
  180.             }
  181.             Code = Code + matrix_arr[part1[0]][part1[1]]
  182.                     + matrix_arr[part2[0]][part2[1]];
  183.         }
  184.         return Code;
  185.     }
  186.  
  187.     public String decryptMessage(String Code)
  188.     {
  189.         String Original = new String();
  190.         String src_arr[] = Divid2Pairs(Code);
  191.         char one;
  192.         char two;
  193.         int part1[] = new int[2];
  194.         int part2[] = new int[2];
  195.         for (int i = 0; i < src_arr.length; i++)
  196.         {
  197.             one = src_arr[i].charAt(0);
  198.             two = src_arr[i].charAt(1);
  199.             part1 = GetDiminsions(one);
  200.             part2 = GetDiminsions(two);
  201.             if (part1[0] == part2[0])
  202.             {
  203.                 if (part1[1] > 0)
  204.                     part1[1]--;
  205.                 else
  206.                     part1[1] = 4;
  207.                 if (part2[1] > 0)
  208.                     part2[1]--;
  209.                 else
  210.                     part2[1] = 4;
  211.             }
  212.             else if (part1[1] == part2[1])
  213.             {
  214.                 if (part1[0] > 0)
  215.                     part1[0]--;
  216.                 else
  217.                     part1[0] = 4;
  218.                 if (part2[0] > 0)
  219.                     part2[0]--;
  220.                 else
  221.                     part2[0] = 4;
  222.             }
  223.             else
  224.             {
  225.                 int temp = part1[1];
  226.                 part1[1] = part2[1];
  227.                 part2[1] = temp;
  228.             }
  229.             Original = Original + matrix_arr[part1[0]][part1[1]]
  230.                     + matrix_arr[part2[0]][part2[1]];
  231.         }
  232.         return Original;
  233.     }
  234.  
  235.     public static void main(String[] args)
  236.     {
  237.         PlayfairCipherDecryption x = new PlayfairCipherDecryption();
  238.         Scanner sc = new Scanner(System.in);
  239.         System.out.println("Enter a keyword:");
  240.         String keyword = sc.next();
  241.         x.setKey(keyword);
  242.         x.KeyGen();
  243.         System.out
  244.                 .println("Enter word to encrypt: (Make sure length of message is even)");
  245.         String key_input = sc.next();
  246.         if (key_input.length() % 2 == 0)
  247.         {
  248.             System.out.println("Encryption: " + x.encryptMessage(key_input));
  249.             System.out.println("Decryption: "
  250.                     + x.decryptMessage(x.encryptMessage(key_input)));
  251.         }
  252.         else
  253.         {
  254.             System.out.println("Message length should be even");
  255.         }
  256.         sc.close();
  257.     }
  258. }

Output:

$ javac PlayfairCipherDecryption.java
$ java PlayfairCipherDecryption
 
Enter a keyword:
sanfoundry
sanfoudrybceghiklmpqtvwxz
s a n f o 
u d r y b 
c e g h i 
k l m p q 
t v w x z 
Enter word to encrypt: (Make sure length of message is even)
learning
Encryption: vlndogrm
Decryption: learning

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

If you find any mistake above, kindly email to [email protected]

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.