示例#1
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();
        }
示例#2
0
        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();
        }