Simulating the Enigma Machine

edited March 2018 in How To...

Does anyone know of a Processing program for simulationg the WWII Enigma coding machine?

Tagged:

Answers

  • edited March 2018

    https://en.wikipedia.org/wiki/Enigma_machine#Simulators

    It's not an easy thing to replicate (for obvious reasons). Good luck.

  • I looked at it, here is my approach:

    // solution without The plugboard (Steckerbrett) 
    // without the 'reflector' (German: Umkehrwalze, meaning 'reversal rotor'),
    
    
    // https : // forum.processing.org/two/discussion/27478/simulating-the-enigma-machine#latest
    // https : // en.wikipedia.org/wiki/Enigma_machine#Rotors
    
    // Rotor data can be found in German Wikipedia: 
    // https : // de.wikipedia.org/wiki/Enigma_(Maschine)#Aufbau
    
    //        A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    //I       E K M F L G D Q V Z N T O W Y H X U S P A I B R C J
    //II      A J D K S I R U X B L H W T M C Q G Z N P Y F V O E
    //III     B D F H J L C P R T X V Z N Y E I W G A K M U S Q O
    //IV      E S O V P Z J A Y Q U I R H X L N F T G K D C M W B
    //V       V Z B R G I T Y U P S D N H L X A W M J Q O F E C K
    
    char currKeyIn='A';
    char currKeyOut='?';
    
    
    String[] rotors = {
      //A B C D E F G H I J K L M N O P Q R S T U V W X Y Z,
      "E K M F L G D Q V Z N T O W Y H X U S P A I B R C J", 
      "A J D K S I R U X B L H W T M C Q G Z N P Y F V O E", 
      "B D F H J L C P R T X V Z N Y E I W G A K M U S Q O", 
      "E S O V P Z J A Y Q U I R H X L N F T G K D C M W B", 
      "V Z B R G I T Y U P S D N H L X A W M J Q O F E C K"
    };
    
    void setup() {
    
      size(600, 600); 
    
      for (int i = 0; i<rotors.length; i++) {
        rotors [i] = rotors[i].replace(" ", "");
        //   println(rotors [i]);
      }//for
    
      println("");
      cipher();
    }//func 
    
    void draw() {
      background(0);
      text("Enigma", width/2, 20);
      text (currKeyIn, width/2-width/4, 78); 
      text (currKeyOut, width/2+width/4, 78);
    }
    
    void keyPressed() {
      if (key==ESC) {
        //ignore
      } else if (keyCode==SHIFT) {
        //ignore
      } 
      // ---
      else 
      {
        // the core
        println("");
        currKeyIn= str(key).toUpperCase().charAt(0);
        cipher();
      }
    }//func 
    
    void cipher() {
      // simultate 5 rotors: 
      currKeyOut = cipherHelper(currKeyIn, rotors[0], 0);
      currKeyOut = cipherHelper(currKeyOut, rotors[1], 1);
      currKeyOut = cipherHelper(currKeyOut, rotors[2], 2);
      currKeyOut = cipherHelper(currKeyOut, rotors[3], 3);
      currKeyOut = cipherHelper(currKeyOut, rotors[4], 4);
    }
    
    char cipherHelper(char in, String rotor, int rotorIndex) {
    
      char out='?';
      // make sure in is upper case 
      in=str(in).toUpperCase().charAt(0);
      // calculate values from 0 to 26 from in
      int numValue=int(in)-65; 
      // println (numValue); 
      // use this value as index for rotor
      out = rotor.charAt(numValue);
    
      // advance Rotor
      advanceRotor(rotorIndex);
    
      return out;
    }//func 
    
    void advanceRotor(int rotorIndex) {
    
      // advance the rotor 
      print("Advance rotor "+rotorIndex+" : "+rotors[rotorIndex]);
    
      // identify last letter and all the other letters 
      int rotorLength = rotors[rotorIndex].length(); 
      char lastLetter =  rotors[rotorIndex].charAt(rotorLength-1);
      String allLettersMinusOne = rotors[rotorIndex].substring(0, rotorLength-1);   
    
      // add last letter at the start 
      rotors[rotorIndex]= lastLetter + allLettersMinusOne;
    
      println(" -> "+rotors[rotorIndex]);
    }
    //
    
  • The rotors don't all rotate every time - the rightmost rotates every click, the one next to that rotates once when the rightmost has rotated all the way round. Etc.

    I'd stick with 3 rotors to start with.

    Plug board is easy enough to implement (transpose two letters on way in and out). As is the reflector.

    This would make a nice weekend project for someone.

  • It's almost done.

  • (I was thinking more of an interactive, graphical one, maybe like the diagrams on the Wikipedia page)

  • a code review of that first one doesn't make me feel good about it

    public void setAlphabet()
    {
    int  i = 0;
    
        rotor[i] = ' ';
        i++;
    
        rotor[i] = 'B';
        i++;
    
        rotor[i] = 'D';
        i++;
    
        rotor[i] = 'F';
        i++;
    
        rotor[i] = 'H';
        i++;
    

    ...

    the second one looks better but he has checked the classes into github...

Sign In or Register to comment.