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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
- 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
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
- Generate the key Square(5×5)
- Encrypt the Plaintext
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