AttemptsTreeNode CreateCopy(AttemptsTreeNode node) { AttemptsTreeNode newNode = new AttemptsTreeNode(); newNode.Frequencies = new Dictionary <char, int>(); newNode.Mappings = new Dictionary <char, char>(); newNode.NextLevel = new List <AttemptsTreeNode>(); newNode.RemainingPlaintextLetters = new List <char>(); foreach (char key in node.Frequencies.Keys) { newNode.Frequencies.Add(key, node.Frequencies[key]); } foreach (char key in node.Mappings.Keys) { newNode.Mappings.Add(key, node.Mappings[key]); } foreach (char c in node.RemainingPlaintextLetters) { newNode.RemainingPlaintextLetters.Add(c); } return(newNode); }
void PrintNode(AttemptsTreeNode node, String cText) { foreach (AttemptsTreeNode temp in node.NextLevel) { Console.WriteLine(node.GetVal(cText)); if (temp.NextLevel.Count != 0) { PrintNode(temp, cText); } } }
bool ContainsCommonStuff(AttemptsTreeNode node, String originalText) { foreach (String c in commonDigraphs) { if (node.GetVal(originalText).Contains(c)) { return(true); } } foreach (String c in commonWords) { if (node.GetVal(originalText).Contains(c)) { return(true); } } return(false); }
AttemptsTreeNode SubstituteNextMostFrequentLetter(AttemptsTreeNode currNode) { AttemptsTreeNode tempNode = CreateCopy(currNode); char mostFrequent = MostFrequent(tempNode.Frequencies); //Dictionary<char, int> newFrequencies = frequencies; tempNode.Frequencies.Remove(mostFrequent); tempNode.Mappings[mostFrequent] = tempNode.RemainingPlaintextLetters[0]; tempNode.RemainingPlaintextLetters.RemoveAt(0); //AttemptsTreeNode newNode = new AttemptsTreeNode(); //newNode.Frequencies = currNode. //newNode.Mappings = curr; //newNode.RemainingPlaintextLetters = remainingPlaintextLetters; tempNode.NextLevel = new List <AttemptsTreeNode>(); return(tempNode); }
void CountCommon(AttemptsTreeNode node, String originalText) { foreach (String c in commonDigraphs) { if (node.GetVal(originalText).Contains(c)) { node.NumCommon++; } } foreach (String c in commonWords) { if (node.GetVal(originalText).Contains(c)) { node.NumCommon++; } } foreach (AttemptsTreeNode next in node.NextLevel) { CountCommon(next, originalText); } }
void CreateFirstLevels(AttemptsTreeNode node, String originalText) { for (int i = 1; i < node.RemainingPlaintextLetters.Count; i++) { AttemptsTreeNode tempNode = CreateCopy(node); char temp = tempNode.RemainingPlaintextLetters[i]; tempNode.RemainingPlaintextLetters.RemoveAt(i); tempNode.RemainingPlaintextLetters.Insert(0, temp); tempNode.Continue = true; node.NextLevel.Add(SubstituteNextMostFrequentLetter(tempNode)); } foreach (AttemptsTreeNode next in node.NextLevel) { if (next.RemainingPlaintextLetters.Count > 22 /** || ContainsCommonStuff(next, originalText)**/) { CreateFirstLevels(next, originalText); } } //if (node.RemainingPlaintextLetters.Count > 23) //{ // foreach (AttemptsTreeNode next in node.NextLevel) // CreateNextLevel(next, originalText); //} //else //{ // if (ContainsCommonStuff(node, originalText)) // { // foreach (AttemptsTreeNode next in node.NextLevel) // CreateNextLevel(next, originalText); // } //} }
private void decryptButton_Click(object sender, EventArgs e) { String cText = inputTextBox.Text; //Ensure there is something to encrypt if (cText == String.Empty) { MessageBox.Show("Please enter a ciphertext input to be decrypted.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Dictionary <char, int> counts = new Dictionary <char, int>(); //Count frequencies of each character for (int i = 0; i < cText.Length; i++) { if (char.IsLetter(cText[i])) { try { counts[cText.ToCharArray()[i]]++; } catch { counts.Add(cText.ToCharArray()[i], 1); } } } Info info = new Info(); /**AttemptsTreeNode **/ attemptsNode = new AttemptsTreeNode(); attemptsNode.Mappings = new Dictionary <char, char>(); foreach (char key in counts.Keys) { info.AddEntry(key, counts[key]); attemptsNode.Mappings.Add(key, key); } StreamReader reader = new StreamReader(Environment.CurrentDirectory + "../../../LetterFrequencies.txt"); List <char> letterFrequencies = new List <char>(); while (reader.Peek() != -1) { letterFrequencies.Add(reader.ReadLine().ToCharArray()[0]); } reader = new StreamReader(Environment.CurrentDirectory + "../../../CommonWords.txt"); commonWords = new List <string>(); while (reader.Peek() != -1) { commonWords.Add(reader.ReadLine()); } reader = new StreamReader(Environment.CurrentDirectory + "../../../CommonDigraphs.txt"); commonDigraphs = new List <string>(); while (reader.Peek() != -1) { commonDigraphs.Add(reader.ReadLine()); } info.Show(); attemptsNode.Frequencies = counts; attemptsNode.RemainingPlaintextLetters = letterFrequencies; attemptsNode.Continue = true; attemptsNode.NumCommon = 0; attemptsNode.NextLevel = new List <AttemptsTreeNode>(); CreateFirstLevels(attemptsNode, cText); //Have substitutions for 3 most common letters CountCommon(attemptsNode, cText); AttemptsTreeNode bestGuess; PrintNode(attemptsNode, cText); }
AttemptsTreeNode MaxCommon(AttemptsTreeNode node, AttemptsTreeNode curr) { }
AttemptsTreeNode CreateNewLevel(AttemptsTreeNode node) { return(node); }