I can remember at school there were two girls who passed each other notes all the time, once the teacher took the paper off them but couldn’t make sense of it as it was written in code, some kind of arrangement they had made with each other. In some crude way that transaction was encrypted.
The need for keeping secrets is as old as humanity itself – but it might Suprise you hold old encryption methods are, in this blog I am going to discuss two encryption methods one VERY old and one VERY famous.
The Ceasar Cipher
Once of the oldest known methods of encryption was used by Julius Ceasar and is named after him. The use of this cipher was documented as early as the year 56 AD.
The Cesar cipher is a substitution cipher and involves shifting the alphabet by an agreed number of positions, this can be easily shown in a diagram.
Shift of 3 to the right
Original Value | Encrypted Value |
A | D |
B | E |
C | F |
… | … |
Z | C |
as Z shifts beyond the end of the alphabet it loops around again to the start.
using this shift pattern of 3 the phrase “Cesear was ahead of his time” would be Fhvhdu zdv dkhdg ri klv wlph.
Given the level of literacy at the time in ancient Rome, this was most likely extremely secure. However by modern standards it is very weak
Breaking Cesar
There are two ways to break this code;
- Brute Force
- Text Analysis
Brute Force
The major weakness in the Ceasar Cipher is the limited number of possibilities. Using the Latin alphabet there are 26 letters – which means you only have 25 possible shifts before you reach the start again. so, if we took our example from earlier and knew it was a Ceasar cipher that was used, we could brute force it as follows (shifting it back to the left).
Code to break: Fhvhdu zdv dkhdg ri klv wlph.
Shift Pattern Used (Left) | Output Text |
1 | Egugct ycu cjgcf qh jku vkog. |
2 | Dftfbs xbt bifbe pg ijt ujnf. |
3 | Cesear was ahead of his time. |
But what if you don’t know it’s a Ceasar Cipher?
It is possible to break the Ceasar cipher by using frequency analysis, this is true for most simple substitution ciphers because they are limited by the fact all you are doing is swapping one letter directly for another. if we were to perform an analysis on English text, we would usually find the letter “E” is the most commonly used letter. the distribution of letter use actually is quite distinctive.
In our encrypted text if we saw that the letter “H” was the most common, it would be a good starting point to assume that this H originated as the letter E, suggesting in this case a 3 character right shift (E,F,G,H) this approach can continue to be applied until the text is readable.
Coding the Ceasar Cipher
Coding the Ceasar Cipher is fairly simple – obviously there are hundreds of ways you can do it. I have some example code here: CeasarCipher (github.com)
The way this program works is as follows;
- Generate a reference list of the alphabet in order
- For each letter in the text to be encrypted find the original position, add the shift pattern and pick th letter from that position in the alphabet (if the total of shift+letter position is >=26 deduct 26 to go back to the start of the alphabet.
To decrypt I simply shift left instead of right (shift – letter position).
The console app expects 3 parameters (the text to be encrypted, if it is to be encrypted or decrypted and the shift number to use).
Below is the console output of an encryption example with a shift pattern of 10;
and decryption.
So we have now seen a basic substitution cipher, but its once that given fairly trivial effort can be broken. With this knowledge let’s step forward to by far the most famous substitution cipher of them all. The Enigma Machine
The Enigma Machine
The Enigma was a machine used extensively by the Nazis in World War 2 to encrypt their messages. The machine itself and its story in general is absolutely fascinating and the heroics and methods of the polish scientists and British Ultra team (lead by Turing) who eventually cracked the code are the stuff of legend. They even made a Hollywood movie about it (The Imitation Game (2014) – IMDb).
I am not going to discuss these events, however if you’ve not read about it though I recommend you do. There are many better resources on the topic than this blog! But I am going to roughly explain how this machine works and show some coded examples of it.
How many Possible combinations
One of the issues identified with the Cesear cipher was that it has only 25 possible combinations. We demonstrated that a trivial brute force attack can decode messages with this, the enigma machine certainly didn’t have this problem. There are a total of 158,962,555,217,360,000 possible combinations with the various configurations of the machine (Numberphile – YouTube).
It also had a very clever system for making frequency analysis much more difficult. in the Ceasar cipher we noted that a letter in plaintext will always become the same letter in the ciphertext – so with a shift of 1 “A” will always be “B”. The enigma machine implemented a mechanical system to make sure this wasn’t the case.
How Does the Machine Work
There is an excellent 5 minute animation on youtube showing how this machine worked (How the Enigma machine works). I recommend you watch this video if you are interested in the workings of the machine. However I will sum it in a more basic way below;
Overview
The diagram below shows a simplified version of the circuitry used in the Enigma machines. Obviously, the full version would have 26 letters.
The Enigma machine could both encrypt and decrypt a message, it consists of 4 Core Components;
Component | Description |
Keyboard | The operator would type the message they wanted to be encrypted via this keyboard |
Plugboard | This is where the initial substitution of letters takes place. pairs of letters are swapped, in total you could rearrange 13 letter pairs. on the diagram above you can see A is swapped with Z and therefore Z with A, P with B and so on.. |
Scramblers (Wheels) | There were 3 moving wheels on the enigma machine each one had the each letter of the alphabet on them – but not in the right order. Their purpose was to scramble the message The wheels shown above are labelled Left, Middle and Right. However, I prefer to think of them as Fast, Medium and Slow. They turn a bit like the mechanism on a clock. The fast wheel turns every keystroke on the keyboard (like a second hand on a clock) The medium wheel turns after one total revolution of the fast week (like a minute hand on a clock) The Slow wheel turns after one total revolution of the medium wheel (like the hour hand on a clock). It is these wheels turning that provide different output for the same letter. |
Reflector | The Reflector reversed the signal (the actual machine was one circuit) to pass through the wheels again to return the encrypted letter typed |
Lampboard | There was a bulb under each letter of the Lampboard and it lit up to tell you what the ciphered letter was, the output of the process. |
There were various configurations of these components and for an operator to decrypt a message they would need to know all the settings that were used when starting to type the message (e.g. the plugboard swaps and what wheels to use in what order).
Example
I will try to explain how this machine works conceptually – obviously in reality it was an Electonic circuit, so this is more explaining the logic behind the machine rather than the mechanics per se.
Let’s say we want to encrypt the word “HI”. How would this all work.
Firstly, you would need to configure the machine – put the wheels you want to use and your plugboard settings. our base machine for this example is shown below.
We have some swaps in the plugboard (A to Q for example), random character mapping in the wheels and pair swaps in the reflector.
So let’s Type the Letter H.
the corresponding position of L in the reflector is F;
The letter “I” is the next letter to calculate – the pattern is the same, but the first wheel (the fast wheel) has moved forward a position.
Note that the plugboard performs a swap here “I” with “P”
and the return from the reflector is as follows.
Input: HI
Output (on these settings): UD
What is interesting is you can see how the wheels moving stops the same letter from being given the same value for example if the above message was HIH – the second H wouldn’t be the letter “U” in the ciphertext note how its path is different.
So an input “HIH” would be UDN.
How does Decryption work
The enigma can decrypt messages the same way by typing in the ciphertext and showing the cleartext on the Lampboard. However, the swaps on the plugboard / wheels and wheel position HAVE to be in the original position from when the original text was typed. so in the example of HIH – we have moved the fast wheel twice – this would need to be back in its original position to convert UDN back to HIH.
it works in exactly the same way though – imagine the blue highlighted cells are the inbound signals (to the reflector) and the yellow ones are outbound (from the reflector).
Coding the Enigma
As always there are so many ways this could be coded, I don’t claim mine to be the most optimised or fastest – but I do think it’s relatively easily understood (which is usually my goal).
The Approach I took was to write 2 Classes
- EnigmaMachine – this is the class that performs the operation of ciphering text
- Wheel – this class has 2 properties values which is a list of key value pairs shown in the tables above and countOfRevolutions an integer that stores how many times the wheel has rotated. The wheel has one method – rotate.. which rather obviously rotates the wheel.
The machine is initialised by setting wheels and the plugboard;
I have used the setting from the tables above. you could change them to official enigma ones if you so wished.
The process for encrypting is exactly as mapped out in the previous examples using positions and values in a collection to work your way to the reflector and back.
The only other tricky thing is knowing when to rotate the wheels. I have created various instances of the wheel class (plugboard, fast,medium,slow and reflector). Technically the reflector and plugboard aren’t wheels – but to save myself some time I coded them as if they were. In effect they work exactly the same but they can’t rotate.
Fast medium and slow wheels can rotate and rotate at different points in the process. I handle this in the rotateWheels method;
Running The Code
Encryption:
Input: “HOW DID ANYONE EVER CRACK THE ENIGMA CODE”
Output: UKD HCY GHOQYN QJRM WSQIM VFF PFARSN PYUM
Decryption:
Input: “UKD HCY GHOQYN QJRM WSQIM VFF PFARSN PYUM”
Output: HOW DID ANYONE EVER CRACK THE ENIGMA CODE
Cracking the Code
The enigma is an incredible machine a mix of maths, mechanics and electronics. I have really enjoyed looking into it for this blog post. It was nearly unbreakable as a cipher but it did have one flaw which could be exploited. The Letter input always gets a different value to the original letter; For example, “S” can never be encrypted as “S”.
this might not seem like a big deal, but it reduced the number of possibilities exponentially, despite this breaking the code was still a ridiculous task. What really led to the cracking of the code was the weakest point in the system. People.
The German army ended nearly every encrypted message with the phrase ‘Heil Hitler’. Given a knowledge of cleartext and the relating ciphertext the code breakers had enough material to work on probable solutions. Without this repetitive use of language, it would have been extremely difficult to break the Enigma cipher. for a much more in-depth guide to how the code was broken please refer to Cryptanalysis of the Enigma.
Code for the Enigma example is on GitHub: Enigma