<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: heavenhunter</title>
    <description>The latest articles on DEV Community by heavenhunter (@heavenhunter).</description>
    <link>https://dev.to/heavenhunter</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3499214%2F04371e37-4e40-4c84-93af-9ccc48378f55.png</url>
      <title>DEV Community: heavenhunter</title>
      <link>https://dev.to/heavenhunter</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/heavenhunter"/>
    <language>en</language>
    <item>
      <title>Enigma Machine : How a step on its rotor change the mapping</title>
      <dc:creator>heavenhunter</dc:creator>
      <pubDate>Sat, 13 Sep 2025 09:48:41 +0000</pubDate>
      <link>https://dev.to/heavenhunter/enigma-machine-how-a-step-on-its-rotor-change-the-mapping-24nf</link>
      <guid>https://dev.to/heavenhunter/enigma-machine-how-a-step-on-its-rotor-change-the-mapping-24nf</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fffsq2vghs4ix372d305b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fffsq2vghs4ix372d305b.png" alt="Excerpt from the book" width="800" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Feeling confused, I decided to dig deeper into that statement.&lt;/p&gt;

&lt;p&gt;As described within the book, the mapping under rotor 1 can be formulized as below:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;Input&lt;/th&gt;
    &lt;td&gt;A&lt;/td&gt;
    &lt;td&gt;B&lt;/td&gt;
    &lt;td&gt;C&lt;/td&gt;
    &lt;td&gt;D&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;Output (step=0)&lt;/th&gt;
    &lt;td&gt;C&lt;/td&gt;
    &lt;td&gt;A&lt;/td&gt;
    &lt;td&gt;B&lt;/td&gt;
    &lt;td&gt;D&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;Output (step=1)&lt;/th&gt;
    &lt;td&gt;D&lt;/td&gt;
    &lt;td&gt;A&lt;/td&gt;
    &lt;td&gt;C&lt;/td&gt;
    &lt;td&gt;B&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;p&gt;The explanation available in Wikipedia is not sufficient for me, it's skipping a lot of detail. Then I see a related post in &lt;a href="https://crypto.stackexchange.com/a/71233/52890" rel="noopener noreferrer"&gt;crypto.stackexchange.com&lt;/a&gt; that can clear up my confusion.&lt;/p&gt;

&lt;h2&gt;
  
  
  The correct way
&lt;/h2&gt;

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

&lt;p&gt;So we need to calculate the offsets, then shift the offset to determine the output for the following step.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;Input&lt;/th&gt;
    &lt;td&gt;A&lt;/td&gt;
    &lt;td&gt;B&lt;/td&gt;
    &lt;td&gt;C&lt;/td&gt;
    &lt;td&gt;D&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;Output (step=0)&lt;/th&gt;
    &lt;td&gt;C&lt;/td&gt;
    &lt;td&gt;A&lt;/td&gt;
    &lt;td&gt;B&lt;/td&gt;
    &lt;td&gt;D&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;Offset (step=0)&lt;/th&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;-1&lt;/td&gt;
    &lt;td&gt;-1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;Offset (step=1)&lt;br&gt;(shift left)&lt;/th&gt;
    &lt;td&gt;-1&lt;/td&gt;
    &lt;td&gt;-1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;Output (step=1)&lt;/th&gt;
    &lt;td&gt;D&lt;/td&gt;
    &lt;td&gt;A&lt;/td&gt;
    &lt;td&gt;C&lt;/td&gt;
    &lt;td&gt;B&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;Explanation&lt;br&gt;(input+offset)&lt;/th&gt;
    &lt;td&gt;A-1&lt;/td&gt;
    &lt;td&gt;B-1&lt;/td&gt;
    &lt;td&gt;C+0&lt;/td&gt;
    &lt;td&gt;D+2&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;Offset (step=1)&lt;/code&gt; is obtained from shifting left (rotating) &lt;code&gt;Offset (step=0)&lt;/code&gt;. The final &lt;code&gt;Output (step=1)&lt;/code&gt; can be obtained from applying &lt;code&gt;Offset (step=1)&lt;/code&gt; to &lt;code&gt;Input&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now the final output is &lt;code&gt;DACB&lt;/code&gt;, exactly matches what explained in the book 😄&lt;/p&gt;

</description>
      <category>cryptography</category>
      <category>encryption</category>
      <category>enigma</category>
    </item>
  </channel>
</rss>
