Published on: 2023-07-09
Reading Time ≈ 7 min

Building and Cracking the Enigma - Part I

As more and more parts of our lives are shifted to the internet, the subject of security is becoming increasingly relevant. Large language models (LLMs) and chat controls claim more and more personal data. A significant part of security on the internet is achieved through encryption. During the last few weeks, I have been increasingly engaged with encryption and went down the rabbit-hole of encryption history. Despite the exciting early days of message encoding and encryptions like the Caesar-Cipher, the Enigma has a very special place in history, especially from the point of a computer scientist. Thats way I decided to rebuild the enigma in software, to subsequently also try to crack it.

Enigma

What is the Enigma

The Enigma is a cipher device mainly used in the second world war by the Nazis. Most of the military communication was transmitted with messages encrypted via the Enigma. Even when the allies obtained one of the Enigma devices, they were not able to decipher their messages because of its complex design created by Arthur Scherbius.

How it works

To understand the following parts, I want to provide a quick (and none complete) overview of how the Enigma works. The Enigma is a symmetric encryption machine in the form of a typewriter. Each Enigma has at least four rotors (wheels) with about 26 steps. There are some variants of the Enigma which also include additional turnover rotors and switches, but for simplicity these will not be considered her. Common to all Enigmas however, was a plugboard where the user could electricity connect two outlets to interchange the key for the letter that was pressed via the letter input buttons. When the user pressed a key an electrical current went through the, may or may not switched, keys of the plugboard, to the entry rotor stepping it and eventually their subsequent rotors, to a final turnover rotor which ultimately highlighted the ciphered latter on the displayed alphabet. Even though the Enigma is a symmetric encryption device, its design always produced different outputs even when the same key was pressed multiple times.

Enigma-Workings

To decipher a given text, the user would need a compatible Enigma machine and must know the exact configuration for the initial rotor state and the plugboard configuration. Overall there where about 150 trillion possible configurations

So cracking this will be quiet a challenge. A challenge even worth a hole (and fantastic) movie. The Imitation Game tells the storey about cracking the Enigma and the invention of our modern day computing model by the mathematician Alan Turing.

Some Cryptographic Context

Cryptography is the term used to describe the encryption of confidential information so that it can only be fully recovered by the intended recipient. Besides cryptography, there is stenography, which deals with hiding information and there is hashing which is also often used in the context of cryptography. With hash functions, however, information is lost, so that this technique is more suitable for unique assignments and the verification of information.

Cryptography and hash functions are fundamental components of our digital lives today. For example, the layers of the ISO-OSI reference model does not specify how to ensure, the information sent is identical to the information received. That's why TCP (the most used protocol on the web) has built-in error detection, based on checksums using hash functions. As the Internet grew out of its baby shoes and spread across universities into every aspect of our lives today, it became clear that the Internet was not designed with security in mind.

In today's world, TLS/SSL forms the basis of this, post-field added, security. On the web, public certificates and private keys are used to encrypt information from unauthorized entities. This is based on an asymmetric process where anyone can encrypt information with the public key, but only the owner of the private key can decrypt it. This is also the foundation for SSH keys and digital signatures. Since asymmetric encryption and decryption is quite time consuming, on the web there is a separate handshake (more about the SSL handshake) negotiated per session, where both sides agree on a secret symmetric key and then use it for encryption. With a symmetrical encryption you can encrypt and decrypt information with the same key, like with the Enigma. With the Enigma, the rotor and switchboard settings are the key, on the Internet it is often a password.

With each and every encryption there is the possibility guess the key. Mathematicians have accordingly searched for cryptographic methods that have a high complexity, making it difficult to guess the key. Trying every possibility in the problem-space through brute-force results in complexity, often measured in bits. Here is a small comparison:

EncryptionPossibilities base 2Possibilities base 10
Enigma2^68≈ 15 × 10^19
RSA 20482^112≈ 51 × 10^32
AES 2652^265≈ 59 × 10^78

Just as a reminder: Every additional bit doubles the possibilities. Current computers would take about 300 trillion years to crack a RSA 2048 key. To get a glims about AES 265 check out this great video.

Implementing the Enigma

I decided to rebuild the Enigma in software, to deeper understand its working, using my go to prototyping language python. First I needed to narrow down the scope of the parts I wanted to implement. I ended up with the following plan:

  • Enigma with 3 rotors and 1 reverse rotor
  • Support for the plugboard
  • Support for the letters a-z
  • Support for choice for 3 out of 5 possible rotors
  • Support for 1 out of 3 reverse rotors

This is a doable scope while also keeping the core concepts of the original Enigma. Modeling the rotors in software resulted basically in one list for every rotor containing tuples for the next step-position.

allRotor = {
    1: [[0, 4], [1, 10], [2, 12], [3, 5], [4, 11], [5, 6], [6, 3], [7, 16], [8, 21], [9, 25], [10, 13], [11, 19], [12, 14], [13, 22], [14, 24], [15, 7], [16, 23], [17, 20], [18, 18], [19, 15], [20, 0], [21, 8], [22, 1], [23, 17], [24, 2], [25, 9]],
    2: [[0, 0], [1, 9], [2, 3], [3, 10], [4, 18], [5, 8], [6, 17], [7, 20], [8, 23], [9, 1], [10, 11], [11, 7], [12, 22], [13, 19], [14, 12], [15, 2], [16, 16], [17, 6], [18, 25], [19, 13], [20, 15], [21, 24], [22, 5], [23, 21], [24, 14], [25, 4]],
    3: [[0, 1], [1, 3], [2, 5], [3, 7], [4, 9], [5, 11], [6, 2], [7, 15], [8, 17], [9, 19], [10, 23], [11, 21], [12, 25], [13, 13], [14, 24], [15, 4], [16, 8], [17, 22], [18, 6], [19, 0], [20, 10], [21, 12], [22, 20], [23, 18], [24, 16], [25, 14]],
    4: [[0, 4], [1, 18], [2, 14], [3, 21], [4, 15], [5, 25], [6, 9], [7, 0], [8, 24], [9, 16], [10, 20], [11, 8], [12, 17], [13, 7], [14, 23], [15, 11], [16, 13], [17, 5], [18, 19], [19, 6], [20, 10], [21, 3], [22, 2], [23, 12], [24, 22], [25, 1]],
    5: [[0, 21], [1, 25], [2, 1], [3, 17], [4, 6], [5, 8], [6, 19], [7, 24], [8, 20], [9, 15], [10, 18], [11, 3], [12, 13], [13, 7], [14, 11], [15, 23], [16, 0], [17, 22], [18, 12], [19, 9], [20, 16], [21, 14], [22, 5], [23, 4], [24, 2], [25, 10]]
}

The plugboard is only a hash map switching two arbitrary letters. The Enigma class just used these inputs as settings and passed each character passed to to the plugboard, the reverse rotor, stepping it, the rotor 1 to 3, stepping them and back to the reverse roter.

Due to the reverse rotor, the Enigma is symmetrical in operation. This means that users do not have to distinguish between cyphering and deciphering inputs. Using it looks something like this:

> pyenigma.py
Provide a strimg to encrypt/decrypt: dont panic   
Encryped: kihdqqocv

You can checkout the code on my GitHub.
Disclaimer: This is a quick and dirty implementation I did in collage. Its NOT production grade!
The next step will be cracking it.

See you!

❤️ Is this article helpful?

Buy me a coffee☕, PayPal me or support this space to keep it 🖖 and ad-free.

If you can't, do send some 💖 or help to share this article.