The Playfair cipher is a substitution cipher that uses digraphs (pairs of letters) instead of single letters, and makes use of symmetrical encryption.
There are some variations on the rules of encipherment. One version of the cipher rules is outlined below:
keyword = pythonFirst, fill in the keyword at the top of the grid, omitting any duplicates (3rd example's keyword has two occurrences of "h". The second one should be omitted).
| P | Y | T | H | O |
| N | ||||
Next, fill in the rest of the slots with the leftover letters of the alphabet that are not in the keyword. Classically, "I" and "J" share a slot (some people elect to omit "Q", "V", or "Z" instead).
| P | Y | T | H | O |
| N | A | B | C | D |
| E | F | G | I/J | K |
| L | M | Q | R | S |
| U | V | W | X | Z |
message = "Tomorrow we attack."
message = "tomorrowweattack"
message = "to mo rr ow we at ta ck"message = "to mo rx ro ww ea tt ac k"Note how shifting the pairings rightward after inserting an 'x' after the first 'r' has created two more double letter digraphs. Correct them sequentially.
message = "to mo rx ro wx we at ta ck"This insertion corrected both double letter digraphs, and evened out the message length. No more changes are necessary.
to -> hprx -> xhIf the two letters have dissimilar rows and columns, as in the second digraph "mo", visualise a rectangle with these letters at opposite vertices, then find the other two vertices. Replace each letter with the vertex sharing its row. The original letters are rendered in bold below. The cipher letters are italicised and bolded.
| P | Y | T | H | O |
| N | A | B | C | D |
| E | F | G | I/J | K |
| L | M | Q | R | S |
| U | V | W | X | Z |
mo -> sy
"to mo rx ro wx we at ta ck" -> "hp sy xh sh xz ug by yb di"Create a function that takes two strings — a plaintext message or ciphertext str, and a keyword — and returns the corresponding ciphertext or deciphered plaintext (without spaces and with "x" in odd places as appropriate).
playfair("Tomorrow we attack.", "python") ➞ "HPSYXHSHXZUGBYYBDI"
playfair("HPSYXHSHXZUGBYYBDI", "python") ➞ "TOMORXROWXWEATTACK"
playfair("MYDZABIFBMENFEQAAE", "rhythm") ➞ "THEXEAGLEHASLANDED"Check the Resources tab if the explanations here are unclear.