Exemplo n.º 1
0
 /// <summary>
 /// Creates Enigma motors
 /// </summary>
 private static void BuildRotors()
 {
     // Rotor 1
     int[] rotor1Cipher  = { 4, 10, 12, 5, 11, 6, 3, 16, 21, 25, 13, 19, 14, 22, 24, 7, 23, 20, 18, 15, 0, 8, 1, 17, 2, 9 };
     int[] rotor1Notches = { 16 };
     _rotor1 = new Rotor(rotor1Cipher, rotor1Notches);
     // Rotor 2
     int[] rotor2Cipher  = { 0, 9, 3, 10, 18, 8, 17, 20, 23, 1, 11, 7, 22, 19, 12, 2, 16, 6, 25, 13, 15, 24, 5, 21, 14, 4 };
     int[] rotor2Notches = { 4 };
     _rotor2 = new Rotor(rotor2Cipher, rotor2Notches);
     // Rotor 3
     int[] rotor3Cipher  = { 1, 3, 5, 7, 9, 11, 2, 15, 17, 19, 23, 21, 25, 13, 24, 4, 8, 22, 6, 0, 10, 12, 20, 18, 16, 14 };
     int[] rotor3Notches = { 21 };
     _rotor3 = new Rotor(rotor3Cipher, rotor3Notches);
     // Rotor 4
     int[] rotor4Cipher  = { 4, 18, 14, 21, 15, 25, 9, 0, 24, 16, 20, 8, 17, 7, 23, 11, 13, 5, 19, 6, 10, 3, 2, 12, 22, 1 };
     int[] rotor4Notches = { 9 };
     _rotor4 = new Rotor(rotor4Cipher, rotor4Notches);
     // Rotor 5
     int[] rotor5Cipher  = { 21, 25, 1, 17, 6, 8, 19, 24, 20, 15, 18, 3, 13, 7, 11, 23, 0, 22, 12, 9, 16, 14, 5, 4, 2, 10 };
     int[] rotor5Notches = { 12, 25 };
     _rotor5 = new Rotor(rotor5Cipher, rotor5Notches);
     // Rotor 6
     int[] rotor6Cipher  = { 9, 15, 6, 21, 14, 20, 12, 5, 24, 16, 1, 4, 13, 7, 25, 17, 3, 10, 0, 18, 23, 11, 8, 2, 19, 22 };
     int[] rotor6Notches = { 12, 25 };
     _rotor6 = new Rotor(rotor6Cipher, rotor6Notches);
     // Rotor 7
     int[] rotor7Cipher  = { 13, 25, 9, 7, 6, 17, 2, 23, 12, 24, 18, 22, 1, 14, 20, 5, 0, 8, 21, 11, 15, 4, 10, 16, 3, 19 };
     int[] rotor7Notches = { 12, 25 };
     _rotor7 = new Rotor(rotor7Cipher, rotor7Notches);
     // Rotor 8
     int[] rotor8Cipher  = { 5, 10, 16, 7, 19, 11, 23, 14, 2, 1, 9, 18, 15, 3, 25, 17, 0, 12, 4, 22, 13, 8, 20, 24, 6, 21 };
     int[] rotor8Notches = { 12, 25 };
     _rotor8 = new Rotor(rotor8Cipher, rotor8Notches);
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Rotor rL = new Rotor(FixedMechanicRotor.ROTOR_II);  //II has notch F
            Rotor rM = new Rotor(FixedMechanicRotor.ROTOR_IV);  //IV has notch K
            Rotor rR = new Rotor(FixedMechanicRotor.ROTOR_V);   //V  has notch A

            //Following WW2 Convention, it is Left-Mid-Right e.g. II IV V
            Rotor[] rotors = { rL, rM, rR };


            Reflector re   = new Reflector(FixedMechanicReflector.REFLECTOR_B);
            Plugboard plug = new Plugboard(new String[] {
                "AV", "BS", "CG", "DL", "FU", "HZ", "IN", "KM", "OW", "RX"
            });                                                                               //Barbarosa
            WindowSetting initialSetting = new WindowSetting('B', 'L', 'A');
            RingSetting   ringPositions  = new RingSetting(2, 21, 12);



            //an example of naming hassle because Enigma is both namespace and class
            Enigma.representation.Enigma enigma = new Enigma.representation.Enigma(rotors, re, plug, ringPositions, initialSetting);



            string myfile = "C:\\Users\\ToshiW\\Documents\\Visual Studio 2012\\Projects\\Enigma\\Enigma\\Resources\\BarbarosaCiphertext.txt";
            string input  = Utility.FileToString(myfile);
            //Console.WriteLine(readResult);
            //Console.ReadLine();

            //Let Enigma do its thing
            string result = enigma.encryptString(input);

            Console.WriteLine(result);
            Console.ReadLine();
        }
Exemplo n.º 3
0
        //method to change the rotors that are used
        public void ChangeRotors(string rot1,string rotName1,char rotNotch1,
		                         string rot2,string rotName2,char rotNotch2,
		                         string rot3,string rotName3,char rotNotch3)
        {
            lblRotorS.Text = rotName1;
            lblRotorM.Text = rotName2;
            lblRotorD.Text = rotName3;
            rr = null;
            rm = null;
            rl = null;

            rr = new Rotor(rot3,lblRotor1,rotNotch3);
            rm = new Rotor(rot2,lblRotor2,rotNotch2);
            rl = new Rotor(rot1,lblRotor3,rotNotch1);

            rr.ResetOffset();
            rm.ResetOffset();
            rl.ResetOffset();

            rr.SetNextRotor(rm);
            rm.SetNextRotor(rl);
            rl.SetNextRotor(reflector);
            rm.SetPreviousRotor(rr);
            rl.SetPreviousRotor(rm);
            reflector.SetPreviousRotor(rl);

            lblRotor1.Text = "A";
            lblRotor2.Text = "A";
            lblRotor3.Text = "A";
        }
