1
$\begingroup$

I'm having trouble making an implementation of Enigma Machine in VBA. My question is about implementing the rotors and their move.

To be more precise, I will show below the way that I code my Enigma Machine for the move of the rotors:

Example :

  • Rotor : I II III
  • Initial value A A A
  • Ring Rotor (set up rotors) : 1 1 1

What i have done:

Initialization:

  • Rotor III BDF ....GAK....QO

become

  • AK....QOBDF....G

Same for the rotor I and II. I'm based this on the initial value of the rotors AAA.

AT each step at the end of my code, i move by 1 to the right the rotor III (then for the rotor II when the letter V appear etc...) such that the rotor III AK...G became GAK .....

So to summarize my code :

  • Initialization of rotors (based on initial value)

  • Pass through the rotors/reflector and the reverse

  • Move by 1 to the right the rotors depending on the letter that i have

Where is my mistake? Can you give me an example of the all process?

$\endgroup$
3
  • $\begingroup$ why do you have the tag authenticated-encryption? it does not apply at all. $\endgroup$
    – kodlu
    Jun 11, 2022 at 13:20
  • $\begingroup$ this link looks pretty comprehensive: math.dartmouth.edu/~jvoight/Fa2012-295/EnigmaSimManual.pdf $\endgroup$
    – kodlu
    Jun 11, 2022 at 13:22
  • $\begingroup$ Thanks you a lot.I'm New to cryptography so that's why i put this. $\endgroup$
    – ZlatanAtm
    Jun 11, 2022 at 16:47

2 Answers 2

1
$\begingroup$

There are many accessible resources. As @kodlu has suggested, page 25 of the operation manual for the Enigma Cipher Machine Simulator by Dirk Rijmenants has all the necessary information.

Dirk Rijmenants (2022) Enigma Cipher Machine Simulator operation manual, version 7.0.6, pp 25. https://www.ciphermachinesandcryptology.com/files/Enigma%20Sim%20Manual.pdf

Your first mistake was the output of Rotor III on the right. Since ring setting is 01 (A) and indicator (reading through the window) is at A (01), the forward mapping of the right rotor, let's say $R$ is simply, $ R = \big( \begin{array} \\ \tt{ ABCDE \ FGHIJ \ KLMNO \ PQRST \ UVWXY \ Z } \\ \tt BDFHJ \ LCPRT \ XVZNY \ EIWGA \ KMUSQ \ O \end{array} \big) $, but not $ R = \big( \begin{array} \\ \tt{ ABCDE \ FGHIJ \ KLMNO \ PQRST \ UVWXY \ Z } \\ \tt AKMUS \ QOBDF \ HJLCP \ RTXVZ \ NYEIW \ G \end{array} \big) $.

An example demands a rather long answer, especially when you say all.

Computing output wire-by-wire

Let alphabet $\mathbb{A} = \{ {\tt A, B,} \cdots, {\tt Z} \}$. It has 26 letters. Let the set of integers $\mathbb{Z} = \{ \cdots, -1, 0, 1, 2, \cdots \}$ has its usual definition.

Let $+: \mathbb{A} \times \mathbb{Z} \rightarrow \mathbb{A}$ be a binary operation that shifting letter $a$ forward by $n$ places and becoming letter $b$ for $a, b \in \mathbb{A}$ and $n \in \mathbb{Z}$, i.e. $b=a+n$. For examples, ${\tt A} + (-2) = {\tt Y}$, ${\tt A} + (-1) = {\tt Z}$, ${\tt A} + 0 = {\tt A}$, ${\tt A} + 1 = {\tt B}$, ${\tt A} + 2 = {\tt C}$, ${\tt A} + 26 = {\tt A}$ and ${\tt A} + 27 = {\tt B}$.

Let $r: \mathbb{A} \rightarrow \mathbb{A}$ be a bijective mapping, i.e. $x_2=r(x_1)$ for $x_1, x_2 \in \mathbb{A}$. Suppose $r$ represents Rotor III on the right. It follows that $r(\tt{A}) = \tt{B}$, $r(\tt{B}) = \tt{D}$, $\cdots$, $r(\tt{Z}) = \tt{O}$. Since $r$ is bijective, there are unique inverses. They are $r^{-1}(\tt{B}) = \tt{A}$, $r^{-1}(\tt{D}) = \tt{B}$, $\cdots$, $r^{-1}(\tt{O}) = \tt{Z}$.

