Thursday, October 30, 2014

Implementing Rotary Encoders in an Touch screen LCD.

A feature I wanted the Enigma Arduino Simulator to have was to slide in a circular motion to change the Enigma Uhr and the ring settings for the rotors.

To do so, I created the following function.

char RotaryButton(INT8U clickX, INT16U clickY, INT8U cX, INT16U cY, byte minR, byte maxR, byte steps)

It takes as parameters, the location of a click, the center of the circular control, the minimum and maximum radius of the area that will be recognized to drag in a circular motion and how many steps will a full rotation consist of.

The function uses pithagoras, to calculate C, the distance from (clickX,clickY) to the center of the circle (cX,cY).

http://betterexplained.com/articles/measure-any-distance-with-the-pythagorean-theorem/

If the distance C is between the minimum and maximum radius accepted, then another algorithm compares (clickX,clickY) with the X,Y coordinates received the last time the function was called. The distance from the current (clickX,clickY) to the previous (clickX,clickY) is computed using pithagoras and then both coordinates are sent to a series of decision functions to see if this distance is going to be used to increment the step counter or decrement it.

To make the increment/decrement decision, the circle is divided in four sections, left and right of a vertical line running through the center of the circle and above and below a horizontal line running through the center of the circle.

For the section to the left of the center, if the new Y value is bigger than the previous Y value (new Y is below old Y and to the left of center), the slider is being rotated counter clockwise.

For the section to the right of the center, if the new Y is bigger than the previous Y (new Y is below old Y and to the right of center), the slider is being rotated clockwise.

Each of those detection sections vote by either increment the "inc" or "dec" variables. At the end, if the "inc" counter is greater than the "dec" counter and the distance exceeds a threshold, a +1 is returned.

The voting algorithm results in a robust detection of the user intentions when rotating around the circle.

You can download the sketch called EnigmaRotaryEncoder.ino from the folder below so you can see how this works.

https://drive.google.com/folderview?id=0B0nObKKDtir6Wlk2dzh2NC1vTjg&usp=sharing#list

The video below starts at the 1:55 mark, where the software rotary encoders are being used:



APC Magazine example decoded

APC Magazine shows an enigma machine built with an Arduino, a two line LCD shield and a PS/2 keyboard.

http://apcmag.com/arduino-projects-enigma-cipher-machine.htm

An encrypted text is given as an exercise to the reader to decode.

I-II-III APC MAG DY AP CM
OLCRH ICCKD CWWMO ZQFNR ISPCI KREKM VMSLI BJZGM DXDEJ WXQZQ EI

Using the manual available at:
http://users.telenet.be/d.rijmenants/Enigma%20Sim%20Manual.pdf

Following the instructions on how messages were sent over the air, I was using a 3 wheel machine with a B reflector and rotors I,II,III, then trying to set the starting position to APC, and then typing MAG to obtain a message key of PWE. The rotors were moved to PWE and then OLCRH was typed, yielding gibberish.

Looking at LCD pictures of their machine, I finally realized that the first line matched the settings required in the order their program asks for them:



Thus, the rotors are I,II,III, APC is the rotor start setting, MAG is the ring setting, three plugs are used DY AP CM and then the message is simply typed starting at OLCRH.

I entered the same settings in my Enigma Engine and created the EnigmaSerialAPCMAG.ino sketch that decodes this text. To use it, upload to an Arduino UNO, open the Serial Monitor with 9600 baud and start typing OLCRH... a neat message about an early supercomputer will appear.

https://drive.google.com/folderview?id=0B0nObKKDtir6Wlk2dzh2NC1vTjg&usp=sharing#list

Enjoy

Tuesday, October 28, 2014

Free Open Source Software implementation of Arduino Enigma Algorithm thru Serial Port

At the link below you can download the source code to run the Enigma algorithm in an Arduino.

Enigma thru serial port

Download the file called EnigmaSerial.ino

