Exemple #1
0
        public void StartRingSettings(string gs, string egs, int numRotors, int l, int m, int r, StreamWriter file)
        {   // ring setting lower case letters a -> z;  0 -> 25
            // l = left ground setting, m = middle ground setting , r = right ground setting

            Enigma.Machine.Rotor      lr;
            Enigma.Machine.Rotor      mr;
            Enigma.Machine.Rotor      rr;
            Enigma.Machine.MachineRun ma;

            int gsL = (int)gs[0] - 65;                  // 1st letter of ground setting. uppercase to 0 -> 25
            int gsM = (int)gs[1] - 65;                  // 2nd letter of ground setting
            int gsR = (int)gs[2] - 65;                  // 3rd letter of ground setting

            string dgs = "";                            // decrypted ground setting

            for (int rsL = 0; rsL <= 25; rsL++)         // ring setting left rotor
            {
                for (int rsM = 0; rsM <= 25; rsM++)     // ring setting middle rotor
                {
                    for (int rsR = 0; rsR <= 25; rsR++) // ring setting right rotor
                    {
                        // new rotors
                        lr = new Enigma.Machine.Rotor(l, gsL, rsL);
                        mr = new Enigma.Machine.Rotor(m, gsM, rsM);
                        rr = new Enigma.Machine.Rotor(r, gsR, rsR);

                        // blank plugs
                        string[] plugs = { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null };
                        // new enigma
                        ma = new Enigma.Machine.MachineRun("B", lr, mr, rr, "O", plugs);

                        dgs = ma.EncryptDecrypt(egs);

                        if (dgs[0] == dgs[3] && dgs[1] == dgs[4] && dgs[2] == dgs[5]) // must be ABCABC
                        {
                            count++;

                            //Console.WriteLine("\n\n" + egs);
                            //Console.WriteLine(dgs);

                            file.WriteLine("\n" + egs);
                            file.WriteLine(dgs + "\n");
                            file.WriteLine("settings are ");
                            file.WriteLine("nrotor order:" + l + "" + m + "" + r);
                            file.WriteLine("ground:" + dgs);
                            file.WriteLine("ring:" + (char)(rsL + 97) + "" + (char)(rsM + 97) + "" + (char)(rsR + 97));
                            file.Flush();
                        }
                    }
                }
            }
        }
