Friday, October 4, 2013

Encryption 101: The Playfair Cypher

Jack Hammond
Junior Developer
Egress Software Technologies Ltd.
Films such as Disney’s National Treasure would have us believe that encryption is confined to professors and academics, or that it is the stomping ground of computer whizzes who sit staring at a screen, watching line upon line of code stream past them until a big message flashes up saying that they have ‘cracked the code’.

In reality, encryption is a fascinating art that spans thousands of years and has the addictive property that the more you learn, the more you want to apply it.

Given that I opened this post mentioning the film National Treasure, I think it’s only apt that we start with the cipher that was made famous in the film: the Playfair Cipher, which was created by Charles Wheatstone in approximately 1854.

Grasping the encryption basics

The fundamentals of the Playfair cipher are fairly straight forward:

  • Pick a keyword that does not have any repeated letters
  • Draw a 5x5 square
  • Write the keyword along the top, moving on to the second line if you need to
  • With that done, add in the rest of the alphabet, skipping the letters that are already in the keyword, and putting I and J in the same cell (since there are 25 cells but 26 letters, it is common practise to place these two letters together)
So for example, if I set the keyword as ‘KEYWORD’, then my completed square would look like this:

With our square laid out, now we can begin the fun part of encrypting a message of our choosing. Just as with constructing our square, there are a few steps that need to be taken to prepare the string of text for encryption. For the purpose of this exercise, I’ll be using the following phrase:
‘The lazy hippo likes to burp’

  • Divide the phrase up into pairs of letters;
    • Th el az yh ip po li ke st ob ur px
    • As you can see here, I’ve added an ‘x’ to the end of the phrase, which is done if there’s an odd number of letters or the same letter forming one pair (so ‘tree’ would become ‘tr ex ex’)
  • Next we take each pair in turn, and depending on the location of the letters in the square, we do one of several things: (original letters are highlighted in purple, and blue ones are encrypted letters)
    •  If the letters are in the same column: Take the letter immediately below the source letter. If the letter is on the very bottom row, just wrap round and continue down from the top row

'YH' is encrypted to 'AP'

  • If they are in the same row: Take the letter immediately to the right of the source letter. If the letter is in the very right-hand column, just wrap round and continue on from the left-hand column.

'IL' is encrypted to 'LF'

  • If they are in different columns or rows: Draw a square round the two letters and then take the letters that are in the opposite corners but still on the same row as the original letter

'TH' is encrypted to 'VF'

Putting this together

So now that we know the basics of the Playfair cipher, we should be able to encrypt our original secret message to:


As you can see, the encrypted text hides our original message quite well; however there are numerous drawbacks to the Playfair cipher, namely to do with repeated letters. The fact that the key word can’t contain repeated letters drastically shrinks the size of the key pool that can be used with this cipher, which of course impacts how long it would take a computer to crack this code (at today’s standards, this is usually several seconds up to a few minutes).

All in all, however, the Playfair cipher remains an early testament to the importance of encrypting sensitive information.

Your turn to crack the code (try these at your desk!)

  1. Using the keyword of ‘software’, encrypt the phrase below:
    • The City of London
    • (Answer: fi ba pb vt sn fm gs xf)
  2. Using the keyword of ‘horse’, encrypt the phrase below:
    • They’re coming from the east
    • (Answer: Ns sz sh br gk vn ce ei ns rz hf dy)
  3. Using the keyword ‘encrypt’, decrypt the secret code below;
    • Ao bc up oz fc ba md mc ru
    • (Answer: Charles Wheatstone)

No comments:

Post a Comment