# Enigma rotation example

According to wikipedia: BDZGO is the expected outcome when doing the enigma chipher on the letters AAAAA with rotors I,II,III and reflector B.

ABCDEFGHIJKLMNOPQRSTUVWXYZ | Alphabet

EKMFLGDQVZNTOWYHXUSPAIBRCJ | Rotor I wiring
AJDKSIRUXBLHWTMCQGZNPYFVOE | Rotor II wiring
BDFHJLCPRTXVZNYEIWGAKMUSQO | Rotor III wiring

YRUHQSLDPXNGOKMIEBFZCWVJAT | Reflector B wiring
ABCDEFGHIJKLMNOPQRSTUVWXYZ | Reflector B wiring


If we now track the first A:

A-E | etw to rotor I
E-A | rotor I to rotor II
A-B | rotor II to rotor III
B-R | Reflector B
R-X | rotor III to rotor II
X-V | rotor II to rotor I
V-I | rotor I to etw


So the output is I instead of B, where is my mistake?

As I noted in this earlier answer to a related question, the rotor permutations in your example are written using a shorthand notation that only shows the output alphabet of the permutation, with the implicit assumption that the input alphabet is always ABCDEFGHIJKLMNOPQRSTUVWXYZ. That is to say, your rotor descriptions:

EKMFLGDQVZNTOWYHXUSPAIBRCJ | Rotor I wiring
AJDKSIRUXBLHWTMCQGZNPYFVOE | Rotor II wiring
BDFHJLCPRTXVZNYEIWGAKMUSQO | Rotor III wiring


actually describe the following permutations:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ | Rotor I
EKMFLGDQVZNTOWYHXUSPAIBRCJ

ABCDEFGHIJKLMNOPQRSTUVWXYZ
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ | Rotor II
AJDKSIRUXBLHWTMCQGZNPYFVOE

ABCDEFGHIJKLMNOPQRSTUVWXYZ
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ | Rotor III
BDFHJLCPRTXVZNYEIWGAKMUSQO


Also, there are two other issues with your example encryption:

• You're applying the rotor permutations in the wrong order; the rotors are conventionally listed from left to right, but the wiring from the input/output to the reflector goes from right to left. So in your example, rotor III should actually be applied first, then rotor II and finally rotor I (and then the reflector and the inverse rotors in the opposite order).

• The rotors are stepped before each letter is encrypted. So if you start with all rotors in the A position, when the first letter of the message is encrypted the rightmost rotor (i.e. rotor III here) will have already rotated into the B position. This means you have to shift the letter right by one step in the alphabet before it enters the rotor, and back left by one step after it leaves the rotor.

Put all together, the correct path of the first letter is:

1. ABDC (rotor III in position B)
2. CCDD (rotor II in position A)
3. DDFF (rotor I in position A)
4. FS (reflector B)
5. SSSS (rotor I in position A, reverse)
6. SSEE (rotor II in position A, reverse)
7. EFCB (rotor III in position B, reverse)

For each rotor, there are effectively three permutations to apply: an alphabet shift to the rotor's current position (i.e. 0 steps for position A, 1 step for position B, 2 steps for position C, etc.), the fixed rotor wiring permutation and finally the reverse shift back from the rotor's position.

In the list above, I've used a dotted arrow ⤑ for the shifts and a solid arrow → for the rotor wiring permutations (and a two-headed arrow ↔ for the reflector). In this example, two of the three rotors are still in position A, so for them, the shifts have no effect, but I've shown them anyway for completeness. Also, just by chance, the wiring in rotor I happens to map the letter S to itself.

• Any idea why this could produce incorrect results after 100 letters? Jun 13, 2019 at 16:24
• @b3nj4m1n: Almost certainly because you got some detail of the wheel stepping wrong. That's the only thing in the Enigma machine that changes in the middle of a message. Perhaps you forgot to implement double stepping? Jun 13, 2019 at 16:31
• You're right. It works now. Thanks for your help. Jun 13, 2019 at 20:40