This is a simple implementation of the Enigma Machine in Go. It is based on the Enigma I, the most common version of the machine used by the German military during World War II.
The Enigma Machine is a simple substitution cipher. It works by taking a letter of the alphabet and substituting it with another letter. The substitution is determined by the settings of the machine, which can be changed by the operator.
- Simulates the Enigma I Machine
- Configurable rotors, reflectors, rotor positions, rotor ring settings, and plugboard pairs
- Command-line interface
- Configurable settings using flags or a config file
- Go 1.22.2 or later
To install the package, run the following command:
go install github.com/natac13/go-enigma-machine
You will then have an go-enigma-machine
executable in your $GOPATH/bin
directory.
The CLI tool takes a message as input and outputs the encrypted message. To use the tool, run the following command:
go-enigma-machine encrypt "bootdev rocks"
Output:
Enigma machine settings used:
- Reflector: B
- Rotors: [III II I]
- Rotor positions: AAA
- Rotor ring settings: AAA
- Plugboard pairs: []
Original message: bootdev rocks
Encrypted message: WLQUC DIFFV VH
You can also specify the settings of the Enigma Machine using flags. For example:
go-enigma-machine encrypt "bootdev rocks" --reflector C --rotors III,IV,II --rotor-positions ABC --rotor-ring-settings DEF --plugboard-pairs AB,CD,EF
Output:
Enigma machine settings used:
- Reflector: C
- Rotors: [III IV II]
- Rotor positions: ABC
- Rotor ring settings: DEF
- Plugboard pairs: [AB CD EF]
Original message: bootdev rocks
Encrypted message: QKHYV RICZR BB
You can also specify the settings in a config file. The config file should be in YAML format.
The file location can be specified using the --config
flag.
And by default, the CLI will look for a file named .go-enigma-machine.yaml
in your home directory.
For example:
rotors:
- III # Leftmost rotor or first rotor
- II
- I # Rightmost rotor or last rotor
rotor-positions: AAA
rotor-ring-settings: AAA
reflector: B
plugboard:
pairs:
- AB
- CD
- EF
The following settings can be configured:
- Reflector: Choose from
A
,B
, orC
. - Rotors: Choose from
I
,II
,III
,IV
, orV
. - Rotor Positions: A three-letter string representing the initial position of the rotors. (e.g.,
AAA
). - Rotor Ring Settings: A three-letter string representing the initial ring setting of the rotors. (e.g.,
AAA
). - Plugboard Pairs: A list of pairs of letters that are swapped before and after the encryption process. (e.g.,
AB,CD,EF
).
The following flags can be used to configure the Enigma Machine:
--reflector
oru
: Choose fromA
,B
, orC
.--rotors
orr
: A list of three rotors to use. (e.g.,I,II,III
). The leftmost rotor is the first rotor, and the rightmost rotor is the last rotor.--rotor-positions
ord
: A three-letter string representing the initial position of the rotors. (e.g.,AAA
).--rotor-ring-settings
ors
: A three-letter string representing the initial ring setting of the rotors. (e.g.,AAA
).--plugboard-pairs
orp
: A list of pairs of letters that are swapped before and after the encryption process. (e.g.,AB,CD,EF
).
Fun Facts:
- The
u
shorthand for reflector selection stand for [U]mkehrwalze, German for "reversing rotor". - The
s
shorthand for rotor ring settings stand for Ring[s]tellung, German for "ring setting".
This was a project I did to learn more about Go. If you have any suggestions or improvements, feel free to open an issue or a pull request.
MIT