If you can read this, you might want to skip straight to the content. Also, kindly take a moment to read my rant about Web design—especially if you’re wondering why this site looks a bit…dull.

The Playfair Cipher

by Ben Goren

Introduction

Although the Baron Playfair’s name is attached to one of the better-known classical ciphers, the baron’s friend, scientist Charles Wheatstone, actually devised the Playfair cipher. After its creation in 1854, the baron succesfully lobbied the Brittish government to adopt the cipher for official use, and thus got his name, and not Wheatstone’s, attached to the cipher.

The Playfair is a primitive—by modern reckoning—block cipher. Any new personal computer sold today can break a message encoded with it in a matter of seconds. That is, with the proper software, you could use such a computer to discover the original text without knowing the cipher key. Some skilled cryptogrophists and puzzle experts can even break it with nothing more than pen and paper.

Nonetheless, it uses some principles common to modern computer block ciphers. Understanding the Playfair will give you a beginning insight into modern cryptography—without all the complex mathematics and number theory.

Preparatory Information

To encode a message, you start by preparing your message, known as plaintext, in a specific way, and then transform form this prepared plaintext based upon an alphabet square—again, in a specific way. The alphabet square varies with your chosen keyword.

Decryption is essentially the reverse process.

For the purpose of this example, consider the following as the plaintext:

Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances.

The words, "First Amendment," will be used as the encryption key.

Preparing the Plaintext

The first step is to prepaire the plaintext. To do so, all letters are written uppercase, in pairs, and without punctuation. All Js are replaced with Is. This particular example contains no Js.

CO NG RE SS SH AL LM AK
EN OL AW RE SP EC TI NG
AN ES TA BL IS HM EN TO
FR EL IG IO NO RP RO HI
BI TI NG TH EF RE EE EX
ER CI SE TH ER EO FO RA
BR ID GI NG TH EF RE ED
OM OF SP EE CH OR OF TH
EP RE SS OR TH ER IG HT
OF TH EP EO PL EP EA CE
AB LY TO AS SE MB LE AN
DT OP ET IT IO NT HG OV
ER NM EN TF OR AR ED RE
SS OF GR IE VA NC ES

Next, double letters—if they occur in a pair—must be divided by an X or a Z. For example, freedom in this example becomes FREXEDOM. Because of the triple s between the first and second words, they are written as CONGRESXSZ SHALL. This rule for double letters reduces the number of visible patterns in the ciphertext.

Finally, if there are an odd number of letters, an extra letter chosen by the person writing the cipher is added to the end.

In its ready-to-encrypt form, the plaintext becomes:

CO NG RE SX SZ SH AL LM
MA KE NO LA WR ES PE CT
IN GA NE ST AB LI SH ME
NT OF RE LI GI ON OR PR
OH IB IT IN GT FR EX EZ
EX ER CI SE TH ER EO FO
RA BR RI DG IN GT HE FR
EX ED OM OF SP EX EC HO
RO FT HE PR ES SO RT HE
RI GH TO FT HE PE OP LE
PE AC EA BL YT OA SX SE
MB LE AN DT OP ET IT IO
NT HE GO VE RN ME NT FO
RA RE DR ES SO FG RI EV
AN CE SB

Notice that, for example, the seventh-from-the-end pair of Ss were not replaced with SXS; this is because previous modifications of double letters shifted everything in a way that they are no longer adjacent.

Preparing the Key

The alphabet square is a five-by-five grid. The key phrase is first written without repeating any letters. The remaining letters of the alphabet are filled in in order:

F I R S T
A M E N D
B C G H K
L O P Q U
V W X Y Z

Note the absence of a J, and that the last three leters of the key had already appeared in the previous ten.

The longer your key, the more secure your message will be—but remember that even the most secure Playfair cipher can be broken easily with the proper tools.

Encryption

Next comes the heart of the Playfair cipher: any pair of letters must both be in the same row, in the same column, or in different rows and columns; no other combinations are possible.

Each letter in a pair that is on the same row is replaced by the letter to the right. The letter to the right of the rightmost letter is the first letter in the same row—it "wraps" around without going to the next line. With this key, FI becomes IR; OQ becomes PU; KB becomes BC, and so on.

Similarly, letters in the same coumn are replace by the next letter below in the same column. IM becomes MC, GX becomes PR.

The hard one: when the letters are neither in the same row nor column, the substitution is based upon their intersection. Of great importance is preserving the order. Start with the first letter, and move across until it is lined up with the second letter; then start with the second, and move up or down until it is lined up with the first. GU becomes KP, *NOT* PK. YM becomes WN. DH becomes NK.

Remember: first move across (left or right), and then up or down.

Finally, perform this transformation for each pair of letters in the modified plaintext and remove the spaces:

