コード例 #1
0
        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("Поле пустое. Заполните его");
            }
        }
コード例 #2
0
        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("Поле пустое. Заполните его");
            }
        }
コード例 #3
0
        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("Поле пустое. Заполните его");
            }
        }