Cracking Enigma with Raspberry Pi

Learn how to use a Raspberry Pi cluster to decrypt the Enigma code.


 23 February 2024   |     3 minute read   |   By Kevin McAleer   |   Share this article on

Videos

For every project I create, I often make a corresponding YouTube video. Sometimes, there might be more than one video for a single project. You can find these videos in this section.

Explore more through these this dedicated videos.

Introduction

Explore the fascinating task of decrypting the Enigma code, a cipher used extensively during World War II, with a modern twist: using a cluster of Raspberry Pi 5s and Python. This guide will take you through the journey, enhancing your coding skills while unraveling a piece of history.


The Enigma Code Explained

Developed by Germany for secure WWII communications, the Enigma code was a cipher of unparalleled complexity. Its security came from daily changing settings, making manual decryption a Herculean task. Only those with matching settings could encrypt and decrypt messages successfully.

Enigma Animation


Raspberry Pi Cluster: Your Decryption Tool

A Raspberry Pi cluster, a group of networked Raspberry Pi computers, acts as our decryption tool. These small but mighty computers work together, distributing the computational load to decrypt the Enigma code efficiently, all managed with Python for simplicity and speed.


Py-Enigma: Simulating the Enigma Machine

Py-Enigma is a Python library that simulates the Enigma machine, allowing you to encrypt and decrypt messages just like the original. It also offers a brute force approach for cracking the code.

To get started, install Py-Enigma:

pip install py-enigma

Encrypting a Message

Hereā€™s how you can use Py-Enigma to encrypt a message:

from enigma.machine import EnigmaMachine

# Setting up the Enigma machine
machine = EnigmaMachine.from_key_sheet(
       rotors='I II III',
       reflector='B',
       ring_settings=[1, 20, 11],
       plugboard_settings='AV BS CG DL FU HZ IN KM OW RX')

machine.set_display('WXC')

# Encrypting the message
ciphertext = machine.process_text('ENIGMA')
print(ciphertext)

Setting Up Dispy on the Raspberry Pi Cluster

Dispy, a Python framework, allows us to distribute computations across multiple Raspberry Pis, speeding up the decryption process.


Building an OctaPi Cluster

Clone the repository and deploy using Docker:

git clone https://www.github.com/kevinmcaleer/ClusteredPi
cd ClusteredPi/stacks/octapi
docker-compose up -d

Distributing Tasks with Dispy

Once Dispy is set up, distribute computational tasks across your Raspberry Pi cluster:

import dispy

nodes = ['192.168.2.*','192.168.1.*']

def add_number(num):
    return num + 1

cluster = dispy.JobCluster(add_number, nodes=nodes, loglevel=dispy.logger.DEBUG)
jobs = []

for n in range(1,100):
    job = cluster.submit(n)
    job.id = n
    jobs.append(job)

cluster.wait()

for job in jobs:
    print(f"Job {job.id} result: {job.result}")

Learn More: The OctaPi Course

Interested in setting up a Raspberry Pi cluster and learning more about distributing tasks? Check out our OctaPi Course.

OctaPi Course

Learn about Raspberry Pi clusters and using Dispy for task distribution in the OctaPi Course.


Decoding the Enigma Code: Step-by-Step

Decoding starts with a Python-simulated Enigma machine setup, followed by encryption of plaintext to ciphertext. Decryption reverses this process, utilizing the same settings. A brute force method then tries every possible setting to uncover the original message, efficiently supported by the Raspberry Pi cluster.


Conclusion

Cracking the Enigma code, once deemed impossible, is now accessible through the power of Raspberry Pi and Python. This journey not only brings a historical cipher to life but also showcases the potential of modern computing. Dive in, and happy decoding!