A simple enigma machine in Go. This is just a toy project to play a bit more with Go and it's tools, dependency management, and all that.
The main objective is to emulate a "generic" M3/M4 Enigma (the most known/used model in the War), in the command line.
According to Wikipedia,
The Enigma machines were a series of electro-mechanical rotor cipher machines developed and used in the early- to mid-20th century to protect commercial, diplomatic and military communication. Enigma was invented by the German engineer Arthur Scherbius at the end of World War I. Early models were used commercially from the early 1920s, and adopted by military and government services of several countries, most notably Nazi Germany before and during World War II. Several different Enigma models were produced, but the German military models, having a plugboard, were the most complex. Japanese and Italian models were also in use.
It was, basically, a machine used to encode military messages on WWII. It has an interesting history and emultating it serves as a fun programming exercise.
Some people belive that an image is worth a thousand words. If that is the case, the followin diagram (stolen from here) sums up pretty well how such a machine works:
Basically, the input travels from the keyboard to the plugboard and then, to each rotor, ultil it reaches the reflector (red arrow). After that, the signal bounces on the opposite path, until it reaches the lightboard (blue arrow). Not that complicated, right?
Simply running the executable is enough to start the application with a default set of options (use --help
) to see all the flags available. By default, the coded text is written to STDOUT
after every newline. You can change this behavior using the -o
flag, e. g. enigma -o coded.txt
.
You can also encode an entire file in one go by piping it to the application: cat plain.txt | enigma -q > coded.txt
.
There are basically two ways to use the API. The first one, in the package enigma exports an easy-to-use built-int enigma machine, with configurable rotors, ring settings and window settings. It is also possible to use the Assemble funcion to specify every individual part of the machine.
The second package, parts, contains the interfaces for every machine part used by the enigma, with default implementations as well.
This implementation is a bit more 'flexible' than the actual enigma hardware. For example, you can use the same rotor more than once all rotors are valid in all positions, etc. This is intentional to make the API and machine construction as flexible as possible.
These are the main references/documentation used in this project:
- How Enigma Machines Work
- An example of the basic Enigma
- Working principle of the Enigma
- Enigma cipher algorithm
- Universal Enigma, for checking
- M3/M4 Enigma emulator, also used for checking
MIT. Take a look at the LICENSE
file.