How Ciphers Work

Melita
6 min readJul 26, 2020

--

Information plays a big part on or daily lives. Everyday, we exchange information with various degrees of importance. It could be something as simple as tonight’s dinner menu, or as critical as when to attack in a war. It has the power to turn a losing battle into a winning one — which is why making sure that information reaches its intended target without being leaked is crucial. This can be done using ciphers.

So, what’s a cipher?

A cipher is an algorithm that can encrypt or decrypt a message. A cipher transforms a message, called plaintext, into something called ciphertext, which is a message that has been scrambled with a certain key. By using a cipher, even if someone manages to find a way to obtain your message, they wouldn’t be able to make sense of it. An example of a plaintext and a ciphertext can be seen in the image below.

An example of a cipher, with the word “SECRET” as plaintext and the word “ZLJYLA” as ciphertext.

In the image above, we can see that the word “SECRET” has been turned into the seemingly nonsensical word, “ZLJYLA”. This is done by using one of the monoalphabetic substitution ciphers, which replaces a letter with another letter. There are two well-known examples of this type of cipher, namely the Atbash Cipher and the Caesar Cipher.

The Atbash Cipher

The Atbash Cipher replaces each letter with its mirrored counterpart. It was originally used for the Hebraic alphabet, but with a little modification, it can also work with the English alphabet. In this cipher, the first letter “A” becomes the last letter “Z”, the second letter “B” becomes the second-to-last letter “Y”, and so on. Using an Atbash Cipher, the plaintext “I LIKE CATS” will become “R ORPV XZGH”. Below is an image that illustrates this cipher.

An illustration of the Atbash Cipher.

The Caesar Cipher

If the Atbash Cipher works by replacing a letter with its mirrored pair, the Caesar Cipher works through shifting letters by a set number of times. With a shift of one, the letter “A” becomes the letter “B”, the letter “B” becomes the letter “C”, and so on. With a shift of two, the letter “A” becomes the letter “C”, the letter “B” becomes the letter “D”, et cetera. Below is an illustration of the Caesar Cipher.

An illustration of the Caesar Cipher, with a shift of one and two.

The first cipher we’ve seen above is an example of the Caesar Cipher. Each letter in the word “SECRET” is shifted 7 times, and the end result is the word “ZLJYLA”. But are these ciphers enough to keep your information from being leaked? Let’s consider ways to crack these ciphers.

Cracking a Cipher, Part One

For example, if you know that the cipher used is an Atbash Cipher, you can just mirror the letters one by one again to solve it. If the one used is a Caesar Cipher, you’ll also be able to crack the code through brute force by testing the letter shifts one by one. But is there no faster way to crack it? And what if the message used a custom substitution cipher?

A monoalphabetic substitution cipher can be cracked through a process called the frequency analysis. A frequency analysis counts a amount of times each letters appear in a cipher, and compares the results to the distribution of letters in the language the cipher is in (e.g. English). If the most common letter of a ciphertext is “G”, then there is a good chance that the letter “G” is originally the letter “E”, which is the most common letter in English. Below is a chart of letter frequency in the English language.

A chart representing the letter distribution in English. Source: Wikipedia.

Other than the frequency of single letters, bigrams (a pair of letters such as “TH” or “ON”) and trigrams (a combination of three letters such as “THE” or “ING”) can also be used to solve a cipher. Some of the most common bigrams in English are “TH”, “HE”, “IN”, “ER”, and “AN”. Meanwhile, some of the most common trigrams are “THE”, “AND”, “ING”, “HER”, “HAT”, and “HIS.”

The longer a message is, more traces such as letter distribution will be revealed. How do we prevent this from happening? The answer is to flatten the letter distribution — using a polyalphabetical cipher, such as the Vignère Cipher.

The Vignère Cipher

A Vignère Cipher is basically using multiple Caesar Ciphers in a message. If a Caesar Cipher shifts an entire message by a certain shift (e.g. 3 shifts), a Vignère Cipher uses multiple repeating shifts in a message. This cipher uses a keyword to decide the shift used throughout the message. For example, if the message is “THIS IS A SECRET MESSAGE” with the keyword “SECRET”, the result would be “LLKJ ML S WGTVXL QGJWTYI.” By shifting the letter “T” with key “S”, it becomes the letter “L”. To do this easily, a Vignère table is used, which can be seen in the image below.

A Vignère table which can be used to cipher and decipher messages. Source: Wikipedia.

With this method, the letter distribution is flattened. But can this code still be cracked? Can the frequency analysis still be done on this cipher?

Cracking a Cipher, Part Two

As has been said before, a Vignère Cipher is, in essence, multiple Caesar Ciphers. Thus, a Vignère Cipher can be cracked like a Caesar Cipher with frequency analysis, if the length of the keyword used is known. There are a few methods to help determine this — one of them is the Kasiski Examination, which relies on the fact that some words will be repeated using the same keyword, leading to repeated ciphertext letters. The longer the ciphertext is, the better this method will work.

After the keyword length is known, frequency analysis can be used on ciphertext letters which have the same shift amount, thus cracking the ciphertext. Then, is there anyway to make a message impossible to crack? Now, let’s talk about the One-Time Pad.

The One-Time Pad

The One-Time Pad (OTP) is a technique that is similar to the Vignère Cipher, in that it uses multiple Caesar Ciphers. The difference between them lies in the length of the keyword — the keyword length of the OTP must be at least as long as the message itself. This means no information of the original message will be left, and the cipher is impossible to crack. Thus, a cipher made using OTP is considered to have perfect secrecy. The only downside is that you need to have a long, random keyword, that must never be known to anyone else other than the sender and recipient of the message.

--

--