As a child, I was always interested in codes and ciphers. Encrypting stuff so that nobody could read it, and coming up with a secret language with some friends! I think we’ve all done this or at least tried to. And most of us have seen movies where these encrypted messages are sent and received by secret agents, or used by military units during wars. But have you ever wondered how exactly these codes and ciphers work, what the difference between these is, and what makes a code/cipher strong?
Let’s take a look!
Code vs Cipher
If you asked someone what the difference between a code and a cipher is, they would most likely say nothing. However, there is a slight difference between these two. In a code, we convert a word or a sentence to something else, which is usually shorter. Codes are usually useful for saving time when sending messages. For example, we may say that the word “slap” stands for the sentence “please proceed to the next line”. To make meaning out of something written using a code, we would need a codebook. A codebook is simply a mapping of a sentence and the short word that represents that sentence.
Ciphers, on the other hand, deal with individual characters. In a cipher, we convert a letter to another letter or symbol, and by doing this with each character, we make our message unreadable to someone who does not know how the encryption was done. One popular cipher is the Morse Code, which is a cipher, contrary to its name. It converts alphabets to sequences of dots and dashes. In this article, we would be focusing more on ciphers.
Transposition and Substitution Ciphers
Ciphers can broadly be categorized into transposition and substitution ciphers, although we can also create ciphers that are both.
In a transposition cipher, we just change the order of alphabets, while leaving the alphabets themselves unchanged. For example, consider the word “tasoiinrnpsto”. Here, I just took the word “transposition”, and rearranged the alphabets. I took all odd indexed alphabets from the word (t is the 1st, a is the 3rd, and so on) and arranged them first. After I had run out of the entire word, I took all even indexed alphabets and arranged those. In doing this, I did not change the alphabets themselves, but only their order.
Another very common example of a transposition cipher is where a strip of paper is wrapped around a pipe of some diameter, and the message is then written on this strip. When the strip is taken off the pipe, it is unreadable. When your own ally wants to decipher what is written, he wraps it around a pipe of the same diameter.
In a substitution cipher, an alphabet is exchanged with another alphabet or symbol. For example, consider the word “ifmmp”. Here, I just substituted every character from my message by the character that follows it. So an ‘a’ becomes a ‘b’, a ‘b’ becomes a ‘c’ and so on. To decipher the message, we just have to replace every character in “ifmmp” by the character that comes before it. Thus, we get the word “hello”. This is an example of a Caesar cipher, where we shift every letter by a constant number of alphabets.
Decryption of message is always a difficult task. Decryption is important for a party that intercepts a message. So let’s say a military unit uses radio for communication, and we, their enemies, have figured out the frequency they use. We tune our own radios to their frequency, and can hear everything they send to and fro. However, they send their messages in an encrypted manner, and we want to figure out their messages. In such a case, we have to find a way to convert the unreadable text to the original message.
Often, the method of encryption used is common knowledge. For example, I know that the messages are being encrypted using a Caesar cipher, or that the military unit uses some pipes and performs a substitution cipher. What’s most interesting is that even with this knowledge, it is not easy to decipher messages. The reason being the lack of knowledge of the key. A key is an important component to deciphering a message. In case of a Caesar cipher, the key would be the number of alphabets by which we shift our letters. In the pipe-based transposition cipher, the key would be the diameter of the pipe used. Without a pipe of the correct diameter, decryption attempts would be futile.
However, in today’s world, it is pretty easy to break something like a Caesar cipher, since we can have a computer check hundreds of keys and check if decryption using that key gives us a meaningful English (or whatever language the sender uses) message, all in a matter of seconds!
However, with more difficult to break ciphers, how do people go about trying to break it. There are several different things that can be tried. One of them is just trying to guess a key- so let’s say we need a 4 digit numeric key, 1776 might be one of our guesses if the sender is American.
Another way to decipher messages is to try keys based on frequency of alphabets. If we know that a particular letter has only one symbol that it has been substituted with, it might be reasonable to say that the symbol that appears most often represents an ‘e’, since it is the most frequent letter in English. The last way that one might use in a decryption is brute force. Brute force is just trying all possible keys one by one, and is usually a task delegated to a fast computer.
A strong cipher is one that is difficult to break. For example, if we do not replace letters with the same symbol every time, but different symbols, then it becomes difficult for a third party to decipher the message. For example, let’s say we use a Caesar cipher, but for every word, the key is the length of the word. This might be difficult to break, but not impossible. Here is how it could be broken. Let’s just say that a computer tries keys one-by-one. When it realizes that a particular word is meaningful using that key, it gives you that word. It keeps giving you words with every key, and this could lead to decryption of the message.
A Substitution Cipher
So here is a cipher that I want you to consider. I will leave you with whether it is a strong cipher or not. We write 3 strings, all of which contain all alphabets, but arranged randomly. Not just all alphabets, we should include all characters that we might find on a keyboard. Just make sure all 3 are of the same length, and contain all alphabets (both upper and lower case) as well as numbers and a space.
For the first character in our message, we find the character in our first string, and note down what index it is at. We find the character at that index in the second string, and replace our letter with this character. For the second alphabet, we follow the same procedure, but instead of the second string, we use the third.
Here is a rough idea of how strong this method might be. There are approximately 95 characters on a keyboard. To arrange these in a random order, there are 95 factorial ways. Hence, there are 95 factorial ways for each string, and only when all three strings are correct can someone break this cipher. There are, in total, (95!)*(95!)*(95!) possible strings that one might have to check in a brute force!
So that’s it for this article. Hope you liked it! If you did, don’t forget to like and share!