Exemplo n.º 4
0
 public Spindle(Rotor rotor1, Rotor rotor2, Rotor rotor3)
 {
     this.rotors = new List <Rotor>();
     this.rotors.Add(rotor1);
     this.rotors.Add(rotor2);
     this.rotors.Add(rotor3);
 }
Exemplo n.º 5
0
        /*
         * It takes first positions of rotors and its
         * numbers that STARTS FROM 1!
         *
         * commutatorsString is string like AB CD OP
         * and so on...
         */
        public EnigmaMachine(
            RotorTypes firstRotorNumber, int firstRotorStartPosition,
            RotorTypes secondRotorNumber, int secondRotorStartPosition,
            RotorTypes thirdRotorNumber, int thirdRotorStartPosition,
            ReflectorTypes reflectorNumber,
            string commutatorsString
            )
        {
            /* if yout want to try make all rotors in array
             * can try to uncomment it and rewrite CipherChar
             *
             * m_rotors = new Rotor[]
             * {
             *  RotorFactory((int)firstRotorNumber,   firstRotorStartPosition),
             *  RotorFactory((int)secondRotorNumber,  secondRotorStartPosition),
             *  RotorFactory((int)thirdRotorNumber,   thirdRotorStartPosition),
             * };
             */

            m_first  = RotorFactory((int)firstRotorNumber, firstRotorStartPosition);
            m_second = RotorFactory((int)secondRotorNumber, secondRotorStartPosition);
            m_third  = RotorFactory((int)thirdRotorNumber, thirdRotorStartPosition);

            m_reflector = ReflectorFactory((int)reflectorNumber);

            m_commutators = new PlugBoard(commutatorsString);
        }
Exemplo n.º 6
0
        public EnigmaMachine()
        {
            //转子对应规则
            rotors = new Rotor[] {
                new Rotor(new int[] { 18, 25, 4, 24, 1, 2, 14, 10, 0, 11, 12, 8, 3, 20, 23, 7, 16, 21, 13, 15, 5, 22, 9, 17, 19, 6 }),
                new Rotor(new int[] { 7, 25, 2, 24, 11, 10, 13, 12, 9, 20, 16, 22, 6, 5, 4, 15, 19, 14, 23, 1, 0, 17, 21, 3, 18, 8 }),
                new Rotor(new int[] { 21, 9, 3, 19, 22, 13, 11, 15, 10, 2, 17, 18, 23, 12, 6, 16, 4, 0, 7, 8, 14, 5, 24, 25, 1, 20 }),
                new Rotor(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 })
            };
            //默认转子
            rotor1 = rotors[0];
            rotor2 = rotors[1];
            rotor3 = rotors[2];
            //默认反射板
            reflector = new Reflector(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 });
            //连线板
            plugboard = new Plugboard();

            alphaToNumber = new Dictionary <char, int>();
            foreach (char letter in alphabet)
            {
                alphaToNumber[letter] = alphabet.IndexOf(letter);
            }

            rotorPosition = 0;
            prevLength    = 0;
        }
Exemplo n.º 7
0
 // cinstructor
 public Enigma(Rotor R1, Rotor R2, Rotor R3, Plugboard P, Reflector Ref)
 {
     Rotor1    = R1;
     Rotor2    = R2;
     Rotor3    = R3;
     Plugboard = P;
     Reflector = Ref;
 }
Exemplo n.º 8
0
 //public Rotor(string layout, Label lbl, char notchPos)
 public Rotor(string layout, char notchPos)
 {
     this.layout = layout;
     this.previous = previous;
     this.next = next;
     //this.lbl = lbl;
     this.notchPos = notchPos;
     offset = 0;
 }
Exemplo n.º 9
0
 public Rotor(string layout,Label lbl,char notchPos, string nm)
 {
     this.layout = layout;
     this.previous = previous;
     this.next = next;
     this.lbl = lbl;
     this.notchPos = notchPos;
     this.name = nm;
     offset = 0;
 }
Exemplo n.º 10
0
 // function that checkes the turnover notch of the rotors
 void Didnotch(Rotor r)
 {
     if (Rotor1.getOffset() == Rotor1.getNotch() - 1 || Rotor2.getOffset() == Rotor2.getNotch() - 1)
     {
         if (Rotor2.getOffset() == Rotor2.getNotch() - 1)
         {
             Rotor3.setOffset(1);
         }
         Rotor2.setOffset(1);
     }
     Rotor1.setOffset(1);
 }
Exemplo n.º 11
0
        private void cmbRightRotor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ShowHideAvilableRotors();
            rightRotor.Offset           = 'A';
            rightRotor.InnerRingSetting = 'A';
            int selectedIndex = ((ComboBox)sender).SelectedIndex;

            rightRotor = rotors[selectedIndex];
            SetRotorsPosition();
            UpdateOffsetLabel();
            ResetText();
        }
        /// <summary>
        /// Checks if the rotor needs to be advanced
        /// </summary>
        /// <param name="r">the previous rotor</param>
        /// <returns></returns>
        private bool CheckToAdvance(Rotor r)
        {
            // stores the list of values that rotate the rotor
            List <char> values = RotorInformation.RotateValues(r.RotorIndex());

            // if the list contains the previous letter, return true
            if (values.Contains(r.LetterInAlpha(25)))
            {
                return(true);
            }

            // does not need to be rotated
            return(false);
        }