Ring setting 01 and indicator window shows A

Suppose the ring setting of rotor $r$ is 01 (A) and indicator window shows A (01). The wiring core is at its home orientation. Given an input $x_1$, output $x_2 = r(x_1)$ where $x_1, x_2 \in \mathbb{A}$.

enigma machine variable names

Effect of indicator setting

Suppose the ring setting is kept at 01 (A). The whole rotor rotates forward by one place and the indicator window shows B (02). Since the wiring core has rotated one place forward, it is a three-step process to compute output $x_2$ from input $x_1$. First, translate the input from the global coordinates system of the whole machine to the local coordinates system of the wiring core of rotor $r$. Second, compute mapping $r$. Third, translate from the local coordinates system back to the global one. It follows that $x_2 = r(x_1+1) + (-1) $. For examples,

$ \begin{array} \\ r({\tt A}+1) + (-1) = r({\tt B}) + (-1) = {\tt D} + (-1) = {\tt C} \\ r({\tt B}+1) + (-1) = r({\tt C}) + (-1) = {\tt F} + (-1) = {\tt E} \\ \vdots \\ r({\tt Z}+1) + (-1) = r({\tt A}) + (-1) = {\tt B} + (-1) = {\tt A} . \end{array}$

Effect of ring setting

Suppose the ring setting is now at 02 (B). The rotor is turned back such that the indicator window shows A (01). The wiring core is now rotated one place backward. It follows that $x_2 = r \big( x_1+(-1) \big) + 1 $. For examples,

$ \begin{array} \\ r\big( {\tt A}+(-1) \big) + 1 = r({\tt Z}) + 1 = {\tt O} + 1 = {\tt P} \\ r\big( {\tt B}+(-1) \big) + 1 = r({\tt A}) + 1 = {\tt B} + 1 = {\tt C} \\ \vdots \\ r\big( {\tt Z}+(-1) \big) + 1 = r({\tt Y}) + 1 = {\tt Q} + 1 = {\tt R} . \\ \end{array}$

Scrambler only without plugboard

Let's consider the general case of an arbitrary ring setting and an arbitrary indicator setting. Let the offset $n_{\mathrm{ring}}$ be number of places that ring setting is away from position 01 (A) in the increasing direction. Let the offset $n_{\mathrm{indicator}}$ be number of places that the indicator shown in the window is away from A (01) in the increasing direction. That is, for ring settings 02, 03, $\cdots$, 26, offset $n_{\mathrm{ring}} = 1, 2, \cdots, 25$ respectively. Also, for indicators B, C, $\cdots$, Z, offset $n_{\mathrm{indicator}} = 1, 2, \cdots, 25$ respectively. The combined effect is offset $n = n_{\mathrm{indicator}} - n_{\mathrm{ring}}$ where $n, n_{\mathrm{indicator}}, n_{\mathrm{ring}} \in \mathbb{Z}$. As a whole, the combined effect of rotor choice, ring setting, indicator shown is $x_2 = r(x_1+n) + (-n)$.

Suppose bijective mapping $m: \mathbb{A} \rightarrow \mathbb{A}$ represents Rotor II in the middle and its wiring core offset be $j \in \mathbb{Z}$. Suppose bijective mapping $l: \mathbb{A} \rightarrow \mathbb{A}$ represents Rotor I on the left and its wiring core offset be $k \in \mathbb{Z}$. Suppose mapping $u: \mathbb{A} \rightarrow \mathbb{A}$ represents Reflector UKW-B. It follows that,

$ \begin{array} \\ x_2 = r(x_1+n) + (-n) \\ x_3 = m(x_2+j) + (-j) \\ x_4 = l(x_3+k) + (-k) \\ x_5 = u(x_4) \\ x_6 = l^{-1}(x_5+k) + (-k) \\ x_7 = m^{-1}(x_6+j) + (-j) \\ x_8 = r^{-1}(x_7+n) + (-n) \\ \end{array}$

Cycle notation of groups

