Java Program to Implement Playfair Cipher Algorithm

Cipher is an algorithm for encryption and decryption. The cipher text is a process that applies to different types of algorithms to convert plain text to coded text. It is referred to as ciphertext. The Playfair cipher was the first practical digraph substitution cipher. The scheme was invented in 1854 by Charles Wheatstone but was named after Lord Playfair who promoted the use of the cipher. In Playfair cipher unlike traditional cipher, we encrypt a pair of alphabets(digraphs) instead of a single alphabet. It was used for tactical purposes by British forces in the Second Boer War and in World War I and for the same purpose by the Australians during World War II. This was because Playfair is reasonably fast to use and requires no special equipment.
Algorithm:
- Create a matrix of 5 cross 5 is made in which all the alphabet of English letters is placed in it. Now, you must be wondering that there are 26 alphabets while the matrix is only having 25 cells. To resolve it alphabets āiā and ājā are placed into a single cell.
- Now insert the key and put the remaining alphabets in the matrix. The matrix is made by inserting the value of the key and remaining alphabets into the matrix row-wise from left to right.
- Convert the text into pairs of alphabets keeping in mind no two alphabets should repeat consecutively. For example: ācodeā is written as ācoā,ādeā
- If the letter is repeating then add āxā to make as many pair sets as many times the alphabet is repeating. For example: āhellohā is written as āheā ālxā, ālxā, āohā. Here letter ālā was consecutive for 2 times hence two sets and two additions of āxās
- Now if after breakdown into pairs, a letter is left alone add āzā to the letter just like we have added āxā. For example: āhelloā is written as āheā ālxā, ālxā, āozā
- Solve the matrix or forming code using 3 standard rules
- If both the alphabet are in the same row, replace them with alphabets to their immediate right.
- If both the alphabets are in the same column, replace them with alphabets immediately below them.
- If not in the same row or column, replace them with alphabets in the same row respectively, but at other pair of corners
Illustration:
| c | o | d | e | u |
| f | s | t | k | g |
| l | r | m | n | p |
| e | i/j | a | h | b |
| v | w | x | y | zĀ |
Implementation:
- Generate the key Square(5Ć5)
- Encrypt the Plaintext
Example
Java
// Java Program for Playfair Cipher AlgorithmĀ
// Importing all utility classesimport java.util.*;Ā
// Main classpublic class Main {Ā
Ā Ā Ā Ā // Removing the duplicate values from the keyĀ Ā Ā Ā static String removeDuplicate(String s)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā int j, index = 0, len = s.length();Ā
Ā Ā Ā Ā Ā Ā Ā Ā char c[] = s.toCharArray();Ā
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < len; i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (j = 0; j < i; j++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (c[i] == c[j])Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == j)Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c[index++] = c[i];Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā s = new String((Arrays.copyOf(c, index)));Ā
Ā Ā Ā Ā Ā Ā Ā Ā return s;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Method 1Ā Ā Ā Ā // Removing the white spaces from string 'st'Ā Ā Ā Ā // which was replaced by the key as space.Ā Ā Ā Ā static String removeWhiteSpace(char[] ch, String key)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā char[] c = key.toCharArray();Ā
Ā Ā Ā Ā Ā Ā Ā Ā // removing character which are input by the userĀ Ā Ā Ā Ā Ā Ā Ā // from string stĀ
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < c.length; i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < ch.length; j++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (c[i] == ch[j])Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c[i] = ' ';Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā key = new String(c);Ā
Ā Ā Ā Ā Ā Ā Ā Ā key = key.replaceAll(" ", "");Ā
Ā Ā Ā Ā Ā Ā Ā Ā return key;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Method 2Ā Ā Ā Ā // To make the pair for encryption in plaintext.Ā Ā Ā Ā static String makePair(String pt)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā String s = "";Ā
Ā Ā Ā Ā Ā Ā Ā Ā char c = 'a';Ā
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < pt.length(); i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (pt.charAt(i) == ' ')Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c = pt.charAt(i);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā s += pt.charAt(i);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i < pt.length() - 1)Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (pt.charAt(i) == pt.charAt(i + 1))Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā s += "x";Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If plain text length is odd thenĀ Ā Ā Ā Ā Ā Ā Ā // adding x to make length even.Ā Ā Ā Ā Ā Ā Ā Ā if (s.length() % 2 != 0)Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā s += "x";Ā
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(s);Ā
Ā Ā Ā Ā Ā Ā Ā Ā return s;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Method 3Ā Ā Ā Ā // To find the position of row and column in matrixĀ Ā Ā Ā // for encryption of the pair.Ā Ā Ā Ā static int[] findIJ(char a, char b, char x[][])Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā int[] y = new int[4];Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (a == 'j')Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā a = 'i';Ā
Ā Ā Ā Ā Ā Ā Ā Ā else if (b == 'j')Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā b = 'i';Ā
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < 5; i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < 5; j++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (x[i][j] == a) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y[0] = i;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y[1] = j;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (x[i][j] == b) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y[2] = i;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y[3] = j;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (y[0] == y[2]) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y[1] += 1;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y[3] += 1;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā else if (y[1] == y[3]) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y[0] += 1;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y[2] += 1;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < 4; i++)Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y[i] %= 5;Ā
Ā Ā Ā Ā Ā Ā Ā Ā return y;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Method 4Ā Ā Ā Ā // To encrypt the plaintextĀ Ā Ā Ā static String encrypt(String pt, char x[][])Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā char ch[] = pt.toCharArray();Ā
Ā Ā Ā Ā Ā Ā Ā Ā int a[] = new int[4];Ā
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < pt.length(); i += 2) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i < pt.length() - 1) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā a = findIJ(pt.charAt(i), pt.charAt(i + 1),Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a[0] == a[2]) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ch[i] = x[a[0]][a[1]];Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ch[i + 1] = x[a[0]][a[3]];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (a[1] == a[3]) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ch[i] = x[a[0]][a[1]];Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ch[i + 1] = x[a[2]][a[1]];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ch[i] = x[a[0]][a[3]];Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ch[i + 1] = x[a[2]][a[1]];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā pt = new String(ch);Ā
Ā Ā Ā Ā Ā Ā Ā Ā return pt;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Method 5Ā Ā Ā Ā // Main driver methodĀ Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Creating an Scanner class object toĀ Ā Ā Ā Ā Ā Ā Ā // take input from userĀ Ā Ā Ā Ā Ā Ā Ā Scanner sc = new Scanner(System.in);Ā
Ā Ā Ā Ā Ā Ā Ā Ā String pt = "instruments";Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Key inputĀ Ā Ā Ā Ā Ā Ā Ā String key = "monarchy";Ā
Ā Ā Ā Ā Ā Ā Ā Ā key = removeDuplicate(key);Ā
Ā Ā Ā Ā Ā Ā Ā Ā char[] ch = key.toCharArray();Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Reading string array of Letters of englishĀ Ā Ā Ā Ā Ā Ā Ā // alphabet as Playfair to implementĀ Ā Ā Ā Ā Ā Ā Ā String st = "abcdefghiklmnopqrstuvwxyz";Ā
Ā Ā Ā Ā Ā Ā Ā Ā st = removeWhiteSpace(ch, st);Ā
Ā Ā Ā Ā Ā Ā Ā Ā char[] c = st.toCharArray();Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Matrix input using above keyĀ Ā Ā Ā Ā Ā Ā Ā char[][] x = new char[5][5];Ā
Ā Ā Ā Ā Ā Ā Ā Ā int indexOfSt = 0, indexOfKey = 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < 5; i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < 5; j++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (indexOfKey < key.length())Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x[i][j] = ch[indexOfKey++];Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elseĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā x[i][j] = c[indexOfSt++];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Printing MatrixĀ
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < 5; i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < 5; j++)Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.print(x[i][j] + " ");Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println();Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // For getting encrypted outputĀ
Ā Ā Ā Ā Ā Ā Ā Ā // Calling makePair() method over object created inĀ Ā Ā Ā Ā Ā Ā Ā // main()Ā Ā Ā Ā Ā Ā Ā Ā pt = makePair(pt);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calling makePair() method over object created inĀ Ā Ā Ā Ā Ā Ā Ā // main()Ā Ā Ā Ā Ā Ā Ā Ā pt = encrypt(pt, x);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Print and display in the consoleĀ Ā Ā Ā Ā Ā Ā Ā System.out.println(pt);Ā Ā Ā Ā }} |
m o n a r c h y b d e f g i k l p q s t u v w x z instrumentsx gatlmzclrqxa
Ā



