Wednesday 10 March 2021

The Playfair cipher


Ciphers are an indispensable tool for mystery, detective, and crime writers.

Most of the ciphers we've talked about here are monoalphabetic ciphers, ciphers where one letter maps to exactly one other letter (The one exception being the Vigenere cipher).

A major problem with monoalphabetic ciphers is that they are very easy to break, especially if you have a lot of text to work with. So, if you have a few short sentences (like in the adventure of the dancing men), it can be quite challenging to the detective, but if the message is a few paragraphs, your detective can probably break it in their sleep.

 With polyalphabetic ciphers, one letter maps to more than one other letter. What this does is it hides the distribution of letters in the text, making statistical analysis difficult (not impossible) and it hides common, short words that give the cipher away, such as ‘the’, ‘a’, ‘on’ ‘in’, ‘is’ and so on. This makes the cipher significantly harder to crack. Of course, a lot of these can be broken by a computer bruteforcing it given enough time, but again, a lot of people aren’t very clear on how to get a computer to bruteforce something.

The Playfair cipher

This is a cipher that has been used for military applications for real in world war 1 and world war II. Of course, with modern computers breaking it in a fraction of a second, it is no longer secure enough for that purpose.

What's interesting about this cipher is that it encrypts pairs of letters, or bigrams rather than single letters. This means, instead of trying to find statistical data for the usual 26 letters, you’d be looking for statistical data for 600 combinations of letters. This makes it impossible to break without a considerable amount of text to start with.

At the heart of the Playfair cipher is a five by five square grid. You’re going to need a key for this cipher to work, so let’s go with cipher because why not? The first thing to do is to fill the grid with the cipher. If there are any repeated letters in your keyword or phrase, skip them the second or third time they appear.



Once that is done, fill the table with the rest of the letters of the alphabet. There are only 25 places in the grid, so you can write only 25 letters. Some people drop Q, or some such uncommon letter, and let the person who deciphers the cipher figure out that the letter is missing (or you can communicate that to them which is probably the better option). I prefer to combine I and J, because that’s what was done with the Latin alphabet in the early days. 

Incidentally, that is why you have 600 pairs – the combinations possible are non-repeated letter pairs as you will see, so you will have 25 choices in your first choice and 24 the second time. This gives 25x24 combinations.


Ciphering

To see how the cipher works, let’s take the following phrase: hello world.

The first thing to do would be to break it into pairs of letters, and separate any double letters. You can use X for the purpose. You can also use it to form a bigram for any letters that are left over like D in this case. You can also use any letter that isn’t common in repeated pairs, like Q, instead of X.

HE LX LO WO RL DX

Now, we cipher it bigram by bigram. The first bigram is HE. Both are on the first row. When we’re dealing with a single row, we replace each letter with the one to the right, wrapping left. That gives EC.

The next pair, LX, is on the same column. This is similar to the last one, as in, we replace each letter with the one below it, again, wrapping up if we have to. Here, that gives us SP.

The next pair is LO. Here, the two are one different rows and columns. In that case, we form a rectangle with the two letters, and each letter is replaced by the letter in the same row as it and in the same column as the other in the pair. This gives GS.


WO is another rectangle, so we follow the same procedure as the previous one: same row, opposite column. That gives VQ (try is out yourself, or look at the video to see how it's done).

RL is another rectangle, using the same process, we get BG.

DX is another rectangle, and again we using the same process to get BY.

So our ciphered message is EC SP GS VQ BG BY.

Deciphering

Once you've ciphered the message, someone has to decipher it in order to read it. To decipher it, we do the same in reverse.

Our first pair is EC.  They’re on the same row, so we replace each letter by the one to left, wrapping to the right if necessary. That gives HE.

The second pair, SP is on the same column, so replace each letter by the one above it, wrapping down if necessary. That gives LX.

The third, GS, is a rectangle, so it’s same column opposite row again. That gives LO.

The fourth, VQ is another rectangle, and with the same process, we get WO.

BG is another rectangle, and again we get RL.

BY is another rectangle, and deciphering gives DX.

The deciphered message is 'HE LX LO WO RL DX'. You can get rid of the X's, and rearrange the spacing, in order to get our original message, ‘HELLO WORLD’.

That is the basics of how the Playfair cipher works. Have fun with it (Maybe try different keys, longer keyphrases, and try enciphering paragraphs for practice). 

That’s it on the playfair cipher for now. If you have any questions of comments, well, mention it in the comments below.

You can follow me on Facebook here or on YouTube here.

See you next time!


1 comment:

  1. මේ වන විටත් ඔබගේ බ්ලොගය අප අපගේ සින්ඩියට ඇතුලත් කොට හමාරය. එය "7. බර කතා බහ" කොලමයේ දක්වා ඇත! අපට අවශ්‍ය වන්නේ බ්ලොග් ස්වර්ණමය යුගයක් යලිත් ඇතිකිරීමටය. ඒ සඳහා ඔබගේ සහයෝගයද අප නිරන්තරයෙන් බලා පොරොත්තු වෙමු. අප පිළිබඳව හැකි උපරිම ප්‍රචාරණයක් ලබා දෙන්න!

    ReplyDelete

How to write a character who is smarter than you

We all have that one character (or few) who is significantly smarter than the writer. So, as a writer, how do you write such a character con...