This project will turn your Raspberry Pi 400 (or any other Raspberry Pi) into a simulation of an Enigma cryptography machine so you can send encrypted messages to your friends. It will also demonstrate how to convert text-to-speech using a synthesizer.
An estimated 100,000 Enigma machines were constructed in different versions and this project will simulate the 3 rotor type used by the German army in World War II.
Enigma machines consisted of a keyboard for typing messages to be encrypted. Each character was first passed to a plugboard where it was mapped to an alternate character before being fed through a series of rotors, each with 26 characters which produced a further transformation, before feeding it back again by a reflector. The resulting ciphertext character was lit up by a lamp on the board between the rotor and keyboard.
Operators were provided with a copy of a monthly “key sheet” which contained initial settings for the machine’s rotors and plugboard, which were changed on a daily basis. Each time a key was pressed to encode a character, the rotors moved so that the same letter was never encoded the same way twice.
The project uses a software simulator written in Python to encrypt and decrypt messages in a similar way to that used in the original machine. It will also show how to convert text-to-speech so that decoded messages can be heard through a speaker.
The following commands will install py-enigma which is written in Python and the text-to-speech synthesizer, speak.
sudo pip3 install py-enigma
This will install py-enigma, your Pi will notify you of completion with “Succesfully installed py-enigma”
sudo apt install espeak
You are now ready to start using your Python Enigma machine.
Excellent documentation is available – https://py-enigma.readthedocs.io/en/latest/index.html
Just like with the physical Enigma machine, a key sheet containing daily settings is required. Originally these were printed on paper and physically distributed. Our version will be contained in a file but it will hold very similar information.
The file will need a row for each day of the month containing the following codes:
Day – Integer value between 1 and 31
Rotors – 3 Roman numerals between I to V, one for each of the rotors
Ring setting – 3 random characters
Plugboard settings – 10 pairs of characters
Reflector type – character B
15 II IV V A B S AV BS CG DL FU HZ IN KM OW RX B
Codes contained in the key sheet file will be read by py-enigma to set up the simulated machine for a particular day of the month.
Detailed information about the key file format can be found in the documentation: https://py-enigma.readthedocs.io/en/latest/keyfile.html
To encrypt a secret message we will follow a similar procedure used by real Enigma machine operators.
Firstly they would check the key sheet for the machine settings for that particular day. They would select 3 out of a set of 5 available rotors and place them on the spindle in the order shown on the key sheet. They would set the start position of the rotors to the 3 letter ring settings.
The 10 character pairs on the key sheet would then be configured on the plugboard by connecting pairs of letters together using cables. The simulation does all of this by reading a row from the keysheet file according to the day of the month.
Once the Enigma machine was set up, the operator would make up a random 3 letter starting sequence and a 3 letter key.
The rotors would be set to the starting sequence then the key would be typed in to encode it. The result was noted for use later.
This is how it is done in the simulation:
pyenigma.py --key-file=keysheet --day=15 --start='ABC' --text='XYZ'
ZMA
Having obtained the encoded key, the operator would set the rotors back to match the unencrypted key. Now the message could be typed in to encrypt it.
pyenigma.py --key-file=keysheet --day=15 --start='XYZ' --text='SENDXTHREEXANDXFOURPENCE'
RZJULYOPHSFFWRCEFGCMWOIZ
The message was then sent via morse code. It contained the transmitting station, receiving station, date, time, rotor starting position and encrypted message key along with the encrypted message itself.
The decryption procedure is similar to encryption but in reverse.
Enigma operators would find the settings for the day in the key sheet and the machine would be configured accordingly. The rotors would then be set to the 3 letter starting position obtained from the message. They would then type in the encrypted key sent in the message to decrypt it.
pyenigma.py --key-file=keysheet --day=15 --start='ABC' --text='ZMA'
XYZ
Now the rotors are re-set to the 3 letter unencrypted key and the message decrypted.
pyenigma.py --key-file=keysheet --day=15 --start='XYZ' --text='RZJULYOPHSFFWRCEFGCMWOIZ'
SENDXTHREEXANDXFOURPENCE
In this final section, we will chain together a series of Linux bash commands to process an encrypted message, decrypt it and pass it through a text-to-speech synthesizer so we can hear the result through speakers.
#!/bin/bash
day="$1"
start_pos="$2"
crypt_key="$3"
crypt_msg="$4"
# Decrypt message key
key=$(pyenigma.py --key-file=keysheet --start=$start_pos --day=$day --text=$crypt_key)
echo "Key: $key"
# Decrypt message using key
msg=$(pyenigma.py --key-file=keysheet --start=$key --day=$day --text="$crypt_msg")
# Print decrypted message with replacement char (X) replaced with space
echo "Message: ${msg//X/ }"
# Speak encrypted messsage
espeak " "; sleep 2; echo "${msg//X/ }" | espeak
Now we can easily decrypt the same message from step 3 and hear it spoken by the espeak synthesizer:
chmod +x decode
./decode 15 ABC ZMA RZJULYOPHSFFWRCEFGCMWOIZ
The response is shown below and you should hear it spoken by espeak.
Key: XYZ
Message: SEND THREE AND FOURPENCE
Now your Raspberry Pi is set up to encrypt secret messages which you can send to other people. Providing they have a copy of your key sheet and they set up their simulated machine accordingly, they will be able to decrypt your messages and send encrypted messages back.
As a bonus, you should also now be able to convert text-to-speech as well.
To understand how the Python code was written to simulate the Enigma machine, you can view the source code files in:
/usr/local/lib/python3.7/dist-packages/enigma
Enigma machines and their history are well documented on-line and several original examples have survived and are on display in museums in the UK, Europe and the United States.
A good place to start is with the references in the py-enigma documentation:
https://py-enigma.readthedocs.io/en/latest/overview.html
Happy encrypting!
From a quick tap to smashing that love button and show how much you enjoyed this project.