Suppose $L, M, R, U$ represent Rotor I on the left, Rotor II in the middle, Rotor III on the right and Reflector UKW-B in the far left respectively.

$ L = \Big( \begin{array} \\ \tt{ ABCDE \ FGHIJ \ KLMNO \ PQRST \ UVWXY \ Z } \\ \tt{ EKMFL \ GDQVZ \ NTOWY \ HXUSP \ AIBRC \ J } \end{array} \Big) $

$ M = \Big( \begin{array} \\ \tt{ ABCDE \ FGHIJ \ KLMNO \ PQRST \ UVWXY \ Z } \\ \tt{ AJDKS \ IRUXB \ LHWTM \ CQGZN \ PYFVO \ E } \end{array} \Big) $

$ R = \Big( \begin{array} \\ \tt{ ABCDE \ FGHIJ \ KLMNO \ PQRST \ UVWXY \ Z } \\ \tt{ BDFHJ \ LCPRT \ XVZNY \ EIWGA \ KMUSQ \ O } \end{array} \Big) $

$ U = \Big( \begin{array} \\ \tt{ ABCDE \ FGHIJ \ KLMNO \ PQRST \ UVWXY \ Z } \\ \tt{ YRUHQ \ SLDPX \ NGOKM \ IEBFZ \ CWVJA \ T } \end{array} \Big) $

Alternatively, the same in cycle notation. It makes manual computing easier.

$ L = \tt (AELTPHQXRU) (BKNW) (CMOY) (DFG) (IV) (JZ) (S) $

$ M = \tt (A) (BJ) (CDKLHUP) (ESZ) (FIXVYOMW) (GR) (NT) (Q) $

$ R = \tt (ABDHPEJT) (CFLVMZOYQIRWUKXSG) (N) $

$ U = \tt (AY) (BR) (CU) (DH) (EQ) (FS) (GL) (IP) (JX) (KN) (MO) (TZ) (VW) $

A simple example

Let's put that into practice. Ring settings are 01 01 01 (A A A). At time t=0, indicators shown on windows are A A Z (01 01 26). At t=1, a key is pressed and the right hand rotor is turned forward by one place. The indicators are now A A A (01 01 01). These are the rotor position for encrypting/decrypting plaintext/ciphertext at its character at position 1. Note that there is no turnover of the middle rotor at this time. Wiring core offsets $n, j, k$ are all $0$.

Suppose $x_1 = {\tt A}$. It follows that $x_2 = {\tt B}, x_3 = {\tt J}, x_4 = {\tt Z}, x_5 = {\tt T}, x_6 = {\tt L}, x_7 = {\tt K}$ and $x_8 = {\tt U}$.

Suppose $x_1 = {\tt B}$. It follows that $x_2 = {\tt D}, x_3 = {\tt K}, x_4 = {\tt N}, x_5 = {\tt K}, x_6 = {\tt B}, x_7 = {\tt J}$ and $x_8 = {\tt E}$.

$\vdots$

Suppose $x_1 = {\tt Z}$. It follows that $x_2 = {\tt O}, x_3 = {\tt M}, x_4 = {\tt O}, x_5 = {\tt M}, x_6 = {\tt C}, x_7 = \tt{P}$ and $x_8 = {\tt H}$.

Computing in an ordered 26-tuple

Let $L, M, R, U$ be elements of a non-commutative group $G$. Since $G$ is a group, the inverses $L^{-1}, M^{-1}, R^{-1} \in G$ exist and are unique. Its multiplicative identity is $ I = \Big( \begin{array} \\ \tt{ ABCDE \ FGHIJ \ KLMNO \ PQRST \ UVWXY \ Z } \\ \tt{ ABCDE \ FGHIJ \ KLMNO \ PQRST \ UVWXY \ Z } \end{array} \Big) $. We use juxtaposition to represent group multiplication. Set $\rho \in G$ be cyclic permutation and $ \rho = \Big( \begin{array} \\ \tt{ ABCDE \ FGHIJ \ KLMNO \ PQRST \ UVWXY \ Z } \\ \tt{ BCDEF \ GHIJK \ LMNOP \ QRSTU \ VWXYZ \ A } \end{array} \Big) $.