Exemplo n.º 13
0
 void Opstarten()
 {
     Rotors = new Rotor[5];
     for (int i = 0; i < 5; i++)
     {
         Rotors[i] = new Rotor();
         if (i < 3)
         {
             Rotors[i].myCode = i + 1;
         }
         Rotors[i].EncryptionIndex = (byte)i;
     }
     StekkerDoos = new Stecker();
     Spiegel     = new Spiegel();
 }
Exemplo n.º 14
0
 public Enigma(int passwordLength, params Rotor[] rotors)
 {
     if (rotors.Length != passwordLength)
     {
         Rotors = new Rotor[passwordLength];
         for (var i = 0; i < passwordLength; i++)
         {
             Rotors[i] = new Rotor(i);
         }
     }
     else
     {
         Rotors = rotors;
     }
 }
Exemplo n.º 15
0
        public EnigmaForm()
        {
            InitializeComponent();
            rr = new Rotor("BDFHJLCPRTXVZNYEIWGAKMUSQO", label2, 'V', "RR");
            rm = new Rotor("AJDKSIRUXBLHWTMCQGZNPYFVOE", label3, 'E', "RM");
            rl = new Rotor("EKMFLGDQVZNTOWYHXUSPAIBRCJ", label5, 'Q', "RL");
            reflector = new Rotor("YRUHQSLDPXNGOKMIEBFZCWVJAT", null, '\0', "reflector");

            //J,Z

            rr.SetNextRotor(rm);
            rm.SetNextRotor(rl);
            rl.SetNextRotor(reflector);
            rm.SetPreviousRotor(rr);
            rl.SetPreviousRotor(rm);
            reflector.SetPreviousRotor(rl);
        }
Exemplo n.º 16
0
        public Enigma(int rotorsCount, int rotorsLayout, int[] offsets)
        {
            r1 = new Rotor("BDFHJLCPRTXVZNYEIWGAKMUSQO", 'V');
            r2 = new Rotor("AJDKSIRUXBLHWTMCQGZNPYFVOE", 'E');
            r3 = new Rotor("EKMFLGDQVZNTOWYHXUSPAIBRCJ", 'Q');
            r4 = new Rotor("NQDJXVLSPHUFACKOIYRWZMEBGT", 'Q');
            r5 = new Rotor("CKPESOHXVUMJRFYALGQBTIDZWN", 'Q');
            reflector = new Rotor("YRUHQSLDPXNGOKMIEBFZCWVJAT", '\0');

            //J,Z

            r1.SetNextRotor(r2);
            r2.SetNextRotor(r3);
            r3.SetNextRotor(reflector);
            r2.SetPreviousRotor(r1);
            r3.SetPreviousRotor(r2);
            reflector.SetPreviousRotor(r3);
        }
Exemplo n.º 17
0
        public void InsertRotor(Rotor rotor)
        {
            if (ReflectorDisc == null)
            {
                throw new Exception("You need to insert the reflector disc first!");
            }

            this.Rotors.Add(rotor);

            if (this.Rotors.Count == 1)
            {
                //The first rotor needs to connect to the reflector
                Disc.Connect(ReflectorDisc, rotor.LeftDisc);
            }
            else
            {
                //just connect to the right disk of the previous rotor to the left disk of this rotor
                var rightDisk = Rotors[Rotors.Count - 2].RightDisc;
                Disc.Connect(rotor.LeftDisc, rightDisk);
            }
        }
        /// <summary>
        /// Sets the enigma machine settings
        /// </summary>
        /// <param name="indexes"></param>
        /// <param name="offsets"></param>
        public void SetEnigmaSettings(int[] indexes, int[] offsets, string reflector, string pb = "")
        {
            plugboard      = "";
            usingPlugboard = false;

            // if an invalid number of values were entered
            if (indexes.Length != NUMBER_OF_ROTORS || offsets.Length != NUMBER_OF_ROTORS)
            {
                throw new IndexOutOfRangeException("INVALID");
            }

            // otherwise the number was valid
            else
            {
                // initialize a new rotor based on the values
                for (int i = 0; i < indexes.Length; i++)
                {
                    rotors[i] = new Rotor(indexes[i], offsets[i]);
                }
            }

            // if the plugboard is not set
            if (pb == "" || pb == null)
            {
                usingPlugboard = false;
            }
            // if the plugboard is set, save the settings
            else
            {
                usingPlugboard = true;
                SetPlugboard(pb);
            }

            // sets the reflector
            this.reflector = reflector;
        }
