public static string VermanCipher(string text, int keyLength, out string generatedKey, ref VermanCipher vermanCipher, CryptType cryptType)
        {
            string result = string.Empty;
            if (vermanCipher == null) vermanCipher = new VermanCipher(keyLength);
            generatedKey = vermanCipher.GeneratedKey;
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = vermanCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = vermanCipher.Decrypt(text);
                    break;
            }

            return result;
        }
Example #2
0
        /// <summary>
        /// Шифр Вернама
        /// </summary>
        public static void VermanCipherTest()
        {
            Console.WriteLine("4. Шифр Вернама");

            Console.WriteLine("Пример работы программы");
            cryptogram = "I study at DNU";
            Console.WriteLine("Текст: " + cryptogram);
            int lengthKey = 5;

            vermanCipher = new VermanCipher(lengthKey);
            Console.WriteLine($"Случайно сгенерированный ключ длиной {lengthKey}: {vermanCipher.GeneratedKey}");
            encryptText = vermanCipher.Encrypt(cryptogram);
            Console.WriteLine("Зашифрованый текст: " + encryptText);
            decryptText = vermanCipher.Decrypt(encryptText);
            Console.WriteLine("Расшифрованый текст: " + decryptText);

            Console.WriteLine("\n");
        }
        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;
            }
        }