It follows that $ \rho \rho = \rho^2 = \Big( \begin{array} \\ \tt{ ABCDE \ FGHIJ \ KLMNO \ PQRST \ UVWXY \ Z } \\ \tt{ CDEFG \ HIJKL \ MNOPQ \ RSTUV \ WXYZA \ B } \end{array} \Big) $, $ \rho^{-1} = \Big( \begin{array} \\ \tt{ ABCDE \ FGHIJ \ KLMNO \ PQRST \ UVWXY \ Z } \\ \tt{ ZABCD \ EFGHI \ JKLMN \ OPQRS \ TUVWX \ Y } \end{array} \Big) $ and $ \rho^0 = I $.

Scrambler only without plugboard

Suppose $R$ represents Rotor III in the right. The mapping $R: \mathbb{A}^{26} \rightarrow \mathbb{A}^{26}$ can be considered as mapping $r$ on steroid by mapping all 26 letters in one go.

Let $X_1, X_2, X_3, X_4, X_5, X_6, X_7, X_8 \in \mathbb{A}^{26}$ are ordered 26-tuple vectors. For example,

$ X_1 = \left[ \begin{array} \\ \tt{A} \\ \tt{B} \\ \vdots \\ \tt{Z} \end{array} \right] = \left[ \tt A \ B \ \cdots \ Z \right] ^ T$.

Therefore,

$ \begin{array} \\ X_2 = (X_1) \rho^n R \rho^{-n} \\ X_3 = (X_2) \rho^j M \rho^{-j} \\ X_4 = (X_3) \rho^k L \rho^{-k} \\ X_5 = (X_4) U \\ X_6 = (X_5) \rho^k L^{-1} \rho^{-k} \\ X_7 = (X_6) \rho^j M^{-1} \rho^{-j} \\ X_8 = (X_7) \rho^n R^{-1} \rho^{-n} \\ \end{array}$

The combined effect the rotors and a reflector is often called a scrambler $S \in G$ or a letchworth scrambler. The latter is more relevant to decryption using the Bombe.

$S = \big( \rho^n R \rho^{-n} \big) \big( \rho^j M \rho^{-j} \big) \big( \rho^k L \rho^{-k} \big) U \big( \rho^k L^{-1} \rho^{-k} \big) \big( \rho^j M^{-1} \rho^{-j} \big) \big( \rho^n R^{-1} \rho^{-n} \big)$

The whole machine including the plugboard

Apply mapping $S$ to vector variable $X_1$, we have $X_8 = ( X_1 ) S $ where $X_1, X_8 \in \mathbb{A}^{26}$. To complete the Enigma Machine, you will need plugboard $P$. The construction of the plugboard makes it a self-inverse such that $P^{-1} = P$ where $P, P^{-1} \in G$. Suppose $E \in G$ is the whole machine $E = P S P^{-1} = P S P$. Apply mapping $E$ to the vector variable $X$, we have $Y = (X) E$ where $X, Y \in \mathbb{A}^{26}$.

Go back to your example that ring setting is 01 01 01 (A A A). At t=0, indicators shown in windows are A A Z (01 01 26). At time t=1, indicators are A A A (01 01 01). These are the rotor settings for encrypting/decrypting plaintext/ciphertext at its character at position 1. Note that there is no turnover of the middle rotor at this time. Wiring core offsets $n, j, k$ are all $0$. There was no mention of the plugboard and I assume none was set. That makes $P = I$ the multiplicative identity. Therefore, in your example,

$ \begin{array} \\ E &= P \big( \rho^n R \rho^{-n} \big) \big( \rho^j M \rho^{-j} \big) \big( \rho^k L \rho^{-k} \big) U \big( \rho^k L^{-1} \rho^{-k} \big) \big( \rho^j M^{-1} \rho^{-j} \big) \big( \rho^n R^{-1} \rho^{-n} \big) P \\ &= I \big( I R I \big) \big( I M I \big) \big( I L I \big) U \big( I L^{-1} I \big) \big( I M^{-1} I \big) \big( I R^{-1} I \big) I \\ &= R M L U L^{-1} M^{-1} R^{-1} \\ &= \tt (AU) (BE) (CJ) (DO) (FT) (GP) (HZ) (IW) (KN) (LS) (MR) (QV) (XY). \end{array}$

