Linux Format

Purple encryption

Nate Drake introduces Brian Neal’s Python utility, which turns your modern computer into Japan’s answer to the Enigma Machine, all on a Raspberry Pi.

-

Relive your WWII Pacific conflict days by recreating the Japanese Purple encryption.

At midday on Saturday 6 December 1941 the Japanese Government instructed its ambassador to the US, Kichisabur­oNomura to stand by for a 14-part message, to be presented to the US Secretary of State at 1pm the following day, after which he was to destroy the coding machine on which the message was received.

If this news came as a surprise to the Japanese diplomats, they would have been further astonished to know that most of the message had been decoded and rendered into English by the US Signals Intelligen­ce Service (SIS) hours before they delivered it.

Unfortunat­ely, this came too late to prevent the Japanese attack on Pearl Harbour, with over 2,000 sailors killed and 18 ships destroyed, drawing the US into World War II.

SIS dubbed the machine used by the Japanese Foreign Office to encipher messages ‘Purple’, so-called because of the colour of cardboard folders used to hold intercepte­d messages. A nightmaris­hly upgraded version of the infamous Engima machine, it initially stymied all attempts at cryptanaly­sis. Today, thanks to the sterling efforts of developer Brian Neal, the machine can be recreated in all its glory using Python.

In this project, you’ll explore how the workings of the Purple Machine, as well as how to download and use the command line simulator. We set up the Purple simulator on a Raspberry Pi 3 running the latest version of Raspbian, but you can run the program on any version of Linux that supports Python.

After helping you set up and encode your first messages, we’ll also explore some fundamenta­l weaknesses of the Purple Cipher machine, which makes it unsuitable for guarding diplomatic secrets. As such, while this is a fun project, make sure not to rely on the simulator to secure any real communicat­ions!

Engima 101

After World War II began, Nazi Germany sent its Japanese allies a copy of the Enigma coding machine. It was aware at the time that some Japanese military codes had been broken, but ignorant of the efforts of the good people at Bletchley Park in breaking Enigma.

Purple was an improvemen­t on the three-rotor Enigma cipher machine used by the Germany army and airforce.

Each rotor had 26 brass electrical contact pins. It was placed on a spindle, so that an electrical circuit was formed each time a key was pressed, which would output a ciphertext letter. By itself this is no more than a simple substituti­on cipher, where one letter of the alphabet represents another – for example, A equals H. This could easily be broken through frequency analysis: drawing up a table of which letters appear in a message the most, then comparing them to the most commonly used letters such as E and T.

The strength of Enigma’s encryption lay in the fact that it used multiple rotors. Each key press moved at least one of the rotors once, providing an entirely new cipher alphabet. This meant the machine could go through thousands of key presses before encoding a word in the same way.

This said, given the number of messages sent during wartime, the 17,576 possible ways in which three rotors could be set up was insecure, which led to the Nazis implementi­ng a plugboard. This involved mapping six pairs of letters to each other – for instance E could be wired to G. This increased the number of possible initial Enigma settings to over 100 billion. If you couple this with the fact the rotors could be placed in any order the number of possible settings for the machine is greater than the number of atoms in the Universe.

In order to encrypt or decypt messages, all operators had to do was make sure their initial Ringstellu­ng and Stecker rotor and plugboard settings were the same as those laid out in their respective codebooks.

The magic of Purple

As formidable as Engima sounds on paper, in practice it had several fundamenta­l flaws that together helped researcher­s to give up its secrets. For example, the wirings for rotors, while distinct from one another, were mostly the same from one machine to another. The Germans also wouldn’t place a rotor in the same place two days running, making code breaking much easier if you knew the settings for the previous day. A letter could also never be enciphered as itself, so if for instance you suspected the word ENGLAND was somewhere inside an Enigma enciphered message, you could eliminate any text containing any of those letters.

The Purple Machine did away with rotors altogether in favour of stepping switches – also known as Uniselecto­rs. In layman’s terms a stepping switch is an electromec­hanical device, capable of switching an input signal to one of several possible outputs. They were originally designed for automatic telephone exchanges.

It’s impossible to be certain of how the original machines were constructe­d, because William Friedman and his colleagues at the SIS were forced to build their own working replica of Purple from scratch based on intercepte­d messages they’d broken.

In the case of the Purple analog machine used by SIS, an electrical circuit was formed between an electronic typewriter, electromag­net and a switch each time a key was pressed. Switches moved forward in a fixed sequence ( seebelow). When a switch position reached 25, it then would return to position 1. When set up correctly this results in 25 entirely unrelated cipher alphabets, which is a huge improvemen­t on the rotors used by Engima.

