ranieri
ranieri

Reputation: 2088

Enigma replica not yielding expected result

I'm trying to mimic the WWII Enigma machine in Python code, following some reading in Wikipedia and other dedicated sources. Currently, I got a machine that scrambles input text and can revert the scrambled output back to input if the configuration is reset. But the problem is the code is not yielding the expected results from the very same configuration that is on Wikipedia:

With the rotors I, II and III (from left to right), wide B-reflector, all ring
settings in A-position, and start position AAA, typing AAAAA will produce the
encoded sequence BDZGO.

If I try to cipher AAAAA using the rotor and reflector configuration I find on this list, I get the ciphered text EVRDW, opposed to BDZGO that is expected.

Since the text is being ciphered and can be deciphered correctly, I believe the mistake is somewhere in the texts' explanation (or my understanding of them), but I couldn't find where my code is not following the operation of an Enigma machine.

Link to the code

Upvotes: 5

Views: 864

Answers (1)

Johan
Johan

Reputation: 1703

I think you are forgetting to translate from ring setting to rotor position before passing the character to the next ring, step 4 below. It seems to me that you only take into account the acctual rotor encoding and pass that on to the next ring. The proper way is

1) letter comes in
2) translate letter using rotor setting offset
3) translate offest letter using the internal ring wiring to get encoded letter
4) translate encoded letter using the rotor setting (offset)
5) pass offsetted encoded letter to next ring

A couple of examples

For example using Rotor I (ring setting A-01, rotor pos A)

Starting position
Pos:          ABCDEFGHIJKLMNOPQRSTUVWXYZ

Ring setting: ABCDEFGHIJKLMNOPQRSTUVWXYZ
              ||||||||||||||||||||||||||
              EKMFLGDQVZNTOWYHXUSPAIBRCJ

Rotor pos:    ABCDEFGHIJKLMNOPQRSTUVWXYZ
              ||||||||||||||||||||||||||
              ABCDEFGHIJKLMNOPQRSTUVWXYZ

Passsing an A to this ring translates to an E, after this the rotor and ring rotates

After one rotation
Pos:          ABCDEFGHIJKLMNOPQRSTUVWXYZ

Ring setting: BCDEFGHIJKLMNOPQRSTUVWXYZA
              ||||||||||||||||||||||||||
              KMFLGDQVZNTOWYHXUSPAIBRCJE

Rotor pos:    BCDEFGHIJKLMNOPQRSTUVWXYZA
              ||||||||||||||||||||||||||
              ABCDEFGHIJKLMNOPQRSTUVWXYZ

so next A becomes an J

Now lets mix with ring settings, lets have the ring setting B-02 (i.e. rotated one step) but have the rotor pos in A

Starting position
Pos:          ABCDEFGHIJKLMNOPQRSTUVWXYZ

Ring setting: ZABCDEFGHIJKLMNOPQRSTUVWXY
              ||||||||||||||||||||||||||
              JEKMFLGDQVZNTOWYHXUSPAIBRC

Rotor pos:    ZABCDEFGHIJKLMNOPQRSTUVWXY
              ||||||||||||||||||||||||||
              ABCDEFGHIJKLMNOPQRSTUVWXYZ

So first A becomes a K then the ring rotates

After one rotation
Pos:          ABCDEFGHIJKLMNOPQRSTUVWXYZ

Ring setting: ABCDEFGHIJKLMNOPQRSTUVWXYZ
              ||||||||||||||||||||||||||
              EKMFLGDQVZNTOWYHXUSPAIBRCJ

Rotor pos:    ABCDEFGHIJKLMNOPQRSTUVWXYZ
              ||||||||||||||||||||||||||
              ABCDEFGHIJKLMNOPQRSTUVWXYZ

so the next A becomes a E

Now lets mix with rotor settings, lets have the ring setting A-01 but have the rotor pos in B

Starting position
Pos:          ABCDEFGHIJKLMNOPQRSTUVWXYZ

Ring setting: ABCDEFGHIJKLMNOPQRSTUVWXYZ
              ||||||||||||||||||||||||||
              EKMFLGDQVZNTOWYHXUSPAIBRCJ

Rotor pos:    BCDEFGHIJKLMNOPQRSTUVWXYZA
              ||||||||||||||||||||||||||
              ABCDEFGHIJKLMNOPQRSTUVWXYZ

The first A then yields rotor output D, after rotation the setup is

After one rotation
Pos:          ABCDEFGHIJKLMNOPQRSTUVWXYZ

Ring setting: BCDEFGHIJKLMNOPQRSTUVWXYZA
              ||||||||||||||||||||||||||
              KMFLGDQVZNTOWYHXUSPAIBRCJE

Rotor pos:    CDEFGHIJKLMNOPQRSTUVWXYZAB
              ||||||||||||||||||||||||||
              ABCDEFGHIJKLMNOPQRSTUVWXYZ

so the second A yields an rotor output I

A word on the rotor settings

To ensure a correct encryption/decryption you need to make 2 settings on each rotor

  1. rotor offset
  2. ring setting

The rotor offset is simply how the complete rotor is rotated at the start (what letter is shown in the little window). Thus A position means that the rotor has the letter A showing, B position means a B et.c. These are automatically changed during decoding/encoding but needs to be correct when starting otherwise there will be garbage coming out

The rotor setting on the otherhand changes the internal wirings relative to the external connections. So if this is in an A-01 position, then a signal coming in on the external A connection gets routed to the rotors internal A connection and then coded to an E internally (given rotor I) and transmitted on the external E line. If the setting is B-02 that means that a signal coming in on the external A connection gets routed to the rotors internal Z connection and thus coded to a J internally, then due to the setting this J gets trasmitted on the external K connection (thus the next rotor would see a K)

What was confusing for me with this was all the positions. I think of them as external and internal. Each rotor has two external (ingoing and outgoing), these never changes position. Then each rotor has two internal positions, one controlled by the rotor offset and the other by the rotor setting, these are changed at setup of the device and the rotor offset is changed during the encoding/decoding process as part of normal operations (the rotor rotates)

Long explanation, hope it helped more that it hurted and that you get the issue sorted out.

Upvotes: 7

Related Questions