public void DecodeTest() { SubstitutionCypher sc = new SubstitutionCypher("hgllo", alphabet); SpecialVariableBaseNumber seed = new SpecialVariableBaseNumber(new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0 }); Assert.AreEqual("hello", sc.Decode(seed)); }
public void WhenIEncryptTheTextUsingTheFollowingLetterSubstitution(Table table) { var cypher = new SubstitutionCypher(); foreach (var tableRow in table.Rows) { cypher.Substitute(tableRow[0][0]).For(tableRow[1][0]); } _encryptedText = cypher.Encrypt(_plainText); }
private void Hack() { string text = textBoxEncryptedText.Text; hacker.Alphabet = alphabet; hacker.Analyze(text); var table1 = new Dictionary <char, double>(); var table2 = new Dictionary <char, double>(); switch (alphabet) { case Alphabet.Latin: table1 = analyzer.LatinTable; table2 = hacker.LatinTable; break; case Alphabet.Ukrainian: table1 = analyzer.UkrainianTable; table2 = hacker.UkrainianTable; break; } var query = from x in table1 orderby x.Value descending select x.Key; var query2 = from x in table2 orderby x.Value descending select x.Key; var substitutions = new Dictionary <char, char>(); for (int i = 0; i < query.Count(); i++) { substitutions.Add(query.ElementAt(i), query2.ElementAt(i)); } ObservableCollection <SubTable> collection = new ObservableCollection <SubTable>(); foreach (var s in substitutions) { collection.Add(new SubTable { Letter = s.Key, SubLetter = s.Value }); } dataGridHackerTable.ItemsSource = collection; SubstitutionCypher cipher = new SubstitutionCypher { Alphabet = alphabet }; textBoxDecryptedText.Text = cipher.Decrypt(text, substitutions); }
private void SubstitutionCypherDecrypt() { SubstitutionCypher cypher = new SubstitutionCypher { Alphabet = alphabet }; string C = textBoxEncryptedText.Text; DoEvents(); Dictionary <char, char> substitutions = new Dictionary <char, char>(); DataGrid dataGrid = gridKey.Children[1] as DataGrid; for (int i = 0; i < alphabet.GetLength(); ++i) { var letter = (dataGrid.GetCell(i, 0).Content as TextBlock).Text[0]; var subletter = (dataGrid.GetCell(i, 1).Content as TextBlock).Text[0]; substitutions.Add(letter, subletter); } textBoxDecryptedText.Text = cypher.Decrypt(C, substitutions); }
public void encodeTest() { SubstitutionCypher sc = new SubstitutionCypher("hello", alphabet); Assert.AreEqual('a', alphabet[0]); Assert.AreEqual('z', alphabet[25]); SpecialVariableBaseNumber seed = new SpecialVariableBaseNumber(26); Assert.AreEqual("hello", sc.Encode(seed)); seed = new SpecialVariableBaseNumber(new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }); Assert.AreEqual("hfllo", sc.Encode(seed)); seed = new SpecialVariableBaseNumber(new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0 }); Assert.AreEqual("hgllo", sc.Encode(seed)); }
public void EncodeDecodePairs() { SpecialVariableBaseNumber seed = new SpecialVariableBaseNumber(26); seed.Inc(); for (int i = 0; i < 100000; ++i) { string originalString = "abcdefghijklmnopqrstuvwxyz"; SubstitutionCypher original = new SubstitutionCypher(originalString, alphabet); if (i == 0) { Console.WriteLine($"{original.NumSeeds} possible combinations"); } string codedString = original.Encode(seed); Assert.AreNotEqual(originalString, codedString); SubstitutionCypher coded = new SubstitutionCypher(codedString, alphabet); Assert.AreEqual(originalString, coded.Decode(seed)); seed.IncBy(732); } SubstitutionCypher usesLargeSeed = new SubstitutionCypher("hello", alphabet); seed = new SpecialVariableBaseNumber(26); seed.IncBy(usesLargeSeed.NumSeeds - 1); string borderEncoded = usesLargeSeed.Encode(seed); Console.WriteLine($"Using seed {seed}"); Console.WriteLine($"Coded: {borderEncoded}"); SubstitutionCypher reverseWithLargeSeed = new SubstitutionCypher(borderEncoded, alphabet); string decoded = reverseWithLargeSeed.Decode(seed); Console.WriteLine($"Decoded: {decoded}"); Assert.AreEqual("hello", decoded); }
private void SubstitutionCypherEncrypt() { SubstitutionCypher cipher = new SubstitutionCypher { Alphabet = alphabet }; string text = Regex.Replace(textBoxOpenText.Text.ToLower(), "[^" + alphabet.GetStringValue() + "]", ""); string M = Regex.Replace(text, @"\s+", " "); DoEvents(); Dictionary <char, char> substitutions = new Dictionary <char, char>(); DataGrid dataGrid = gridKey.Children[1] as DataGrid; for (int i = 0; i < alphabet.GetLength(); ++i) { var letter = (dataGrid.GetCell(i, 0).Content as TextBlock).Text[0]; var subletter = (dataGrid.GetCell(i, 1).Content as TextBlock).Text[0]; substitutions.Add(letter, subletter); } textBoxEncryptedText.Text = cipher.Encrypt(M, substitutions); }