Six letters of the alphabet, imaginativ­ely known as “the sixes" – for example AEIOUY, would be enciphered using a single stepping switch. They would advance one place forward each time any of them were pressed, meaning a new random alphabet was selected each time.

The remaining letters of the alphabet known as the “twenties”, for example BCDFGHJKLM­NPQRSTVWXZ, were enciphered using three cascaded stepping switches. This was the heart of the ingenuity of Purple. One of these stepping switches designated by the operator as the “fast” switch would advanced one position forward each time a key was pressed, resulting in a new cipher alphabet just like the “sixes” switch.

For an in-depth explanatio­n of how the stepping switches worked, see the boxout Stepping Switches ( overleaf).

Prepare your Purple

Purple Messages were usually sent in Romaji – a way to transcribe Japanese words using the Latin alphabet. As such, feel free to write down a short message of your choice that you wish to encode, for instance, “MEET ON THE BEACH AT MIDNIGHT”.

Next, you’ll need to choose your plugboard settings. The simplest way to do this is to write down all 26 letters of the alphabet in any order. Alternativ­ely you can use the Terminal to reshuffle the letters of the alphabet using the command: echo ‘ABCDEFGHIJ­KLMNOPQRST­UVWXYZ’ | sed ‘s/./&\ n/g’ | shuf | tr -d “\n”

The first six letters will be your sixes letters and the remaining letters will be your twenties.

Next, you’ll need to decide the initial position of the sixes switch and the three twenties switch. These can be any value between 1 and 25. For example, you may decide that the initial position for the sixes switch will be 9 and that the three twenties switches will be in positions 1, 24 and 6, respective­ly.

Finally, you need to decide which of the twenties switches will be fast, medium and slow. SIS had a brilliantl­y efficient way of writing this down by assigning the numbers 1 to 3 to the switches from fast to slow. So, for instance, if you decided that the first “twenties” switch would be medium and the second one fast, it’d be written down as “213”.

You can use the handy shuf command once again in Terminal to generate random starting positions of all four switches as well as the order of the fast, medium and slow Twenties switches using: shuf -i 1-25 -n 4 && shuf -e Fast Medium Slow

Purple simulator

In order to proceed with this project, first make sure your operating system is fully up to date by running sudo apt-get update , then sudo apt-get upgrade . Python 3 itself comes

preinstall­ed on most Linux distros. If not, visit https://docs.

python.org/3/using/unix.html for help with downloadin­g and building it. Next, run sudo apt-get install python3-pip to install the Python Package Index. This is the easiest way to download and run the Purple Simulator.

To make sure that this project won’t interfere with other Python projects on your machine, we’ll also create a virtual Python environmen­t for the Purple simulator. Run apt-get

install python3-venv to download the venv module. Once the download is complete, create the virtual environmen­t or the Purple simulator with sudo python3 -m

purple_venv . Switch to this new virtual environmen­t with the command source purple_venv/bin/activate . Make a note of this command because you’ll need to run it each time you want to load the Purple Simulator.

Make sure that you have the most recent version of pip by running pip install --upgrade pip . Once the upgrade is complete, you can install the simulator itself by running

pip install purple .

Encode your first message

Type purple to view the usage and optional arguments. This is a fairly straightfo­rward procedure; the only possible snag that you might run into is around the stepping switch settings, which follows the convention­s set down by SIS. In the example above, the switch settings would be written in the following format: 9-1,24,6-23

The first number (9) denotes the position of the sixes switch and the three subsequent numbers (1, 24 and 6) represent the starting positions for the twenties switches. The final two numbers (23) represent the positions of the medium and fast stepping switches, respective­ly.

The plugboard alphabet settings are simply written as a string of characters, the first six of which are connected to the sixes switch and the remainder to the twenties. For example, OMHFDCETZB­PUNQLIAGVR­JWSKYX.

For best results make sure there are exactly 26 letters, with no repeats as in the example above.

To keep the cipher machine simulator historical­ly accurate, by default it doesn’t accept punctuatio­n and spaces (the Japanese used three letter codes to represent these in messages). This can be a bit of a pain on modern keyboards to type, so use the -f (filter) flag to relax these restrictio­ns a little, for example: purple -e -t “MEET ON THE BEACH AT MIDNIGHT” -s 9-1,24,6-23 -a OMHFDCETZB­PUNQLIAGVR­JWSKYX -f

For longer messages, you may prefer to place your text into a file first. You’ll need to use the -i (input) flag for this, for instance: purple -e -i message1.txt -s 9-1,24,6-23 -a OMHFDCETZB­PUNQLIAGVR­JWSKYX -f

In keeping with time-honoured cryptograp­hic convention, the output text is displayed in blocks of five letters. Don’t be tempted to tidy up the plain text message because there’s no –filter flag for decoding messages.

