This repository features a TypeScript simulation of the iconic Enigma machine β an encryption device used by the German military during World War II.
Having watched The Imitation Game movie with prior understanding of cryptography, I decided to challenge myself into creating a simulation of the basic functionality of the original Enigma machines. Then later try to break it just like Alan Turing and his collegues did.
Now the first step is done π
The rest of this document tells what I found out and provides a guide to run the simulation on your computer.
To try the live demo, first check out how to use it here.
Check out the simulation in action: Try it Yourself
The Enigma machine consisted of rotors, reflector, plugboard and a keyboard. Each key press initiated a complex encryption process involving the rotation of rotors and the reflection of signals. Its daily-changing settings posed a significant challenge to Allied codebreakers.
The process of transforming an input character involves a sequence of steps through various components, including the plugboard, rotors, reflector, and plugboard again before reaching the lampboard. Here's a brief description of the transformation pipeline:
-
Plugboard:
- The input character enters the plugboard, where initial letter swaps occur. Pairs of letters are connected, providing an additional layer of complexity to the encryption.
-
Rotors:
- The character then passes through a series of rotors, each with its own internal wiring. As the rotors rotate with each keypress, the electrical signal is further scrambled through these intricate pathways.
-
Reflector:
- After passing through the rotors, the signal is directed to the reflector. The reflector ensures that the signal is then sent back through the rotors in a different path, contributing to the complexity of the encryption.
-
Rotors (again):
- The signal, now reflected, passes through the rotors again in the reverse direction. This double-pass through the rotors adds a crucial non-linearity to the encryption process.
-
Plugboard (again):
- The signal re-enters the plugboard for a second letter swap. This final permutation provides an additional layer of complexity to the output.
-
Lampboard:
- The transformed signal finally reaches the lampboard, where the illuminated letter represents the encrypted output. This letter corresponds to the final encrypted character.
The transformation process involves a carefully orchestrated series of steps through various components, creating a highly secure and dynamic encryption process during each keypress.
Follow these steps to set up and run the Enigma machine simulation on your local machine:
- Node.js installed on your machine
-
Clone the repository:
git clone https://github.com/henryhale/enigma.git
-
Navigate to the project directory:
cd enigma
-
Install dependencies:
npm install # pnpm install
Run the provided example script to witness the Enigma machine in action:
-
Node.js
npm run simulate # pnpm simulate
-
Browser
npm run dev # pnpm dev
Explore the script to create your own scenarios and delve into different Enigma machine configurations.
To build the simulation for production, run the provided command
npm run build
# pnpm build
- source/: Implementation of the Enigma machine, rotors, plugboard and reflector.
- demo/simulate.ts: Example script for Node.js.
- demo/browser.ts: Example script for the browser (with index.html)
This section focuses on the browser demo taking you through how to encrypt and decrypt messages
- Open the demo locally in your browser or check out the live demo.
Mode
: Encryption - select theencrypt
radio button.Rotors
: For simplicity, leave each rotor position at0 - A
. Otherwise set the rotors to the desired positions say0 - A
,1 - B
,2 - C
. Be sure to note the order as it will be used to decrypt the messages.Lampboard
: Everytime you input a key, the output (ciphered) key will light up.Input
: Type the plain text message here.Output
: The output is represented the ciphered text from the input message.Mode
: Decryption - to decrypt the message, open the demo in another tab and selectdecrypt
under theMode
section.Rotors
: Set the rotor positions to the values they were at during encryption.Input
: Enter the ciphered text into the input textarea.Output
: The corresponding result is the plaintext you entered during the encryption.
If you have reached this point, you have got it π
Contributions are welcome! If you have suggestions or improvements, feel free to open an issue or create a pull request.
- How did the Enigma work? Youtube Video
- The Imitation Game (2014) - Film
- Enigma Machine - Wikipedia article
- The Enigma Machine - Crypto Museum
- Enigma: The Battle for the Code by Hugh Sebag-Montefiore
This project is licensed under the MIT License - see the LICENSE.md file for details.
Copyright Β© 2023 Henry Hale