private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { // Get array of checked letters List <char[]> checkedLetterLists = new List <char[]>(); foreach (DataGridView dgv in dataGrids) { List <char> checkedLetters = new List <char>(); foreach (DataGridViewRow row in dgv.Rows) { DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)(row.Cells["checkBoxes"]); if (Convert.ToBoolean(cell.Value)) { checkedLetters.Add((char)(row.Cells["Letter"].Value)); } } checkedLetterLists.Add(checkedLetters.ToArray()); } // Get array of keys string[] keys = CaesarAndVigenere.joinPossibleKeys(checkedLetterLists.ToArray()); // Make DataTable for the final output DataGridView resultsTable = new DataTable(); resultsTable.Columns.Add("Key", typeof(string)); resultsTable.Columns.Add("Chi-squared score", typeof(double)); resultsTable.Columns.Add("Decrypted text", typeof(string)); for (int i = 0; i < keys.Length; i++) { string key = keys[i]; DataRow newRow = resultsTable.NewRow(); string decryptedText = CaesarAndVigenere.vigenereText(parent.ciphertext, key, false); //Console.WriteLine(decryptedText.Length); newRow[0] = key; newRow[1] = CaesarAndVigenere.chiSquaredScore(decryptedText); newRow[2] = decryptedText; resultsTable.Rows.Add(newRow); ((BackgroundWorker)sender).ReportProgress((i * 1000) / keys.Length); } // Push to the DGV }
private void findScores() { int keyLength = Int32.Parse(keyLengthTextBox.Text); //List<double[]> keyLetters = new List<double[]>(); string[] textColumns = CaesarAndVigenere.vigenereColumnSeparator(parent.ciphertext, keyLength); for (int x = 0; x < keyLength; x++) { DataTable dt = new DataTable(); dt.Columns.Add("Letter", typeof(char)); dt.Columns.Add("Chi-squared score", typeof(double)); dt.Columns[0].ReadOnly = true; dt.Columns[1].ReadOnly = true; for (int i = 0; i < 26; i++) { DataRow dr = dt.NewRow(); dr[0] = (char)(i + 65); dr[1] = CaesarAndVigenere.chiSquaredScore(CaesarAndVigenere.caesarText(textColumns[x], CaesarAndVigenere.ALPHABET[i], false)); dt.Rows.Add(dr); } dataGrids[x].DataSource = null; dataGrids[x].Columns.Clear(); dataGrids[x].DataSource = dt; dataGrids[x].Sort(dataGrids[x].Columns[1], ListSortDirection.Ascending); DataGridViewCheckBoxColumn cbc = new DataGridViewCheckBoxColumn(); dataGrids[x].Columns.Add(cbc); cbc.HeaderText = "Check letter"; cbc.Name = "checkBoxes"; cbc.Width = 75; cbc.ReadOnly = false; } checkTopWithin20(); //solveForKeys(); }