Exemplo n.º 1
0
        private void breakVigenereButton_Click(object sender, EventArgs e)
        {
            const int numberOfLengths = 16;

            // Scores start at 1
            List <double> scores = new List <double>(
                CaesarAndVigenere.vigenereKeyLengthScores(parent.ciphertext, numberOfLengths));

            scores.RemoveAt(0);

            List <double> scoresInOrder = new List <double>(scores);

            //scores.Sort();
            //scores.Reverse();
            //int roundedUpQuarterLength = (int)Math.Ceiling(scores.Count / 4.0);
            //scores.RemoveRange(roundedUpQuarterLength, scores.Count - roundedUpQuarterLength);
            double maxTwenty = scores.Max() * 0.8;

            Console.WriteLine("Max: " + maxTwenty);
            scores = scores.Where(x => x > maxTwenty).ToList();

            List <int> bestIndicies = scores.Select(x => scoresInOrder.IndexOf(x)).ToList();

            bestIndicies.Sort();

            if (bestIndicies[0] + 1 != 4)
            {
                changeKeyLength(bestIndicies[0] + 1);
            }
            else
            {
                //keyLengthChanged();
                findScores();
                checkTopWithin20();
                solveForKeys();
            }
        }
        private void makeGraph()
        {
            if (!invalidNumberLabel.Visible)
            {
                int numberOfLengths = Int32.Parse(numberOfLengthsTextBox.Text);

                DataTable dt = new DataTable();
                dt.Columns.Add("Key Length", typeof(int));
                dt.Columns.Add("IC Score", typeof(double));

                double[] scores = CaesarAndVigenere.vigenereKeyLengthScores(grandparent.ciphertext, numberOfLengths);

                for (int length = 1; length <= numberOfLengths; length++)
                {
                    DataRow newRow = dt.NewRow();

                    newRow[0] = length;
                    newRow[1] = scores[length];

                    dt.Rows.Add(newRow);
                }

                keyLengthScoresDGV.DataSource = null;
                keyLengthScoresDGV.DataSource = dt;
                //keyLengthScoresDGV.Sort(keyLengthScoresDGV.Columns[1], ListSortDirection.Ascending);
                keyLengthScoresDGV.Columns[0].Width = 85;

                //chart1.ChartAreas[0].AxisX.Minimum = 1;
                chart1.Series["Letter Scores"].Points.DataBindY(scores.Skip(1).ToArray());

                // Crop y axis scale
                double smallest = scores.Skip(1).Min();
                Console.WriteLine(smallest);
                chart1.ChartAreas[0].AxisY.Minimum = Math.Round(0.8 * smallest, 2);
            }
        }