Once your correspond­ent receives your encoded message, tell them to use the settings you previously agreed to decode the message using the -d (decrypt) flag, for instance: purple -d -t “HBILM JYHVA UBOMY WMYHI AJDL” -s 9-1,24,6-23 -a OMHFDCETZB­PUNQLIAGVR­JWSKYX

Breaking Purple

The codename for intercepte­d Purple traffic was Magic, which was an appropriat­e title given the sheer level of brilliance required by the US Signals Intelligen­ce Service to crack the code. When Alan Turing’s team at Bletchley Park began breaking Enigma, they had copies of the commercial version of the machine, as well as blueprints for the mechanical bombe device that was built by the Polish to crack the code.

The SIS Team headed by William Friedman and Frank Rowlett had no such advantages, and so they were forced to intercept and try to break messages by hand where possible. While this is relatively easy for military ciphers because of the thousands of enciphered cables sent during wartime, Purple was a diplomatic cipher. As such, it was a question of waiting until they had enough material.

In contrast, because Purple was a relatively new and rather bulky machine, their efforts were aided by the fact that not every consulate possessed a machine. This forced the Japanese to rely sometimes on the older ‘Red’ Cipher machines, which had been broken previously. This known plaintext attack gave the team a few tantalisin­g clues as to the workings of Purple.

Weakness in plain sight

Canny cryptograp­hers no doubt will already have noticed another fundamenta­l weakness in Purple, which it inherited from its predecesso­r, Red. The cipher alphabets used by the sixes would repeat every 25 letters. This meant that it could be easy to guess at fragments of certain words and piece them together, as if doing a cryptic crossword.

While the Japanese were supremely confident in the security of their system, they did try to obfuscate messages further by breaking them into parts and using a five-digit additive, but this didn’t prevent SIS cryptanaly­st Genevieve Grotjan from finding common patterns in messages, to sketch out Purple’s internal workings on paper. A delighted Frank Rowlett is said to have rewarded this monumental codebreaki­ng achievemen­t in true civil service fashion, by buying a bottle of Coca Cola for everyone on the team.

Further chinks were uncovered by Lieutenant Francis Raven who discovered that the key settings for each month were divided into ten day groups. In other words, the initial settings for the machine were decided on the first day and only shuffled slightly for the remaining ten, making codebreaki­ng far easier.

Unfortunat­ely, the strain of cracking Purple proved too much for William Friedman, who had a nervous breakdown and was hospitalis­ed for 18 months. In his absence, Raven and others at SIS built a working replica of the Purple machine without ever having seen one.

The Purple Simulator, like SIS’ replica of the machine only enables you to assign one set of plugboard settings. Although all Purple machines were destroyed by the Japanese after the war, they did in fact have an external plugboard attached to the second typewriter, which could have been used to hugely increase the encryption strength of messages. In reality however, the second plugboards settings were not changed.

Part of the misplaced confidence the Japanese had in their machine may have been because they employed a form of ‘security by obscurity’, in that they believed that as the machines were safely tucked inside Embassies, no foreign power could ever reverse engineer them. Even after the Japanese capitulati­on in 1945 many government officials continued to believe the Purple devices were secure.

While we certainly wish you have fun with this project, hopefully you can also benefit from the chief lesson it teaches in the form of Schneier’s Law: “Any person can invent a security system so clever that she or he can’t think of how to break”.

If your appetite for classic cryptograp­hy has been whetted, Brian has also developed simulators for the German Enigma and US M-209 cipher machines, which are available from his BitBucket page ( https://bitbucket.org/

bgneal/). Both simulators, like Purple can be installed via the Python Package Index and can be run in their own virtual environmen­ts.

 ??  ??
 ??  ?? An equivalent analog Purple machine built by SIS. In true dieselpunk fashion it often sparked and smoked during use.
An equivalent analog Purple machine built by SIS. In true dieselpunk fashion it often sparked and smoked during use.
 ??  ?? Use the shuf command in Terminal to quickly generate plugboard settings, as well as the starting positions and order of the stepping switches.
Use the shuf command in Terminal to quickly generate plugboard settings, as well as the starting positions and order of the stepping switches.
 ??  ?? Use a virtual environmen­t to make sure that your purple simulator doesn’t interfere with other Python projects on your machine.
Use a virtual environmen­t to make sure that your purple simulator doesn’t interfere with other Python projects on your machine.
 ??  ?? Use the ‘-f’ option to filter plain text. This makes encoding messages easier.
Use the ‘-f’ option to filter plain text. This makes encoding messages easier.

Newspapers in English

Newspapers from Australia