title: How to emulate an electric signal (Enigma emulator)
body: <p>I want to emulate Enigma machine, therefore I need to emulate an electric signal.</p>
  
  <p><a href="https://i.stack.imgur.com/vDaEG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vDaEG.jpg" alt="How enigma works: plugboard and scramblers"></a></p>
  
  <p>If you're not familiar with Enigma, here's how it works: the electric signal travels from plugboard through scrabmlers. Scrabmlers rotate, so the path of the signal changes (output is different).</p>
  
  <p>So far, I've been doing it like this (this is a simplified code):</p>
  
  <pre><code>for (int i = 0; i &lt; data.length; i++)
  {
      for (int j = 0; j &lt; key.Length; j++)
      {
          // encrypting
          data[i] = scramblers[j][(byte)(data[i] + key[j])];
      }
      for (int j = 0; j &lt; key.Length; j++)
      {
          // rotating scramblers
          key[j]++;
          if (key[j] != 0) break;
      }
  }
  </code></pre>
  
  <p>As you can see, I used input (not encrypted) data as an index of an array (scrambler connection). The output (encrypted data) is a content of an array (scrambler).</p>
  
  <p>The problem is that when you queue a bit more scramblers in a row (e.g. 100,000) it's being very very slow. So I thought there must be a way to optimize this part (maybe somehow through pointers or linked array?).</p>
  
  <p>This code is currently written in C# and I know that for the best optimization it should be rewritten into C and C++ but let's just leave it like that right now.</p>
  
  <p><strong>My question is: How can I optimize the code shown above for its purpose (if even possible)?</strong></p>