105800 Views
82496 Views
47243 Views
47066 Views
45458 Views
38527 Views
Pi-Apps
Intermediate MicroPython
Arduino Alvik
Raspberry Pi Time machine
Now Ad-Free
Guiding Light
Intermediate level MicroPython
Introduction to FreeCAD for Beginners
Building a Robot Arm with Raspberry Pi and PCA9685
Building User Authentication for Static Sites with FastAPI
Mastering Pydantic for Robust Data Validation
Mastering Markdown for Documentation with Jekyll
KevsRobots Learning Platform
56% Percent Complete
By Kevin McAleer, 2 Minutes
Learn how to encrypt messages like the one below using the Enigma machine and Py-Engima.
HEY ROBOT MAKERS THIS IS A SECRET MESSAGE becomes:
HEY ROBOT MAKERS THIS IS A SECRET MESSAGE
SUPSH GPYCV JTPYF TDQWV HIBEW FPDBN TAUEL IQXMS ZBDCT
No Spaces The Enigma machine had no spacebar, so spaces were represented by the letter “X”. This means that the message above should be read as: `HEYXROBOTXMAKERSXTHISXISXAXSECRETXMESSAGE’ We can easily replace space characters using the python .replace() method. e.g. plaintext = plaintext.replace(" ","X")
The Enigma machine had no spacebar, so spaces were represented by the letter “X”. This means that the message above should be read as:
`HEYXROBOTXMAKERSXTHISXISXAXSECRETXMESSAGE’
We can easily replace space characters using the python .replace() method.
e.g.
plaintext = plaintext.replace(" ","X")
from enigma.machine import EnigmaMachine machine = EnigmaMachine.from_key_sheet( rotors='II V III', reflector='B', ring_settings='1 1 1', plugboard_settings='AV BS CG DL FU HZ IN KM OW RX', ) # Set the initial position of the Enigma rotors machine.set_display('QJF') def pad_string(input): padding_needed = (5 - len(input) % 5) % 5 # Append the "X"s to the end of the string padded_string = input + "X" * padding_needed return padded_string def format_for_transmission(input_string): # Use list comprehension to insert a space after every 5th character # [input_string[i:i+5] for i in range(0, len(input_string), 5)] creates substrings of every 5 characters # ' '.join(...) then joins these substrings with a space # Calculate the number of "X"s needed to make the length divisible by 5 return ' '.join(input_string[i:i+5] for i in range(0, len(input_string), 5)) plaintext = 'HEY ROBOT MAKERS THIS IS A SECRET MESSAGE' plaintext = plaintext.replace(" ","X") plaintext = pad_string(plaintext) ciphertext = machine.process_text(plaintext) print(format_for_transmission(ciphertext)) # The output should be: # SUPSH GPYCV JTPYF TDQWV HIBEW FPDBN TAUEL IQXMS ZBDCT
In the next lesson, we will learn how to use Py-Engima to crack the Enigma machine using a brute force attack.
< Previous Next >