Friday, May 1, 2015

Enigma - Hardware Implemented in Software

For several years, actually more like 30, I have being fascinated with the workings of the Enigma Machine.

On several occasions I have toyed with a Enigma Machine software design, but was never able to build an accurate solution. I have a very specific implementation in mind. So far, nothing has worked correctly. But, I guess, I have had a low-grade mental effort constantly considering the problem.

About three years ago, Wardy posted his build of a hardware implementation. That renewed my interest, but even with more work on my software implementation, and even with still a lot more thinking, there was no real progress.

Recently, my friend Jeff - KO7M posted his Enigma Machine implementation, I purposely did NOT read his details, because I wanted to continue with my implementation without external insight. But then again, his efforts renewing my interest in my software implementation. I still have that specific implementation in mind that I wanted to pursue.

So now, after several weeks of casual work on my software Enigma Machine, it still does NOT work as expected, some character are encoded correctly, but many others are NOT.

So, . . .

At night, sometimes I wake up,  thinking about the problem. And each night I seem to understand a little bit more.

There are several ideas that have helped me understanding the Enigma Machines operation. To simplify, I like to think of it in terms of:
  • Forget the Plug Board, it is a simple character exchange.
  • Code Wheels act like a "Fixed Wired, but Rotating PlugBoard", with simple two-letter exchanges.
  • The logic of the rotation of wheels is easier to understand if I think about it like a PlugBoard without cables plugged in, that is, character are not exchanged (i.e., A-to-A, B-to-B, etc)
  •  And, the Reflector is a simple adjacent character exchange (i.e., A-to-B, C-to-D, etc)
  • The code algorithm from the Input-to-the-Reflector, and the Reflector-to-the-Output, should mirror each other.
  • All wheel advancement and algorithms use Clock Maths.
  • Exchange of characters can be implemented after a correct wheel advancement algorithm is found and implemented.

Last Night

Last night, I suddenly woke up, . . . and had a algorithmic solution I wanted to try. In about five minutes, I had it typed in (see below).


With proper Settings, I can now correctly Encode and Decode any M3 and M4 Enigma message, as verified.


The following is the guts of my software Enigma Machine implementation:

# Get Message Character
$i = ord($m)-65;

# Inc Wheels as Necessary
@P[3] += 1;     # Increment with each character
@P[2] += ((@P[3]%26) == $Adv3_Static+1);
@P[1] += ((@P[2]%26) == $Adv2_Static+1);
@P[0] += 0;

$R3 = @P[3] % 26;
$R2 = @P[2] % 26;
$R1 = @P[1] % 26;
$R0 = @P[0] % 26;

# Do Enigma, Encode or Decode
$i = @PB[$i];
$i = (@WL3[($i+$R3)%26]+26-$R3)%26;
$i = (@WL2[($i+$R2)%26]+26-$R2)%26;
$i = (@WL1[($i+$R1)%26]+26-$R1)%26;
$i = (@WL0[($i+$R0)%26]+26-$R0)%26;
$i = @R[$i]; 
$i = (@WR0[($i+$R0)%26]+26-$R0)%26;
$i = (@WR1[($i+$R1)%26]+26-$R1)%26;
$i = (@WR2[($i+$R2)%26]+26-$R2)%26;
$i = (@WR3[($i+$R3)%26]+26-$R3)%26;
$i = @PB[$i];

# Show Light, The Encode Character
print chr($i+65);

I will post a complete listing, after I code a proper User Interface and implement Multi-Notch Wheel advancements.

There are some details of newer Enigma Machine versions that I will not try implement, but they should be simple.

Note: There are probably optimizations that could be used in the code (I am still learning python).

This has be a long, mentally challenging, and fun project. But now, I guess I need to find another  :-)

-- Home Page:

No comments:

Post a Comment