Exemplo n.º 19
0
        public void InitRotors()
        {
            //--------------------ABCDEFGHIJKLMNOPQRSTUVWXYZ---------//
            rotors.Add(new Rotor("EKMFLGDQVZNTOWYHXUSPAIBRCJ", 'A', 'Q', 1, false, 'A'));
            rotors.Add(new Rotor("AJDKSIRUXBLHWTMCQGZNPYFVOE", 'A', 'E', 2, false, 'A'));
            rotors.Add(new Rotor("BDFHJLCPRTXVZNYEIWGAKMUSQO", 'A', 'V', 3, false, 'A'));
            rotors.Add(new Rotor("ESOVPZJAYQUIRHXLNFTGKDCMWB", 'A', 'J', 4, false, 'A'));
            rotors.Add(new Rotor("VZBRGITYUPSDNHLXAWMJQOFECK", 'A', 'Z', 5, false, 'A'));

            rightRotor  = rotors[2];
            middleRotor = rotors[1];
            leftRotor   = rotors[0];
            reflector   = new Rotor("YRUHQSLDPXNGOKMIEBFZCWVJAT", 'A', '\0', 0, true, 'A');

            SetRotorsPosition();

            for (int i = 1; i <= 5; i++)
            {
                cmbLeftRotor.Items.Add(new ComboBoxItem()
                {
                    Content = i.ToString()
                });
                cmbMiddleRotor.Items.Add(new ComboBoxItem()
                {
                    Content = i.ToString()
                });
                cmbRightRotor.Items.Add(new ComboBoxItem()
                {
                    Content = i.ToString()
                });
            }

            cmbRightRotor.SelectedIndex  = rightRotor.RotorNumber - 1;
            cmbMiddleRotor.SelectedIndex = middleRotor.RotorNumber - 1;
            cmbLeftRotor.SelectedIndex   = leftRotor.RotorNumber - 1;
        }
Exemplo n.º 20
0
        private void btnRandomSettings_Click(object sender, RoutedEventArgs e)
        {
            Random     rand          = new Random();
            List <int> rotorsNumbers = new List <int>();

            while (rotorsNumbers.Count != 3)
            {
                int num = rand.Next(0, 4);
                if (!rotorsNumbers.Contains(num))
                {
                    rotorsNumbers.Add(num);
                }
            }

            rightRotor  = rotors[rotorsNumbers[0]];
            middleRotor = rotors[rotorsNumbers[1]];
            leftRotor   = rotors[rotorsNumbers[2]];

            cmbRightRotor.SelectedIndex  = rotorsNumbers[0];
            cmbMiddleRotor.SelectedIndex = rotorsNumbers[1];
            cmbLeftRotor.SelectedIndex   = rotorsNumbers[2];

            SetRotorsPosition();

            rightRotor.Offset            = (char)rand.Next('A', 'Z');
            rightRotor.InnerRingSetting  = (char)rand.Next('A', 'Z');
            middleRotor.Offset           = (char)rand.Next('A', 'Z');
            middleRotor.InnerRingSetting = (char)rand.Next('A', 'Z');
            leftRotor.Offset             = (char)rand.Next('A', 'Z');
            leftRotor.InnerRingSetting   = (char)rand.Next('A', 'Z');

            UpdateOffsetLabel();

            int pairsCount = rand.Next(0, 10);

            pairs.Clear();
            lbxPlugBoardPairs.Items.Clear();

            while (pairs.Count < pairsCount)
            {
                char first  = (char)rand.Next('A', 'Z');
                char second = (char)rand.Next('A', 'Z');
                if (first == second)
                {
                    continue;
                }

                try
                {
                    Pair.SetPair(ref pairs, first, second);
                }
                catch
                {
                    continue;
                }
            }

            foreach (Pair p in pairs)
            {
                lbxPlugBoardPairs.Items.Add(p.ToString());
            }
        }
Exemplo n.º 21
0
        private void DefineRotors()
        {
            rotor1 = new Rotor();
            rotor2 = new Rotor();
            rotor3 = new Rotor();

            rotor1.RotorLetters.AddRange(new RotorLetter[]
            {
                new RotorLetter('A', 'E'),
                new RotorLetter('B', 'K'),
                new RotorLetter('C', 'M'),
                new RotorLetter('D', 'F'),
                new RotorLetter('E', 'L'),
                new RotorLetter('F', 'G'),
                new RotorLetter('G', 'D'),
                new RotorLetter('H', 'Q'),
                new RotorLetter('I', 'V'),
                new RotorLetter('J', 'Z'),
                new RotorLetter('K', 'N'),
                new RotorLetter('L', 'T'),
                new RotorLetter('M', 'O'),
                new RotorLetter('N', 'W'),
                new RotorLetter('O', 'Y'),
                new RotorLetter('P', 'H'),
                new RotorLetter('Q', 'X'),
                new RotorLetter('R', 'U'),
                new RotorLetter('S', 'S'),
                new RotorLetter('T', 'P'),
                new RotorLetter('U', 'A'),
                new RotorLetter('V', 'I'),
                new RotorLetter('W', 'B'),
                new RotorLetter('X', 'R'),
                new RotorLetter('Y', 'C'),
                new RotorLetter('Z', 'J')
            });
            rotor1.ArrowPosition   = 16;
            rotor1.CurrentPosition = 0;

            rotor2.RotorLetters.AddRange(new RotorLetter[]
            {
                new RotorLetter('A', 'A'),
                new RotorLetter('B', 'J'),
                new RotorLetter('C', 'D'),
                new RotorLetter('D', 'K'),
                new RotorLetter('E', 'S'),
                new RotorLetter('F', 'I'),
                new RotorLetter('G', 'R'),
                new RotorLetter('H', 'U'),
                new RotorLetter('I', 'X'),
                new RotorLetter('J', 'B'),
                new RotorLetter('K', 'L'),
                new RotorLetter('L', 'H'),
                new RotorLetter('M', 'W'),
                new RotorLetter('N', 'T'),
                new RotorLetter('O', 'M'),
                new RotorLetter('P', 'C'),
                new RotorLetter('Q', 'Q'),
                new RotorLetter('R', 'G'),
                new RotorLetter('S', 'Z'),
                new RotorLetter('T', 'N'),
                new RotorLetter('U', 'P'),
                new RotorLetter('V', 'Y'),
                new RotorLetter('W', 'F'),
                new RotorLetter('X', 'V'),
                new RotorLetter('Y', 'O'),
                new RotorLetter('Z', 'E')
            });
            rotor2.ArrowPosition   = 4;
            rotor2.CurrentPosition = 0;

            rotor3.RotorLetters.AddRange(new RotorLetter[]
            {
                new RotorLetter('A', 'B'),
                new RotorLetter('B', 'D'),
                new RotorLetter('C', 'F'),
                new RotorLetter('D', 'H'),
                new RotorLetter('E', 'J'),
                new RotorLetter('F', 'L'),
                new RotorLetter('G', 'C'),
                new RotorLetter('H', 'P'),
                new RotorLetter('I', 'R'),
                new RotorLetter('J', 'T'),
                new RotorLetter('K', 'X'),
                new RotorLetter('L', 'V'),
                new RotorLetter('M', 'Z'),
                new RotorLetter('N', 'N'),
                new RotorLetter('O', 'Y'),
                new RotorLetter('P', 'E'),
                new RotorLetter('Q', 'I'),
                new RotorLetter('R', 'W'),
                new RotorLetter('S', 'G'),
                new RotorLetter('T', 'A'),
                new RotorLetter('U', 'K'),
                new RotorLetter('V', 'M'),
                new RotorLetter('W', 'U'),
                new RotorLetter('X', 'S'),
                new RotorLetter('Y', 'Q'),
                new RotorLetter('Z', 'O')
            });
            rotor3.ArrowPosition   = 21;
            rotor3.CurrentPosition = 0;
        }
