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!


Monday, 1 March 2021

Colombo needs more trees

I had to visit the Colombo city area some time back. That involved a bit of walking, and parking the car in some rather, well, sunny places. I think I should mention the car had a bit of an AC issue. By some miracle, I actually got home without too much of a heat headache, but it got me thinking.

Colombo seriously needs more trees. Or solar panels. Or more trees.

Please hear me out.

Colombo is situated in the wet zone. Its average annual rainfall is 2348 mm according to a quick google search. The daytime temperature seems to hover at 30-32 Celsius. It receives a ton of sunlight. That is basically plant heaven.

There are a few streets with a full canopy overhead, and in those places, well, it's actually quite pleasant. However, when you look at most of the streets, there is no significant tree cover. 

I mean, I can see people have tried and there are juvenile trees in places - I understand they take years to grow. Sure, it'll take time for those trees to provide any meaningful level of shade, but when they do, it'll be so worth it. 

But, in some places, you don't see that, either. There's the street and there are buildings, and the sun just grills the road surface which then blinds everyone around, roasts the pedestrians, and barbeques the vehicles from below.

My suggestion is, if it's possible to grow trees, do grow them. They look lovely and help cool the streets. I recognize the problems with this though - not every street has enough space between the tarmac and the buildings for a large tree. There is electrical wiring and they could get in the way of trees in some places. 

I'm not saying they have grown trees in every possible location - they haven't. 

Well, a possible solution is a canopy of vines. There are some vines you could use as a sort of shade tree that uses a bit less land area for roots and stuff. A bit of a problem with this is that it'll make a road a veritable tunnel. It would be better than a true tunnel of course - mottled sunlight would come in like with a tree cover - but some people may find it a bit claustrophobic. Another thing is maintenance - it could become more difficult than trimming off a few branches, and if the netting overhead is too dense, the dead plant matter (leaves and such) could pile up on top of the net, making it a true tunnel, and a stinky one at that. 

Still, I think this is a viable solution for some places.

If you think it will be a problem for streetlights, well, you could use those poles as part of the structure and put the vine canopy over them.

But I recognize that some people might not like this approach, but well, there is another solution. Solar panels.

Sure, they wouldn't be quite as shading, but there is a lot of solar energy falling on Sri Lanka in general that is underutilized. Sure, it's not available when it's dark, but we're in the tropics and pretty close to the equator at that, which means the length of the day doesn't vary by much. It's not like you have to account for twenty hours of darkness on some days. 

According to linked source, Colombo receives somewhere between 2.5 - 4.0 kWh/m^2 of solar energy per day depending on the season. When travelling in Colombo, it feels like a lot of that is used to heat the tar and mess up with car AC systems for some reason. It's a lot of energy. Solar panels over some areas of road can be used to capture some of that.

This is especially true for highways, especially in areas where the view isn't too great. There is a maximum possible height on highways anyway due to all the roads going over them, so installing solar panels over at least some sections can help. It won't be a tunnel if you install it in short sections about the width of a road crossing overhead. That'll be a lot of electricity we can use, and people won't be frying in their cars.

This is just my opinion on what I think should be done, as someone who is decidedly not good at dealing with sunlight or heat in large doses. In addition to reducing the severity those problems, you get electricity and/or a pleasant atmosphere as a bonus.

Do you agree with me, or do you see any issues with this approach? I'd love to have a discussion on this topic, so please comment below.

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

See you next time!

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...