The machine settings are initialized in the initEnigma() function, the wheel types, rotor positions, ring settings, plugboard and Uhr are initialized there.

In the loop function, the serial port is read, if the special character ! is sent, the next characters will be used as rotor positions. Then the machine will encode a character.

This will allow you to have an external device to the computer performing the enigma encryption.

This is the same engine used in the Arduino Enigma Machine Simulator.

This open enigma engine has been used to create several enigma machines. arduinoenigma.blogspot.com/2015/10/projects-made-with-arduino-enigma.html

Wednesday, October 22, 2014

Using the Arduino Enigma Simulator


This guide shows how to operate the Arduino Enigma Simulator and encrypt your first message.

 The about screen is shown upon the first 3 times power is applied.

This screen clarifies that this device is for educational, historical and entertainment purposes. The encryption principle implemented has been broken many times in the past.

A list of online resources that provided information used to build this implementation are listed.

 Touch OK to continue to the main screen.
This is the home screen, the Enigma machine is ready to encode messages with the settings in effect at the moment. If this is the first time the machine is powered up, the default settings are loaded, otherwise the last settings used are kept.

To start encoding a message, press and hold the keys at the bottom of the screen, the wheels will move, a lamp in the middle section will illuminate for as long as the key is pressed and the corresponding character will also be printed in the tape running above the lamp field. Once the key is no longer pressed, the lamp will turn off.

The default setting of AAAA can be changed by dragging the rings to the right of the corresponding letter up or down.

By default, the machine is configured with the B thin reflector, Beta greek wheel (additional wheel), Wheels 1,2,3. All wheel settings to A, No plugs.

Encoding a message with the default settings is no fun, so let's touch the Enigma logo to open the machine and be able to change the rotors.

The machine is now open, notice that the bulbs in the lamp field are exposed, the rotors are shown, the printer, texture, serial port functions can be changed and a box of spare rotors is shown at the bottom of the screen.

To start changing the rotors, the ones installed must first be removed, to do so, touch the letters in the rotors to be removed.
Rotor I has been brought down to the middle area and placed flat on its side. Here the inner setting can be changed or it can be placed in the spare rotor box by touching the area inside the spare rotor box.
The rotor has been placed in the spare rotor box, continue removing the other three rotors by touching them and touching the box below.

Once a rotor is in the intermediate position above the lamp field it can be placed in the machine if there is an open space or back in the spare box.

This can be used to change the position of a rotor without putting it back in the box. For example to move Rotor III from the rightmost position to the second position, touch the rotor and then touch the empty space in the rotor mechanism.
All the rotors have been removed from the machine, now we will install different ones. Start by touching the letters of rotor VII on the spare rotor box.
Rotor VII is now flat on its side and the inner setting can be changed.

The next step will shown how to set the machine to use reflector C, greek rotor Beta, and rotors VII, VIII, VI. Inner rotor setting will be set to PLUS, outer settings to BIKE.

To change the inner setting, touch the contacts of the rotor and while pressing down, slide around the wheel until the desired letter is shown. Sliding in a clockwise direction increments the setting. Doing so in a counter-clockwise direction decrements the setting

Changing the inner setting changes the letter substitutions performed with the same visible letters on the wheels. The turnover point for the rotor, the letter at which the rotor to the left is advanced is not affected by the inner setting.

The inner setting has been changed to L, now its time to install the rotor in the machine. To do so, press the location where the second rotor would be installed, The leftmost location in a four wheel machine can only be filled with a greek rotor, Beta or Gamma.
Next, Rotor VIII is selected, rotated to U and installed in the third position.
Rotor VI is selected, rotated to S and installed in the fourth position.
Lastly, a greek rotor must be selected, rotors I thru VIII cannot be installed in this position. The greek rotors cannot be installed in the middle positions either. To close the machine, all rotors must be present. The machine will not let you install a non-greek rotor in the left position, or a greek rotor in the next 3 positions.
Greek rotor Beta is selected, rotated to P and installed in the leftmost position.