Exemplo n.º 22
0
 public void SetPreviousRotor(Rotor previous)
 {
     this.previous = previous;
 }
 /// <summary>
 /// Checks the letter in the Cipher and the index in the alphabet
 /// </summary>
 /// <param name="l">the stored letter</param>
 /// <param name="i">the stored index</param>
 /// <param name="r">the rotor to check</param>
 private void RightToLeft(ref char l, ref int i, Rotor r)
 {
     l = r.LetterInCipher(i);
     i = r.AlphaIndexOfLetter(l);
 }
 /// <summary>
 /// Checks the letter in the alphabet and the index of the cipher
 /// </summary>
 /// <param name="l">the stored letter</param>
 /// <param name="i">the stored index</param>
 /// <param name="r">the rotor to check</param>
 private void LeftToRight(ref char l, ref int i, Rotor r)
 {
     l = r.LetterInAlpha(i);
     i = r.CipherIndexOfLetter(l);
 }
Exemplo n.º 25
0
 public void SetNextRotor(Rotor next)
 {
     this.next = next;
 }
Exemplo n.º 26
0
 //change the reflector
 public void SetReflector(string refl)
 {
     reflector = new Rotor(refl,null,'\0');
     reflector.SetPreviousRotor(rl);
     rl.SetNextRotor(reflector);
 }
Exemplo n.º 27
0
        void MainFormLoad(object sender, System.EventArgs e)
        {
            rr = new Rotor("BDFHJLCPRTXVZNYEIWGAKMUSQO",lblRotor1,'V');
            rm = new Rotor("AJDKSIRUXBLHWTMCQGZNPYFVOE",lblRotor2,'E');
            rl = new Rotor("EKMFLGDQVZNTOWYHXUSPAIBRCJ",lblRotor3,'Q');
            reflector = new Rotor("YRUHQSLDPXNGOKMIEBFZCWVJAT",null,'\0');

            //J,Z

            rr.SetNextRotor(rm);
            rm.SetNextRotor(rl);
            rl.SetNextRotor(reflector);
            rm.SetPreviousRotor(rr);
            rl.SetPreviousRotor(rm);
            reflector.SetPreviousRotor(rl);
        }
