private void Entropy(object sender, RoutedEventArgs e) { if (RichText.GetText(RichTextOrig) != String.Empty) { int[] alphabetCount = new int[alphabet.Length]; int n = 0; double[] alphabetChance = new double[alphabet.Length]; double entropy = 0.0D; string text = RichText.GetText(RichTextOrig).Substring(0, RichText.GetText(RichTextOrig).Length - 2).ToLower(); string replStr = " ,.!?:-;()[]'\""; for (int i = 0; i < replStr.Length; i++) { text = text.Replace(replStr[i].ToString(), ""); } for (int i = 0; i < text.Length; i++) { for (int j = 0; j < alphabet.Length; j++) { if (text[i] == alphabet[j]) { alphabetCount[j]++; n++; } } } for (int i = 0; i < alphabetChance.Length; i++) { alphabetChance[i] = (double)alphabetCount[i] / n; } for (int i = 0; i < alphabetChance.Length; i++) { if (alphabetChance[i] != 0) { entropy += alphabetChance[i] * Math.Log(alphabetChance[i], 2); } } TextEntropy.Text = (entropy * (-1)).ToString(); Histogram histo = new Histogram(alphabetChance); histo.Show(); } else { MessageBox.Show("Поле пустое. Заполните его"); } }
private void Entropy(object sender, RoutedEventArgs e) { string text = RichText.GetText(OriginalText).Substring(0, RichText.GetText(OriginalText).Length - 2).ToLower(); if (!checkText(text)) { MessageBox.Show("Введите символ алфавита, не пустую строку :)"); return; } int[] alphCount = new int[alph.Length]; int n = 0; double[] alphChance = new double[alph.Length]; double entropy = 0.0D; string rplChars = " ,.!?:-;()[]'\""; foreach (var chr in rplChars) { text = text.Replace(chr.ToString(), ""); } if (!checkText(text)) { MessageBox.Show("Введите символ алфавита, не пустую строку :)"); return; } for (int i = 0; i < text.Length; i++) { for (int j = 0; j < alph.Length; j++) { if (text[i] == alph[j]) { alphCount[j]++; n++; } } } for (int i = 0; i < alphChance.Length; i++) { alphChance[i] = (double)alphCount[i] / n; } for (int i = 0; i < alphChance.Length; i++) { if (alphChance[i] != 0) { entropy += alphChance[i] * Math.Log(alphChance[i], 2); } } TextEntropy.Text = (entropy * (-1)).ToString(); Histogram histo = new Histogram(alphChance); histo.Show(); }
private void Encrypt(object sender, RoutedEventArgs e) { RichTextEnc.Document.Blocks.Clear(); if (RichText.GetText(RichTextOrig) != String.Empty && TextA.Text != String.Empty) { char a = TextA.Text.ToLower()[0]; string text = RichText.GetText(RichTextOrig).ToLower().Substring(0, RichText.GetText(RichTextOrig).Length - 2); string[] words = text.Split(' '); string encText = ""; //Histogram int[] alphabetCount = new int[alphabet.Length]; int number = 0; double[] alphabetChance = new double[alphabet.Length]; // foreach (string word in words) { string temp = word; if (temp.Length % 2 == 1) { temp += a; } for (int i = 0; i < temp.Length; i = i + 2) { encText += (LetterNumber(temp[i]) * alphabet.Length + LetterNumber(temp[i + 1])).ToString().PadLeft(4, '0'); } encText += " "; } RichText.SetText(RichTextEnc, encText); //Histogram string replStr = " ,.!?:-;()[]'\""; for (int i = 0; i < replStr.Length; i++) { text = text.Replace(replStr[i].ToString(), ""); } for (int i = 0; i < text.Length; i++) { for (int j = 0; j < alphabet.Length; j++) { if (text[i] == alphabet[j]) { alphabetCount[j]++; number++; } } } for (int i = 0; i < alphabetChance.Length; i++) { alphabetChance[i] = (double)alphabetCount[i] / number; } Histogram histo = new Histogram(alphabetChance); histo.Show(); } else { MessageBox.Show("Поле пустое. Заполните его"); } }
private void Encrypt(object sender, RoutedEventArgs e) { RichTextEnc.Document.Blocks.Clear(); if (RichText.GetText(RichTextOrig) != String.Empty && TextA.Text != String.Empty && TextB.Text != String.Empty) { int n = alphabet.Length; int a = Int32.Parse(TextA.Text); int b = Int32.Parse(TextB.Text); //Histogram int[] alphabetCount = new int[alphabet.Length]; int number = 0; double[] alphabetChance = new double[alphabet.Length]; // if (a >= 0 && a < n && b >= 0 && b < n && IsSimple(a) && IsSimple(b) && (n % a != 0)) { string text = RichText.GetText(RichTextOrig).ToLower(); text = text.Substring(0, text.Length - 2); string encText = ""; for (int i = 0; i < text.Length; i++) { if (alphabet.Contains(text[i])) { encText += alphabet[((a * LetterNumber(text[i])) + b) % n]; } else { encText += text[i]; } } RichText.SetText(RichTextEnc, encText); //Histogram string replStr = " ,.!?:-;()[]'\""; for (int i = 0; i < replStr.Length; i++) { text = text.Replace(replStr[i].ToString(), ""); } for (int i = 0; i < text.Length; i++) { for (int j = 0; j < alphabet.Length; j++) { if (text[i] == alphabet[j]) { alphabetCount[j]++; number++; } } } for (int i = 0; i < alphabetChance.Length; i++) { alphabetChance[i] = (double)alphabetCount[i] / number; } Histogram histo = new Histogram(alphabetChance); histo.Show(); } else { MessageBox.Show("Введенные коэффициенты ошибочны. Исправьте это"); } } else { MessageBox.Show("Поле пустое. Заполните его"); } }
private void Decrypt(object sender, RoutedEventArgs e) { RichTextOrig.Document.Blocks.Clear(); if (RichText.GetText(RichTextEnc) != String.Empty) { string encText = RichText.GetText(RichTextEnc).ToLower().Substring(0, RichText.GetText(RichTextEnc).Length - 2); string[] words = encText.Split(' '); string text = ""; //Histogram string digitAlphabet = "0123456789"; int[] digitAlphabetCount = new int[digitAlphabet.Length]; int number = 0; double[] digitAlphabetChance = new double[digitAlphabet.Length]; // foreach (string word in words) { string temp = word; for (int i = 0; i < temp.Length; i = i + 4) { int chain = Int32.Parse(temp.Substring(i, 4)); int row = chain / alphabet.Length; int column = chain % alphabet.Length; text += alphabet[row].ToString() + alphabet[column].ToString(); } text += " "; } RichText.SetText(RichTextOrig, text); string replStr = " ,.!?:-;()[]'\""; for (int i = 0; i < replStr.Length; i++) { encText = encText.Replace(replStr[i].ToString(), ""); } for (int i = 0; i < encText.Length; i++) { for (int j = 0; j < digitAlphabet.Length; j++) { if (encText[i] == digitAlphabet[j]) { digitAlphabetCount[j]++; number++; } } } for (int i = 0; i < digitAlphabetChance.Length; i++) { digitAlphabetChance[i] = (double)digitAlphabetCount[i] / number; } Histogram histo = new Histogram(digitAlphabetChance); histo.Show(); } else { MessageBox.Show("Поле пустое. Заполните его"); } }