Evaluate $Y = (X)E$ by setting $X=I$. Given that $X = \left[ \tt{A B \cdots Z} \right]^T$, it gives $Y = \left[ \tt{U E \cdots H} \right]^T$ which is the same result as before using wire-by-wire method.

(Note: One more excursion, it happens that $S$ is its self inverse $S^{-1} = S$. It follows that $E$ is its self inverse too $E^{-1} = E$. This gives the property that one uses the same machine setting for both encryption and decryption. Let's get back the problem at hand.)

A further example

There is an example on page 6 in Rijmenants (2022). An Army Enigma I with reflector UKW-B was used. Rotor order is I II V. We have, \begin{array} \\ U &= \tt (AY) (BR) (CU) (DH) (EQ) (FS) (GL) (IP) (JX) (KN) (MO) (TZ) (VW) \\ P &= \tt (BG) (CD) (ER) (FV) (HN) (JK) (LM) (OP) (TY) (UI) (A) (Q) (S) (W) (X) (Z) \\ R &= \tt (AVOLDRWFIUQ) (BZKSMNHYC) (EGTJPX) \\ M &= \tt (A) (BJ) (CDKLHUP) (ESZ) (FIXVYOMW) (GR) (NT) (Q) \\ L &= \tt (AELTPHQXRU) (BKNW) (CMOY) (DFG) (IV) (JZ) (S) \end{array}

Ring setting is 06 22 14 (F V N). Message key is X W B (24 23 02), i.e. what can be seen in the indicator window at time t=0. At t=0, $n = (02-01) - (14-01) = 14$ (mod 26), $j = (23-01) - (22-01) = 1$ (mod 26) and $k = (24-01) - (06-01) = 18$ (mod 26). We will use subscript $t$ to represent a particular time or character position of the ciphertext/plaintext, e.g. $S_t, E_t$.

At t=1, indicator is X W C and $ \begin{array} \\ E_1 &= P \big( \rho^{15} R \rho^{-15} \big) \big( \rho M \rho^{-1} \big) \big( \rho^{18} L \rho^{-18} \big) U \big( \rho^{18} L^{-1} \rho^{-18} \big) \big( \rho M^{-1} \rho^{-1} \big) \big( \rho^{15} R^{-1} \rho^{-15} \big) P \\ &= P S_1 P \\ &= \tt (AU) (BS) (CI) (DQ) (EM) (FY) (GX) (HZ) (JW) (KL) (NT) (OP) (RV). \end{array} $

At t=2, indicator is X W D and $ \begin{array} \\ E_2 &= P \big( \rho^{16} R \rho^{-16} \big) \big( \rho M \rho^{-1} \big) \big( \rho^{18} L \rho^{-18} \big) U \big( \rho^{18} L^{-1} \rho^{-18} \big) \big( \rho M^{-1} \rho^{-1} \big) \big( \rho^{16} R^{-1} \rho^{-16} \big) P \\ &= P S_2 P \\ &= \tt (AF) (BE) (CZ) (DU) (GS) (HK) (IX) (JQ) (LV) (MY) (NT) (OP) (RW). \end{array} $

$\vdots$

At t=24, indicator is X W Z and $ \begin{array} \\ E_{24} &= P \big( \rho^{12} R \rho^{-12} \big) \big( \rho M \rho^{-1} \big) \big( \rho^{18} L \rho^{-18} \big) U \big( \rho^{18} L^{-1} \rho^{-18} \big) \big( \rho M^{-1} \rho^{-1} \big) \big( \rho^{12} R^{-1} \rho^{-12} \big) P \\ &= P S_{24} P \\ &= \tt (AS) (BN) (CH) (DP) (EX) (FY) (GR) (IJ) (KZ) (LM) (OV) (QW) (TU). \end{array} $

At t=25, indicator is X X A and $ \begin{array} \\ E_{25} &= P \big( \rho^{13} R \rho^{-13} \big) \big( \rho^2 M \rho^{-2} \big) \big( \rho^{18} L \rho^{-18} \big) U \big( \rho^{18} L^{-1} \rho^{-18} \big) \big( \rho^2 M^{-1} \rho^{-2} \big) \big( \rho^{13} R^{-1} \rho^{-13} \big) P \\ &= P S_{25} P \\ &= \tt (AY) (BR) (CQ) (DX) (EO) (FP) (GH) (IW) (JN) (KU) (LT) (MZ) (SV). \end{array} $

$\vdots$

At t=44, indicator is X X T and $ \begin{array} \\ E_{44} &= P \big( \rho^{32} R \rho^{-32} \big) \big( \rho^2 M \rho^{-2} \big) \big( \rho^{18} L \rho^{-18} \big) U \big( \rho^{18} L^{-1} \rho^{-18} \big) \big( \rho^2 M^{-1} \rho^{-2} \big) \big( \rho^{32} R^{-1} \rho^{-32} \big) P \\ &= P S_{44} P \\ &= \tt (AY) (BR) (CS) (DF) (EO) (GN) (HK) (IT) (JL) (MV) (PU) (QW) (XZ). \end{array} $

Below are some example output.

t= 1, Q > Q 17+15= 6 F > I  9-15=20,       20+ 1=21 U > P 16- 1=15,
            15+18= 7 G > D  4-18=12 L >> G  7+18=25 Y > O 15-18=23,
            23+ 1=24 X > I  9-1= 8,         8+15=23 W > R 18-15= 3 C > D
t= 2, B > G  7+16=23 W > F  6-16=16,       16+ 1=17 Q > Q 17- 1=16,
            16+18= 8 H > Q 17-18=25 Y >> A  1+18=19 S > S 19-18= 1,
             1+ 1= 2 B > J 10- 1= 9,        9+16=25 Y > H  8-16=18 R > E
...
t=24, D > C  3+12=15 O > L 12-12= 0,        0+ 1= 1 A > A  1- 1= 0,
             0+18=18 R > U 21-18= 3 C >> U 21+18=13 M > C  3-18=11,
            11+ 1=12 L > K 11- 1=10,       10+12=22 V > A  1-12=15 O > P
t=25, P > O 15+13= 2 B > Z  0-13=13,       13+ 2=15 O > M 13- 2=11,
            11+18= 3 C > M 13-18=21 U >> C  3+18=21 U > R 18-18= 0,
             0+ 2= 2 B > J 10- 2= 8,        8+13=21 U > I  9-13=22 V > F
...
t=44, Z > Z  0+ 6= 6 F > I  9- 6= 3,        3+ 2= 5 E > S 19- 2=17,
            17+18= 9 I > V 22-18= 4 D >> H  8+18= 0 Z > J 10-18=18,
            18+ 2=20 T > N 14- 2=12,       12+ 6=18 R > D  4- 6=24 X > X

                     1     1     2     2     3     3     4    4
           1   5     0     5     0     5     0     5     0    4
X  -       QBLTW LDAHH YEOEF PTWYB LENDP MKOXL DFAMU DWIJD XRJZ <- ciphertext
X1 -       QGMYW MCANN TRPRV OYWTG MRHCO LJPXM CVALI CWUKC XEKZ

X8 -       CREVI RNERE USYYP CXCRE JALOV BRNYW RUYRE XCPRH UYZX
Y  -       DERFU EHRER ISTTO DXDER KAMPF GEHTW EITER XDOEN ITZX <- plaintext
$\endgroup$
1
$\begingroup$

Not sure if you figured this out. The process flow without the plugboard is more like;

  1. Press key on keyboard.
  2. Right rotor moves one step. 2a) If right rotor notch at prowl, rotate middle rotor 2b) If middle rotor notch at prowl, rotate left rotor 2c) Signal from keyboard moves to rotor right.
  3. Signal move from rotor right to rotor middle.
  4. Signal moves from rotor middle to rotor left.
  5. Signal moves from rotor left into reflector.
  6. Signal moves from reflector to rotor left.
  7. Signal moves from rotor left to rotor middle.
  8. Signal moves from rotor middle to rotor right.
  9. Signal moves from rotor right to lamp board.

The movement of the rotors occurs at the pressing of the key, at the beginning of the flow. Then the signal moves through the rotors and back again.

Rotor input side = ABCDEFG...Z Rotor output side = JGFTXSW...I

When a key is pressed (lets say A), the right rotor moves from A to B. The signal A enter the rotor at B and exits at G.

Hope this helps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.