The Playfair cipher or Playfair square is a manual symmetric encryption technique and was the first literal digraph substitution cipher.
The Playfair cipher uses a 5 by 5 table containing a key word or phrase. Memorization of the keyword and some simple rules was all that was required to create the 5 by 5 table and use the cipher. Since there are 25 cells and 26 letters in English alphabet, treat both "I" and "J" as same character "I" for all purposes.
Any spaces in the key or the message are to be ignored. They are just for understanding. No test cases will contain spaces in the either in key or the message.
Eg. Key - "ACM CQM CODE"
To generate the key table, one would first fill in the spaces in the table with the letters of the keyword (dropping any duplicate letters), then fill the remaining spaces with the rest of the letters of the alphabet in order (treat both "I" and "J" as the same character "I"). The key should be written in the top rows of the table, from left to right.The keyword together with the conventions for filling in the 5 by 5 table constitute the cipher key.
To encrypt a message, one would break the message into digraphs (groups of 2 letters) and map them out on the key table.
If both letters in the pair occupy the same cell (or only one letter is left), add an "X" after the first letter. No message end with a "X".
Eg. Message - "KEEP CALM AND COODE ON"
Digraph - KE EP CA LM AN DC OX OD EO NX
An X is added after first O to prevent the two Os of COODE to form the pair. An X is added at the end to make a pair for the last letter N.
The two letters of the digraph are considered as the opposite corners of a rectangle in the key table. Note the relative position of the corners of this rectangle. Then apply the following rules, in order, to each pair of letters in the plaintext:
1. 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 is the rightmost in the row). (MO will be replaced by QA according to the above cipher key)
2. 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 is the bottom-most in the column). (VD will be replaced by AH according to the above cipher key)
3. If the letters are not on the same row or column, replace them with the letters on the same row respectively but at the other pair of corners of the rectangle defined by the original pair. The order is important – the first letter of the encrypted pair is the one that lies on the same row as the first letter of the plaintext pair. (KE is replaced by IB according to the above cipher key, I in same row as K, B in same row as E)
On replacing, if you find the cell I/J, replace with character I.
Given the key and the message, encrypt the message using Playfair Cipher.
Each line will contain two strings, the first being the key and the second will be the message. Neither the key, nor the message will contain spaces. The key and the message will be composed of only uppercase letters (A-Z). Assume there cannot be two or more consecutive "X" in the message. Input till EOF.
Output one line for each test case which will be the encrypted message using Playfair Encyption Scheme.
1 <= Length(Key) <= 10
1 <= Length(Message) <= 100
Problem Setter : Shikhar Sharad
Problem Tester : Sunny Lalwani