Java Program to Encrypt 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 Enode a Message 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 PlayfairCipherEncryption
  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 static void main(String[] args)
  188.     {
  189.         PlayfairCipherEncryption x = new PlayfairCipherEncryption();
  190.         Scanner sc = new Scanner(System.in);
  191.         System.out.println("Enter a keyword:");
  192.         String keyword = sc.next();
  193.         x.setKey(keyword);
  194.         x.KeyGen();
  195.         System.out
  196.                 .println("Enter word to encrypt: (Make sure length of message is even)");
  197.         String key_input = sc.next();
  198.         if (key_input.length() % 2 == 0)
  199.         {
  200.             System.out.println("Encryption: " + x.encryptMessage(key_input));
  201.         }
  202.         else
  203.         {
  204.             System.out.println("Message length should be even");
  205.         }
  206.         sc.close();
  207.     }
  208. }

Output:

$ javac PlayfairCipherEncryption.java
$ java PlayfairCipherEncryption
 
Enter a keyword:
Sanfoundry
Sanfoudrybceghiklmpqstvwxz
S a n f o 
u d r y b 
c e g h i 
k l m p q 
s t v w x 
Enter word to encrypt: (Make sure length of message is even)
Learningcenter
Encryption: acndogrmegavgd

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 & technical discussions at Telegram SanfoundryClasses.