Exemplo n.º 28
0
        // main function
        static void Main(string[] args)
        {
            // ROTORS
            string[] rotorArray = { "EKMFLGDQVZNTOWYHXUSPAIBRCJ", "AJDKSIRUXBLHWTMCQGZNPYFVOE", "BDFHJLCPRTXVZNYEIWGAKMUSQO", "ESOVPZJAYQUIRHXLNFTGKDCMWB", "VZBRGITYUPSDNHLXAWMJQOFECK" };
            rotorArray[0] = "EKMFLGDQVZNTOWYHXUSPAIBRCJ";
            rotorArray[1] = "AJDKSIRUXBLHWTMCQGZNPYFVOE";
            rotorArray[2] = "BDFHJLCPRTXVZNYEIWGAKMUSQO";
            rotorArray[3] = "ESOVPZJAYQUIRHXLNFTGKDCMWB";
            rotorArray[4] = "VZBRGITYUPSDNHLXAWMJQOFECK";

            char[] notches = { 'Q', 'E', 'V', 'J', 'Z' };


            // reflector
            Reflector r = new Reflector();

            // plugboRD
            Plugboard p = new Plugboard();

            do
            {
                try
                {
                    // INPUT FROM USER
                    string input;
                    do
                    {
                        int ring1 = 0, ring2 = 0, ring3 = 0, of1 = 0, of2 = 0, of3 = 0, r1 = 0, r2 = 0, r3 = 0;

                        Console.WriteLine("**********************************");
                        Console.WriteLine("please, choose your action:");
                        Console.WriteLine("[1] Dycrypt OR Encrypt");
                        Console.WriteLine("[2] Quit.");
                        Console.WriteLine("**********************************");


                        input = Console.ReadLine();

                        if (input == "1")
                        {
                            //-------------------------------------------------------------------------------
                            // get plugboard from user
                            //-------------------------------------------------------------------------------
                            int    flag = 0;
                            char[] ar;
                            do
                            {
                                Console.WriteLine("Please enter plugboard: (format of: AB CD EF) ");
                                ar = Console.ReadLine().ToUpper().ToCharArray();
                                if (checkplugboard(ar) != false)
                                {
                                    flag = 1;
                                    p.setPlugboard(ar);
                                }
                                else
                                {
                                    Console.WriteLine();
                                    Console.WriteLine("wrong input.");
                                }
                            } while (flag == 0);

                            //-------------------------------------------------------------------------------
                            //get rotors
                            //-------------------------------------------------------------------------------
                            flag = 0;
                            do
                            {
                                try
                                {
                                    Console.WriteLine("Please enter rotors: (format of 1-2-3 OR 5-4-3)");
                                    string rotors = Console.ReadLine();
                                    r1   = rotors[0] - 49;
                                    r2   = rotors[2] - 49;
                                    r3   = rotors[4] - 49;
                                    flag = 1;
                                }
                                catch (System.IndexOutOfRangeException) { Console.WriteLine(); Console.WriteLine("wrong input."); }
                                catch (System.IO.IOException) { Console.WriteLine(); Console.WriteLine("wrong input."); }
                            } while (flag == 0);
                            //-------------------------------------------------------------------------------
                            //get offset
                            //-------------------------------------------------------------------------------
                            flag = 0;
                            do
                            {
                                try
                                {
                                    Console.WriteLine("Please enter offset: (format of 01-20-13 OR 02-13-25)");
                                    string offsets = Console.ReadLine();
                                    of1  = (offsets[0] - 48) * 10 + (offsets[1] - 48) - 1;
                                    of2  = (offsets[3] - 48) * 10 + (offsets[4] - 48) - 1;
                                    of3  = (offsets[6] - 48) * 10 + (offsets[7] - 48) - 1;
                                    flag = 1;
                                }
                                catch (System.IndexOutOfRangeException) { Console.WriteLine(); Console.WriteLine("wrong input."); }
                                catch (System.IO.IOException) { Console.WriteLine(); Console.WriteLine("wrong input."); }
                            } while (flag == 0);


                            //-------------------------------------------------------------------------------
                            //get ring setting
                            //-------------------------------------------------------------------------------
                            flag = 0;
                            do
                            {
                                try
                                {
                                    Console.WriteLine("Please enter ring settings: (format of 01-20-13 OR 02-13-25)");
                                    string ringsettings = Console.ReadLine();
                                    ring1 = (ringsettings[0] - 48) * 10 + (ringsettings[1] - 48) - 1;
                                    ring2 = (ringsettings[3] - 48) * 10 + (ringsettings[4] - 48) - 1;
                                    ring3 = (ringsettings[6] - 48) * 10 + (ringsettings[7] - 48) - 1;
                                    flag  = 1;
                                }
                                catch (System.IndexOutOfRangeException) { Console.WriteLine(); Console.WriteLine("wrong input."); }
                                catch (System.IO.IOException) { Console.WriteLine(); Console.WriteLine("wrong input."); }
                            } while (flag == 0);

                            //-------------------------------------------------------------------------------
                            //get notch
                            //-------------------------------------------------------------------------------


                            Rotor ro1 = new Rotor(of1, ring1, notches[r1], rotorArray[r1]);
                            Rotor ro2 = new Rotor(of2, ring2, notches[r2], rotorArray[r2]);
                            Rotor ro3 = new Rotor(of3, ring3, notches[r3], rotorArray[r3]);

                            Enigma e = new Enigma(ro3, ro2, ro1, p, r);
                            Console.WriteLine("Please enter the message:");
                            string str = Console.ReadLine().ToUpper();

                            Console.WriteLine("************* OUTPUT *************");

                            for (int i = 0; i < str.Length; i++)
                            {
                                if (str[i] == ' ')
                                {
                                    continue;
                                }
                                else
                                {
                                    Console.Write(e.RunEnigma(str[i]));
                                }
                            }
                            Console.WriteLine();
                            Console.WriteLine("**********************************");
                            Console.WriteLine("PRESS ENTER TO CONTINEU.");
                            Console.ReadLine();
                            Console.Clear();
                            Console.WriteLine("**********************************");
                            Console.WriteLine("**********************************");
                            Console.WriteLine("Last runnig enigma settings are:");
                            Console.WriteLine("Rotors: " + (r1 + 1) + "-" + (r2 + 1) + "-" + (r3 + 1) + ".");
                            Console.WriteLine("Plugboard: " + new string(ar));
                            Console.WriteLine("Offset: " + (ro1.getOffset() + 1) + "-" + (ro2.getOffset() + 1) + "-" + (ro3.getOffset() + 1) + ".");
                            Console.WriteLine("Ring settings: " + (ring1 + 1) + "-" + (ring2 + 1) + "-" + (ring3 + 1) + ".");
                            Console.WriteLine("**********************************");
                        }
                        else if (input == "2")
                        {
                            Console.WriteLine("see ya.");
                            return;
                        }
                        else
                        {
                            Console.Clear();
                            Console.WriteLine("wrong input..");
                        }
                    } while (input != "0");

                    //  performance();
                }
                catch (System.IO.IOException)
                {
                    Console.Clear();
                    Console.WriteLine("wrong input.");
                }
                catch (System.IndexOutOfRangeException)
                {
                    Console.Clear();
                    Console.WriteLine("wrong input.");
                }
            } while (true);


            // performance();
        }
        /// <summary>
        /// Chooses the rotors for your enigma machines
        /// </summary>
        public void ChooseRotors(RotorNumber rotor1, RotorNumber rotor2, RotorNumber rotor3)
        {
            switch (rotor3)
            {
            case RotorNumber.Rotor1:
                this._rotor3 = new Rotor(rotorI, true, rotorITurnover);
                break;

            case RotorNumber.Rotor2:
                this._rotor3 = new Rotor(rotorII, true, rotorIITurnover);
                break;

            case RotorNumber.Rotor3:
                this._rotor3 = new Rotor(rotorIII, true, rotorIIITurnover);
                break;

            case RotorNumber.Rotor4:
                this._rotor3 = new Rotor(rotorIV, true, rotorIVTurnover);
                break;

            case RotorNumber.Rotor5:
                this._rotor3 = new Rotor(rotorV, true, rotorVTurnover);
                break;

            default:
                throw new IndexOutOfRangeException($"{rotor3} is not a valid rotor");
            }

            switch (rotor2)
            {
            case RotorNumber.Rotor1:
                this._rotor2 = new Rotor(rotorI, false, rotorITurnover);
                break;

            case RotorNumber.Rotor2:
                this._rotor2 = new Rotor(rotorII, false, rotorIITurnover);
                break;

            case RotorNumber.Rotor3:
                this._rotor2 = new Rotor(rotorIII, false, rotorIIITurnover);
                break;

            case RotorNumber.Rotor4:
                this._rotor2 = new Rotor(rotorIV, false, rotorIVTurnover);
                break;

            case RotorNumber.Rotor5:
                this._rotor2 = new Rotor(rotorV, false, rotorVTurnover);
                break;

            default:
                throw new IndexOutOfRangeException($"{rotor2} is not a valid rotor");
            }

            switch (rotor1)
            {
            case RotorNumber.Rotor1:
                this._rotor1 = new Rotor(rotorI, false, rotorITurnover);
                break;

            case RotorNumber.Rotor2:
                this._rotor1 = new Rotor(rotorII, false, rotorIITurnover);
                break;

            case RotorNumber.Rotor3:
                this._rotor1 = new Rotor(rotorIII, false, rotorIIITurnover);
                break;

            case RotorNumber.Rotor4:
                this._rotor1 = new Rotor(rotorIV, false, rotorIVTurnover);
                break;

            case RotorNumber.Rotor5:
                this._rotor1 = new Rotor(rotorV, false, rotorVTurnover);
                break;

            default:
                throw new IndexOutOfRangeException($"{rotor1} is not a valid rotor");
            }

            this._rotor3.AdvanceAdjacentRotor += this._rotor2.RotateHandler;
            this._rotor2.AdvanceAdjacentRotor += this._rotor1.RotateHandler;
        }
