The Playfair cipher is one of the traditional ciphers which comes under the category of substitution ciphers. In Playfair Cipher, unlike traditional cipher, we encrypt a pair of alphabets (digraphs) instead of a single alphabet. In Playfair cipher, initially, a key table is created. The key table is a 5×5 matrix consisting of alphabets that acts as the key for encryption of the plaintext. Each of the 25 alphabets must be unique and one letter of the alphabet (usually ‘j’) is omitted from the table, as we need only 25 alphabets instead of 26. If the plaintext contains ‘j’, then it is replaced by ‘i’.
Process for Playfair Cipher:
- The plaintext message is split into pairs of two letters (digraphs). If the plaintext has an odd number of characters, append ‘z’ to the end to make the message of even length.
- Identify any double letters placed side by side in the plaintext and replace the second occurrence with an ‘x’ e.g. ‘hello’ -> ‘he lx lo’.
- Now, locate the letters in the 5×5 key table.
- Use the following rules for encryption of plaintext:
- If the letters appear on the same row of your table, replace them with the letters to their immediate right respectively (wrapping around to the left side of the row if a letter in the original pair was on the right side of the row).
- If the letters appear on the same column of your table, replace them with the letters immediately below respectively (wrapping around to the top side of the column if a letter in the original pair was on the bottom side of the column)
- If the letters are not on the same row or column, replace them with the letters in their own row but in the same column as the other letter.
The decryption process of Playfair cipher is the same encryption process, but it is applied in a reverse manner. The receiver has the same key and can create the same key table, and he uses it to decrypt the ciphertext messages generated using that key.
Example-1 Key: Problem PlainText: Playfair Playfair Cipher Key Matrix: [p, r, o, b, l] [e, m, a, c, d] [f, g, h, i, k] [n, q, s, t, u] [v, w, x, y, z] Cipher Text is: rpcxhegb Example-2 Key: Problem PlainText: Hello Playfair Cipher Key Matrix: [p, r, o, b, l] [e, m, a, c, d] [f, g, h, i, k] [n, q, s, t, u] [v, w, x, y, z] Cipher Text is: faozpb
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.