The rotor selection and placement can be done in any order, not necessarily the same order shown here.
Now, touch the reflector and it will change from B thin to C thin. Touching it again will change it to A for an Enigma I machine, then B, and C for an M3 Enigma. The model and creation year of the selected enigma machine will be shown in the front panel view when the machine is closed. This helps distinguish the specific model when a three rotor machine is used

After the reflector is changed, we can either close the machine or install some plugs in the plugboard. To do so, touch the plug area below the spare rotor box.
The plugboard is now shown. The machine keys are shown at the top of the screen. To return to the previous screen, touch the keys at the top. To install some plugs, touch the area below the letters.

Start installing a plug from J to U by touching below the J.
To install the other side of the plug, touch the area below the U.
The area below the J will now show that is connected to U, The area below U will show that is connected to J,

A strip of paper will be shown below the plugboard showing the order in which the plugs were installed. When an Uhr unit is not used, JU is the same as UJ.

When using the Uhr, the order of the plugs does matter. That is why they are shown below the plugboard.

To remove a plug, touch either end. At this point to remove this plug, touch either the area below the J or below the U.
After the plugs for JU, continue installing the plugs:
LY NE XT QR BA DF OS KI
Lastly, install the plugs for VW
When exactly ten pairs of plugs are installed, the Uhr unit is shown. By default it is set to 00, which disables the Uhr scrambling function and instead functions like normal jumpers between the letters selected.

An Enigma machine shipped with 10 jumpers, If more or less are used, the Uhr unit will not be shown.

To set the Uhr, press anywhere in the outer circle and while pressing, slide along the outer line in a clockwise or counter-clockwise direction until the desired Uhr setting from 00-39 is showing.

The Uhr unit was wired in such a way that when in the 00 position, is behaved like a set of plugs, A was exchanged B and viceversa, The next three settings would disrupt the symmetry of the plugs, A would be connected to K, but K would connect to Y. Settings that were multiple of 4 (4,8,12...) would regain reciprocity, however the letters swapped would be different of the plugged pairs, A and J would be swapped.

Once the desired Uhr number has been selected, the plugboard will substitute the jumped letters in a different order to the one shown in the paper.

The actual plugboard substitution can be seen if the arduino is connected to a computer, the serial function of the Arduino Enigma Simulator is set to V (verbose), and a monitor program is opened with 9600, 8, N, 1

To return to the previous screen, touch the enigma keys at the top.
We have been returned to the open machine view. The top view of the installed rotors is shown. a gap is shown below the number 5 and the number 8, corresponding to the M and P plug which did not receive plugs. Notice the screw head on the plug.

We can now change the default starting position of the machine shown upon power up by touching and dragging the rings to the right of the rotor letters.

The machine options shown to the right of the rotor mechanism can also be set.

Touching the printer text changes from No, to 0,4,5,6. No printer is shown when in NO, if 0 is shown, the characters are not grouped, otherwise they are shown in groups of 4, 5, or 6 characters.

Touching the texture text changes from No, to 1, 2. No texture is shown in the main screen when in NO, a fine texture is shown in 1, a coarse texture when in 2.

Touching the serial text changes from No, to V, to C. Nothing is shown thru the serial port when in NO. The V option (verbose) shows the wheel types and substitutions performed, the characters swapped by the plugboard/Uhr combination and the internal path the letters follow.

The C option for the serial port allows operating the coding function of the Enigma machine through the serial port. The machine has to be closed (Enigma logo showing). The operator sitting at a computer can change the wheels by sending a ! and then the wheels. For example, to set the machine to BIKE, send !BIKE, to set an M3 machine to CAB, send !CAB. Extra characters after the wheels are coded with the settings in effect at the moment. To set the wheel types, internal positions, plugs and Uhr, physical access to the Enigma screen is needed.

Touching the close text checks if all the wheels are installed and closes the machine.

The i to the right of the word Close shows the about screen.

