DEV Community

heavenhunter
heavenhunter

Posted on

Enigma Machine : How a step on its rotor change the mapping

I'm reading through publicly available e-book "Cryptography: An Introduction (3rd Edition)" by Nigel Smart, I'm confused when reading the following section on page 50.

Excerpt from the book

Now assume that rotor one moves on one step, so A now maps to D under rotor one, B to A, C to C and D to B.

Feeling confused, I decided to dig deeper into that statement.

As described within the book, the mapping under rotor 1 can be formulized as below:

Input A B C D
Output (step=0) C A B D
Output (step=1) D A C B

I was expecting the output under rotor 1 after it moves one step to be a shift from its initial output, from CABD becomes either ABDC or DCAB. But to my surprise, the book says DACB instead.

The explanation available in Wikipedia is not sufficient for me, it's skipping a lot of detail. Then I see a related post in crypto.stackexchange.com that can clear up my confusion.

The correct way

My mistake is I shift the output character, but I should shift the offsets instead. If the input is A and the output is B then the offset is 1. If the input is D and the output is C then the offset is -1 (or 3 in modulo 4, because we only have 4 alphabet characters ABCD).

So we need to calculate the offsets, then shift the offset to determine the output for the following step.

Input A B C D
Output (step=0) C A B D
Offset (step=0) 2 -1 -1 0
Offset (step=1)
(shift left)
-1 -1 0 2
Output (step=1) D A C B
Explanation
(input+offset)
A-1 B-1 C+0 D+2

Offset (step=1) is obtained from shifting left (rotating) Offset (step=0). The final Output (step=1) can be obtained from applying Offset (step=1) to Input.

Now the final output is DACB, exactly matches what explained in the book 😄

Top comments (0)