コード例 #1
0
        public static string NGrammarSubstitutionCipher(string text, out string showText, ref NGrammarSubstitutionCipher grammarSubstitutionCipher, CryptType cryptType)
        {
            string result = string.Empty;
            if (grammarSubstitutionCipher == null) grammarSubstitutionCipher = new NGrammarSubstitutionCipher();
            var dictionary = grammarSubstitutionCipher.GetGrammarDictionary();
            showText = string.Empty;
            int index = 0;
            foreach (var kvp in dictionary)
            {
                showText += kvp.Key + "=" + kvp.Value + "; ";
                index++;
                if (index == 26)
                {
                    showText += "\n";
                    index = 0;
                }
            }
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = grammarSubstitutionCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = grammarSubstitutionCipher.Decrypt(text);
                    break;
            }

            return result;
        }
コード例 #2
0
        public static void NGrammarSubstitutionCipherTest()
        {
            Console.WriteLine("8. N-граммные подстановки");

            Console.WriteLine("Пример работы программы");
            cryptogram = "Dima Bezotosnyi is the best";
            Console.WriteLine("Текст: " + cryptogram);
            grammarSubstitutionCipher = new NGrammarSubstitutionCipher();
            Console.WriteLine(grammarSubstitutionCipher.ShowGrammarDictionary());
            encryptText = grammarSubstitutionCipher.Encrypt(cryptogram);
            Console.WriteLine("Зашифрованый текст: " + encryptText);
            decryptText = grammarSubstitutionCipher.Decrypt(encryptText);
            Console.WriteLine("Расшифрованый текст: " + decryptText);

            Console.WriteLine("\n");
        }
コード例 #3
0
        private void EncryptButton_Click(object sender, System.EventArgs e)
        {
            switch ((CipherType)this.CipherTypeComboBox.SelectedIndex)
            {
            case CipherType.SubstitutionCipher:
                this.OutputTextRichTextBox.Text = LogicCipher.SubstitutionCipher(
                    this.InputTextRichTextBox.Text,
                    int.Parse(this.CountSubstitutionNumericUpDown.Value.ToString(CultureInfo.InvariantCulture)),
                    CryptType.Encrypt);
                break;

            case CipherType.TranspositionCipher:
                this.OutputTextRichTextBox.Text = LogicCipher.TranspositionCipher(
                    this.InputTextRichTextBox.Text,
                    this.KeyTextBox.Text,
                    CryptType.Encrypt);
                break;

            case CipherType.VigenereCipher:
                var vigenereCipherType = VigenereCipherType.None;
                if (this.StraightCheckBox.Checked)
                {
                    vigenereCipherType = VigenereCipherType.Straight;
                }
                if (this.ReverseCheckBox.Checked)
                {
                    vigenereCipherType = VigenereCipherType.Reverse;
                }
                this.OutputTextRichTextBox.Text = LogicCipher.VigenereCipher(
                    this.InputTextRichTextBox.Text,
                    this.KeyTextBox.Text,
                    vigenereCipherType,
                    CryptType.Encrypt);
                break;

            case CipherType.VermanCipher:
                this.vermanCipher = new VermanCipher(int.Parse(this.CountSubstitutionNumericUpDown.Value.ToString(CultureInfo.InvariantCulture)));
                this.OutputTextRichTextBox.Text = LogicCipher.VermanCipher(
                    this.InputTextRichTextBox.Text,
                    int.Parse(this.CountSubstitutionNumericUpDown.Value.ToString(CultureInfo.InvariantCulture)),
                    out this.showText,
                    ref this.vermanCipher,
                    CryptType.Encrypt);
                this.ShowDialogButton.Enabled = true;
                break;

            case CipherType.RunningKeyCipher:
                this.OutputTextRichTextBox.Text = LogicCipher.RunningKeyCipher(
                    this.InputTextRichTextBox.Text,
                    this.KeyTextBox.Text,
                    CryptType.Encrypt);
                break;

            case CipherType.CaesarCipher:
                this.OutputTextRichTextBox.Text = LogicCipher.CaesarCipher(
                    this.InputTextRichTextBox.Text,
                    int.Parse(this.CountSubstitutionNumericUpDown.Value.ToString(CultureInfo.InvariantCulture)),
                    CryptType.Encrypt);
                break;

            case CipherType.NGrammarSubstitutionCipher:
                this.grammarSubstitutionCipher  = new NGrammarSubstitutionCipher();
                this.OutputTextRichTextBox.Text = LogicCipher.NGrammarSubstitutionCipher(
                    this.InputTextRichTextBox.Text,
                    out this.showText,
                    ref this.grammarSubstitutionCipher,
                    CryptType.Encrypt);
                this.ShowDialogButton.Enabled = true;
                break;

            case CipherType.PlayFairCipher:
                this.OutputTextRichTextBox.Text = LogicCipher.PlayFairCipher(
                    this.InputTextRichTextBox.Text,
                    CryptType.Encrypt);
                break;

            case CipherType.AutoKeyCipher:
                var keyCipherType = AutoKeyCipherType.UseText;
                if (this.StraightCheckBox.Checked)
                {
                    keyCipherType = AutoKeyCipherType.UseText;
                }
                if (this.ReverseCheckBox.Checked)
                {
                    keyCipherType = AutoKeyCipherType.UseCryptogram;
                }
                this.autoKeyCipher = new AutoKeyCipher(this.KeyTextBox.Text, keyCipherType);
                this.OutputTextRichTextBox.Text = LogicCipher.AutoKeyCipher(
                    this.InputTextRichTextBox.Text,
                    this.KeyTextBox.Text,
                    ref this.showText,
                    ref this.autoKeyCipher,
                    keyCipherType,
                    CryptType.Encrypt);
                this.ShowDialogButton.Enabled = true;
                break;

            case CipherType.FractionalCipher:
                this.OutputTextRichTextBox.Text = LogicCipher.FractionalCipher(
                    this.InputTextRichTextBox.Text,
                    CryptType.Encrypt);
                break;
            }
        }