Exemple #2
0
        public void BruteForceCrib(string msg, string crib, int numOFRotors, string mirror)
        {
            msg  = msg.ToLower();
            crib = crib.ToLower();
            string decrypted = "";

            Enigma.Machine.Rotor rl;
            Enigma.Machine.Rotor rm;
            Enigma.Machine.Rotor rr;
            StreamWriter         file = new System.IO.StreamWriter("C:\\Users\\JiB\\Desktop\\enigmaMAPnew\\BruteForceNew_1.txt");

            Console.WriteLine("crib: " + crib + "\n");
            file.WriteLine("crib: " + crib + "\n");
            Console.WriteLine(" msg: " + msg + "\n");
            file.WriteLine(" msg: " + msg + "\n");

            Stopwatch found = new Stopwatch();

            found.Start();

            Stopwatch timer2 = new Stopwatch();

            timer2.Start();

            string line  = "";
            ulong  count = 0;

            int currentPosL = 0;
            int currentPosM = 0;
            int currentPosR = 0;

            for (int ii = 1; ii <= numOFRotors; ii++)
            {
                for (int jj = 1; jj <= numOFRotors; jj++)
                {
                    for (int kk = 1; kk <= numOFRotors; kk++)
                    {
                        if (ii != jj && ii != kk && jj != kk)
                        {
                            Console.WriteLine("Searching rotor setting:  " + ii + ", " + jj + ", " + kk);
                            file.WriteLine("Searching rotor setting:  " + ii + ", " + jj + ", " + kk);
                            file.Flush();

                            for (int i = 65; i <= 90; i++)
                            {
                                for (int j = 65; j <= 90; j++)
                                {
                                    for (int k = 65; k <= 90; k++)
                                    {
                                        count++;

                                        currentPosL = i - 65;
                                        currentPosM = j - 65;
                                        currentPosR = k - 65;
                                        //--------------------------------------------------------------------

                                        rl = new Enigma.Machine.Rotor(ii, currentPosL, 0);
                                        rm = new Enigma.Machine.Rotor(jj, currentPosM, 0);
                                        rr = new Enigma.Machine.Rotor(kk, currentPosR, 0);
                                        string[] plugs = { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null };
                                        Enigma.Machine.MachineRun ma = new Enigma.Machine.MachineRun(mirror, rl, rm, rr, "O", plugs);
                                        currentPosL = rl.GetCpos();
                                        currentPosM = rm.GetCpos();
                                        currentPosR = rr.GetCpos();
                                        decrypted   = ma.EncryptDecrypt(msg).ToLower();

                                        if (decrypted.Contains(crib))
                                        {
                                            found.Stop();
                                            long se = timer2.ElapsedMilliseconds / 1000;    //seconds
                                            long mi = timer2.ElapsedMilliseconds % 1000;    // milliseconds
                                            line  = "\n MATCH FOUND AT SETTING:  " + ii + ", " + jj + ", " + kk + " : " + (char)(i) + "," + (char)(j) + "," + (char)(k) + "   : timed at " + se + "." + mi + " seconds";
                                            line += "\r\n  number of comparisons: " + count;
                                            line += "\r\n\t  decrypted msg: " + decrypted + "\n";
                                            Console.WriteLine(line);
                                            file.WriteLine(line);
                                            file.Flush();
                                            //return;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            file.WriteLine("\r\n\r\n" + count);

            timer2.Stop();
            long sec = timer2.ElapsedMilliseconds / 1000;
            long min = sec / 60;

            sec = sec % 60;
            long hours = min / 60;

            min = min % 60;
            long mil = timer2.ElapsedMilliseconds % 1000;

            Console.WriteLine("completed in: " + hours + ":" + min + ":" + sec + "." + mil);

            file.WriteLine("completed in: " + hours + ":" + min + ":" + sec + "." + mil);

            file.Flush();
            file.Close();
        }
        public void CreateData()
        {
            string        connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\JiB\\Documents\\Enigma.mdf;Integrated Security=True;Connect Timeout=30";
            SqlConnection connection       = new SqlConnection(connectionString);


            Enigma.Machine.Rotor rl; // = new Enigma.Machine.Rotor("III", 0, 0);
            Enigma.Machine.Rotor rm; // = new Enigma.Machine.Rotor("II", 0, 0);
            Enigma.Machine.Rotor rr; // = new Enigma.Machine.Rotor("I", 0, 0);

            Console.WriteLine("Starting SQL creation ...");

            for (int c = 65; c <= 90; c++)
            {
                string letter = Convert.ToChar(c).ToString().ToLower();
                Console.WriteLine("working on letter : " + letter);
                int count = 0;

                Stopwatch timer2 = new Stopwatch();
                timer2.Start();

                for (int i = 65; i <= 90; i++)
                {
                    for (int j = 65; j <= 90; j++)
                    {
                        for (int k = 65; k <= 90; k++)
                        {
                            rl = new Enigma.Machine.Rotor("I", i - 65, 0);
                            rm = new Enigma.Machine.Rotor("III", j - 65, 0);
                            rr = new Enigma.Machine.Rotor("II", k - 65, 0);
                            string[] plugs = { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null };
                            Enigma.Machine.MachineRun ma = new Enigma.Machine.MachineRun("B", rl, rm, rr, "O", plugs);

                            string     sqlInsert     = "INSERT INTO InOut(I, L, M, R, O, nextL, nextM, nextR, numLrotor, numMrotor, numRrotor ) VALUES(@letter, @L, @M, @R, @O, @nextL, @nextM, @nextR, @numLrotor, @numMrotor, @numRrotor)";
                            SqlCommand insertCommand = new SqlCommand(sqlInsert, connection);
                            insertCommand.Parameters.AddWithValue("@letter", letter);
                            insertCommand.Parameters.AddWithValue("@L", (i - 65));
                            insertCommand.Parameters.AddWithValue("@M", (j - 65));
                            insertCommand.Parameters.AddWithValue("@R", (k - 65));
                            insertCommand.Parameters.AddWithValue("@O", ma.EncryptDecrypt(letter));
                            insertCommand.Parameters.AddWithValue("@nextL", rl.GetCpos());
                            insertCommand.Parameters.AddWithValue("@nextM", rm.GetCpos());
                            insertCommand.Parameters.AddWithValue("@nextR", rr.GetCpos());
                            insertCommand.Parameters.AddWithValue("@numLrotor", rl.GetN());
                            insertCommand.Parameters.AddWithValue("@numMrotor", rm.GetN());
                            insertCommand.Parameters.AddWithValue("@numRrotor", rr.GetN());

                            try
                            {
                                connection.Open();
                                insertCommand.ExecuteNonQuery();
                                count++;
                            }
                            catch (SqlException se)
                            {
                                MessageBox.Show("" + se);
                            }
                            finally
                            {
                                connection.Close();
                            }
                        }
                    }
                }

                Console.WriteLine("letter " + letter + " has been inserted " + count + " times");
                timer2.Stop();
                long sec = timer2.ElapsedMilliseconds / 1000;
                long mil = timer2.ElapsedMilliseconds % 1000;
                Console.WriteLine(sec + "." + mil + " seconds");
            }

            Console.WriteLine("SQL created ...");
            Console.WriteLine("press any key to close ...");
            Console.ReadLine();
        }