Set the wheels to BIKE by dragging the rings until the desired letter is in the center of the screen.

Once the machine is fuly set up, close it by touching the handle on the top left corner, just above the reflector or touching the word Close.

The settings will be saved to non-volatile memory and retained when the power is off. To clear the last used settings, select different wheels.
The machine is now closed and ready to operate. The starting position of the wheels is shown and can also be changed by dragging the rings to the rights of the wheels.

If power is removed at this point, when powered up again, these settings will be reloaded, if this is not desirable, open the machine again by touching the enigma logo and change the wheels. Also, touch the plugs and scramble or remove them altogether.

To start encoding text, press the keys at the bottom of the screen, the wheels will turn, then a lamp in the middle representing the encoded character will illuminate, the same character will be simultaneously printed if the printer is enabled,
The word TEST was encoded to PAXM.

You can use Daniel Palloks Universal Enigma to verify the encoding

Universal Enigma

M4 "Shark" U-Boats

Wheels: C.Beta VII-VIII-VI (B,I,K,E) / Rings: 16 12 21 19 / Plugged: JU LY NE XT QR BA DF OS KI VW /Uhr=17

To clear the printer tape, cut it by touching it on the right most part.
We can change the Uhr setting and encode another text. To do so, touch the plugs below the keys.
Rotate the Uhr to position 29 and then click the keys above the plugs to return to the front panel view.
The word TEST was encoded with the same starting position BIKE, but the Uhr setting was different, instead of PAXM, the Uhr has now changed the cryptotext to PVLM

You are now ready to change the settings and operate the Arduino Enigma Simulator

The Arduino Enigma

This is how the machine looks like when powered by a 9V battery. It can operate as shown, or be connected to the computer via a USB cable to receive power, or it can be operated from the computer using a terminal program.



On initial power up, it defaults to an Enigma M4, with reflector B thin, Greek wheel Beta, and rotors 1,2,3, set to A, no plugs connected on the plug board. The plugboard is at the bottom, the keyboard is just above it. In the center section the lamp field is visible and the rotors are at the top.

Care was taken to ensure compatibility with the operation of enigma machines and quirks such as double stepping of the middle rotor were implemented. There is a printer tape running in between the lamp field and the rotors. 

Modifying the machine settings is done by dragging rings, touching plugs, operating a rotary wheel, the use of menus was mostly avoided to help keep the realistic feeling of operating the machine.

The next post will go through modifying the machine settings to encrypt a message,

The Enigma Machine


The enigma machine was an electro-mechanical encryption device used around World War II. The main parts are the keyboard, which moves the rotors, then applies a current through the rotors. This current is sent back through the rotors by a reflector and then it lights up a lamp in the lamp field representing the encrypted letter. The lamp stayed lit for as long as the key was pressed, once the key was released, pressing the same key again would move the rotors and another lamp would light. The reflector was its principal weakness, it made encrypting and decrypting easy, for the same wheel position, if A was encrypted to G, pressing G decrypted it back to A. This had the side effect that a letter would never encrypt to itself, a property exploited by the people who broke the enigma code.

Enhancements were made to the machine, such as a plug board that exchanged letters before sending them through the rotors.

Other enhancement were the rewirable reflector, UKW D, another was the UHR box.

The following two pages contain all the technical information needed to understand or replicate the Enigma machine

http://cryptomuseum.com/crypto/enigma/

https://en.wikipedia.org/wiki/Enigma_machine

Sunday, October 19, 2014

The Beginnings

In July, I found an excellent Enigma simulator for the Android platform and this got me thinking. Wouldn't it be something to have an Enigma simulator running in an arduino with a touchscreen. Once the shield has been mated to the arduino, it sort of resembles the actual machine.

Sure, a phone app is more practical, but this is a physical device can be interacted with, taken places, show around and be a conversation piece. It can even be powered away from a computer by using a 9V battery and a barrel jack plug. It even fits in a shirt pocket.

And this is how this got started...