Exemplo n.º 30
0
        /*Hello! This is an Enigma Machine.
         * Currently your machine is set up like this:
         *
         * [ R1 ][ R2 ][ R3 ]
         * [ 01 ][ 01 ][ 01 ]
         *
         * ABCDEFGHIJKLMNOPQRSTUVWXYZ
         * ||||||||||||||||||||||||||
         * ABCDEFGHIJKLMNOPQRSTUVWXYZ
         *
         * [1] Use the machine.
         * [2] Change the rotors.
         * [3] Wire the plugbox.
         * [4] Exit the application.
         *
         */



        static void Main(string[] args)
        {
            Rotor[] aantalRotors  = new Rotor[5];
            Plugbox enigmaPlugbox = new Plugbox();
            Enigma  enigma        = new Enigma();

            enigma = new Enigma();
            // default/start waarden invullen

            int[]  currentRotors = { 1, 2, 3 };
            char[] menuKeuzes    = { '1', '2', '3', '4' };
            char   keyStrike;


            for (int i = 0; i < aantalRotors.Length; i++)
            {
                aantalRotors[i]        = new Rotor();
                aantalRotors[i].myCode = i;
            }

            Console.WriteLine($"Hello! This is an Enigma Machine.\n" +
                              $"Currently your machine is set up like this:\n\n");

            printRotor();
            printPlugbox();


            // menu keuze functie aanmaken

            Console.Write($"\n");
            Console.WriteLine($"[1] Use the machine.\n" +
                              $"[2] Change the rotors.\n" +
                              $"[3] Wire the plugbox.\n" +
                              $"[4] Exit the application\n");

            do
            {
                keyStrike = Console.ReadKey().KeyChar;
            }while (!menuKeuzes.Contains(keyStrike));

            switch (keyStrike)
            {
            case '1':
                useMachine();
                break;

            case '2':
                break;

            case '3':
                break;

            case '4':
                break;
            }

            void printRotor()
            {
                foreach (Rotor enigmaRotor in enigma.Rotors)
                {
                    if (currentRotors.Contains(enigmaRotor.myCode))
                    {
                        Console.Write($"[ R{enigmaRotor.myCode} ] ");
                    }
                }

                Console.Write("\n");
                foreach (Rotor enigmaRotor in enigma.Rotors)
                {
                    if (currentRotors.Contains(enigmaRotor.myCode))
                    {
                        Console.Write("[ " + String.Format("{0:00}", enigmaRotor.myRotation) + " ] ");
                    }
                }
                Console.Write("\n");
            }

            void printPlugbox()
            {
                Console.WriteLine("\n");
                Console.WriteLine(enigmaPlugbox.encryptionKeysLeft);

                for (int i = 0; i < enigmaPlugbox.encryptionKeysLeft.Length; i++)
                {
                    if (i != enigmaPlugbox.encryptionKeysLeft.Length - 1)
                    {
                        Console.Write("|");
                    }
                    else
                    {
                        Console.WriteLine("|");
                    }
                }
                Console.WriteLine(enigmaPlugbox.encryptionKeysRight);
            }

            void useMachine()

            /*
             * [ENIGMA MACHINE]
             *
             * [ R1 ] [ R2 ] [ R3 ]
             * [ 10 ] [ 01 ] [ 01 ]
             *
             * Input: CIXHNHWGK
             * Output:
             */
            {
                string inputMsg  = "";
                string outputMsg = "";

                ConsoleKeyInfo keyinfo;

                do
                {
                    Console.Clear();
                    Console.WriteLine("[ENIGMA MACHINE]\n");

                    printRotor();


                    Console.Write("\n" +
                                  $"Input:    {inputMsg}\n" +
                                  $"Output:   {outputMsg}\n\n" +
                                  $"(Esc) key to go back\n");

                    keyinfo    = Console.ReadKey(true);
                    inputMsg  += Char.ToUpper(keyinfo.KeyChar);
                    outputMsg += enigma.Gebruiken(Char.ToUpper(keyinfo.KeyChar));
                }while (keyinfo.Key != ConsoleKey.Escape);
            }
        }