OWEHEGRYTYNQBVOAEMGDMQVBXINRXGKI
SMBEDNTFBLOFNQENDSLIEGOFCRQMPIXE
QCFCRFSMKRISGRDXGRGEOMRNSKGEMPIL
FEGFSREKSMKRGNISGRNAWCLIRQGRMGCQ
IPIFGNXENRIQSFGNSRHKIUIFGNXGPQPA
XGMBNMLVZSLMRYRNACPAMDKDPQDRRFMW
DSGNCPXASEENDSILFEEGETNRIQRBSRAX
MDGMFH

The above is the final ciphertext. Were Playfair to be a perfect, unbreakable cipher—which it is not—it would be impossible to recover the original message without knowing the encryption key, "First Amendment."

Decryption

To decrypt the message, simply reverse the entire process. Break the ciphertext into pairs of letters:

OW EH EG RY TY NQ BV OA
EM GD MQ VB XI NR XG KI
SM BE DN TF BL OF NQ EN
DS LI EG OF CR QM PI XE
QC FC RF SM KR IS GR DX
GR GE OM RN SK GE MP IL
FE GF SR EK SM KR GN IS
GR NA WC LI RQ GR MG CQ
IP IF GN XE NR IQ SF GN
SR HK IU IF GN XG PQ PA
XG MB NM LV ZS LM RY RN
AC PA MD KD PQ DR RF MW
DS GN CP XA SE EN DS IL
FE EG ET NR IQ RB SR AX
MD GM FH

Write down the alphabet square with the key:

F I R S T
A M E N D
B C G H K
L O P Q U
V W X Y Z

Transform the pairs of letters in the opposite direction from that used for encryption:

CO NG RE SX SZ SH AL LM
MA KE NO LA WR ES PE CT
IN GA NE ST AB LI SH ME
NT OF RE LI GI ON OR PR
OH IB IT IN GT FR EX EZ
EX ER CI SE TH ER EO FO
RA BR RI DG IN GT HE FR
EX ED OM OF SP EX EC HO
RO FT HE PR ES SO RT HE
RI GH TO FT HE PE OP LE
PE AC EA BL YT OA SX SE
MB LE AN DT OP ET IT IO
NT HE GO VE RN ME NT FO
RA RE DR ES SO FG RI EV
AN CE SB

This message is now readable, although removing the extra spaces and substitutions for double letters makes it more readable:

CONGRESS   SHALL  MAKE   NO  LAW
RESPECTING  AN ESTABLISHMENT  OF
RELIGION OR PROHIBITING THE FREE
EXERCISE  THEREOF  OR  ABRIDGING
THE FREEDOM OF  SPEECH OR OF THE
PRESS OR THE RIGHT OF THE PEOPLE
PEACEABLY  TO  ASSEMBLE  AND  TO
PETITION  THE  GOVERNMENT FOR  A
REDRESS OF GRIEVANCES

Modern Comparisons

Computer-run block ciphers work in a manner similar to Playfair’s: they break the original message into blocks of characters and apply a complex mathematical transformation, based upon the key, to each of those blocks.

Naturally, modern ciphers are not restriced to upper-case, no-punctuation, J-less messages. Any form of data that can be stored on a computer can be encrypted with a modern cipher.

A modern block cipher can be run in a mode similar to that of Playfair, where the same block (in Playfair, a pair of letters) always encrypts to the same bit of ciphertext: in our example, CO will always come out as OW. Indeed, many poorly-written encryption programs use just this technique, called Electronic Codebook, or ECB.

More sophisticated implementation of a cipher will use one of many other modes. The most common is called Cipher Feedback Mode, or CFB.

CFB starts by encrypting something other than your message, to get things started. This bit at the front of things is called an initialization vector, or IV. The IV need not be secret, but the same IV should never be re-used with the same encryption key.

First, encrypt the IV. Take the IV and combine it with the first block of the plaintext. With computers, this is done with a mathematical function called a binary XOR; a similar effect could be accomplished with Playfair by "adding" the two together: C + H = K; W + F = B. It is this value which is written to the ciphertext.

Next, take the result from the last step, encrypt it as normally, and add it to the next block from the plaintext. In this way, the encryption of each block depends upon the encryption of each preceeding block.

Encrypt IV -> XOR (add) result with first block of plaintext -> write as ciphertext -> encrypt from previous -> XOR with next block of plaintext -> write as ciphertext -> repeat

The example encoded with Playfair modified in this way, using an IV of "AB" might look thus:

OKHKBGVF…

This process greatly increases the security of the encryption system. When done with computers, the speed of the processing of the encryption is not significantly hindered.


Information in this document about the Playfair cipher comes from the Appendix to Paul Preuss: Arthur C. Clarke’s Venus Prime, Volume 2: Maelstrom. New York, New York: Avon Books, 1988. ISBN 0-380-75345-6.

Information about Electronic Codebook and Cipher Feedback block cipher encryption modes comes from Bruce Schneier: Applied Cryptography: protocols, algorithms, and source code in C. New York, New York: John Wiley & Sons, Inc., 1994. ISBN 0-471-59756-2.