Exemplo n.º 31
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Enigma.EnigmaMachine"/> class.
 /// </summary>
 /// <param name="slow">Slow rotor.</param>
 /// <param name="medium">Medium rotor.</param>
 /// <param name="fast">Fast rotor.</param>
 public EnigmaMachine(Rotor slow, Rotor medium, Rotor fast)
 {
     SetRotors(slow, medium, fast);
 }
Exemplo n.º 32
0
 /// <summary>
 /// Sets the rotors.
 /// </summary>
 /// <param name="slow">Slow.</param>
 /// <param name="medium">Medium.</param>
 /// <param name="fast">Fast.</param>
 public void SetRotors(Rotor slow, Rotor medium, Rotor fast)
 {
     SlowRotor   = slow;
     MediumRotor = medium;
     FastRotor   = fast;
 }
Exemplo n.º 33
0
        // function to run performance tests
        static public void performance()
        {
            // ROTORS
            string[] rotorArray = { "EKMFLGDQVZNTOWYHXUSPAIBRCJ", "AJDKSIRUXBLHWTMCQGZNPYFVOE", "BDFHJLCPRTXVZNYEIWGAKMUSQO", "ESOVPZJAYQUIRHXLNFTGKDCMWB", "VZBRGITYUPSDNHLXAWMJQOFECK" };
            rotorArray[0] = "EKMFLGDQVZNTOWYHXUSPAIBRCJ";
            rotorArray[1] = "AJDKSIRUXBLHWTMCQGZNPYFVOE";
            rotorArray[2] = "BDFHJLCPRTXVZNYEIWGAKMUSQO";
            rotorArray[3] = "ESOVPZJAYQUIRHXLNFTGKDCMWB";
            rotorArray[4] = "VZBRGITYUPSDNHLXAWMJQOFECK";

            char[] notches = { 'Q', 'E', 'V', 'J', 'Z' };


            // reflector
            Reflector r = new Reflector();

            // plugboRD
            Plugboard p    = new Plugboard();
            string    plug = "ZU HL CQ WM OA PY EB TR DN VI";

            char[] ar = plug.ToCharArray();


            //-------------------------------------------------------------------------------
            //get rotors
            //-------------------------------------------------------------------------------
            int n = 0;

            while (n < 1000)
            {
                Random rnd = new Random();
                int    r1, r2, r3;
                int    of1, of2, of3;
                int    ri1, ri2, ri3;
                do
                {
                    r1 = rnd.Next(5);
                    r2 = rnd.Next(5);
                    r3 = rnd.Next(5);
                } while (r1 == r2 || r2 == r3 || r1 == r3);

                //-------------------------------------------------------------------------------
                //get offset
                //-------------------------------------------------------------------------------

                do
                {
                    of1 = rnd.Next(26);
                    of2 = rnd.Next(26);
                    of3 = rnd.Next(26);
                } while (of1 == of2 || of2 == of3 || of1 == of3);


                //-------------------------------------------------------------------------------
                //get ring setting
                //-------------------------------------------------------------------------------
                do
                {
                    ri1 = rnd.Next(26);
                    ri2 = rnd.Next(26);
                    ri3 = rnd.Next(26);
                } while (ri1 == ri2 || ri2 == ri3 || ri1 == ri3);


                //-------------------------------------------------------------------------------
                //get notch
                //-------------------------------------------------------------------------------


                Rotor ro1 = new Rotor(of1, ri1, notches[r1], rotorArray[r1]);
                Rotor ro2 = new Rotor(of2, ri2, notches[r2], rotorArray[r2]);
                Rotor ro3 = new Rotor(of3, ri3, notches[r3], rotorArray[r3]);

                Enigma e   = new Enigma(ro3, ro2, ro1, p, r);
                string str = "UMDPQCUAQNLVVSPIARKCTTRJQKCFPTOKRGOZXALDRLPUHAUZSOSZFSUGWFNFDZCUGVEXUULQYXOTCYRPSYGGZHQMAGPZDKCKGOJMMYYDDH";
                for (int i = 0; i < str.Length; i++)
                {
                    if (str[i] == ' ')
                    {
                        continue;
                    }
                    else
                    {
                        //Console.Write(e.RunEnigma(str[i]));
                        e.RunEnigma(str[i]);
                    }
                }
                Console.WriteLine();
                n++;
            }
        }
Exemplo n.º 34
0
 public void SetRotor1(int value)
 {
     rotor1 = rotors[value];
 }