Добавил:
донатики - https://qiwi.com/n/1ZOMBIE1 Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
курсовая / ЯП_Курсовая_БунинаАВ.docx
Скачиваний:
13
Добавлен:
11.12.2022
Размер:
1.09 Mб
Скачать

Приложение а

Исходный код программы

using MaterialSkin.Controls;

using System;

using System.IO;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using System.Windows.Forms;

namespace WFA_KR_01042021

{

public partial class FormDecode : MaterialForm

{

char[] alphabet = { 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', ' ' };

public FormDecode()

{

InitializeComponent();

toolTip1.SetToolTip(textBoxGronsf, "Ключ для метода Гронсфельда состоит из цифр.");

toolTip1.SetToolTip(textBoxVijener, "Ключ для метода Виженера состоит из букв.");

toolTip1.SetToolTip(ButtonOpen, "Выберите файл из системы.");

toolTip1.SetToolTip(buttonGronsf, "После нажатия произойдет дешифровка методом Гронсфельда.");

toolTip1.SetToolTip(buttonVijener, "После нажатия произойдет дешифровка методом Виженера.");

}

private void ButtonOpen_Click(object sender, EventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = "*.txt file|*.txt";

if (ofd.ShowDialog() == DialogResult.OK)

{

textBoxRez.Text = File.ReadAllText(ofd.FileName, Encoding.Default).ToUpper();

}

if (textBoxRez.Text == "")

{

MessageBox.Show("Выберите файл.", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

else

{

label1.Visible = true;

textBoxGronsf.Visible = true;

buttonGronsf.Visible = true;

}

}

private void buttonGronsf_Click_1(object sender, EventArgs e)

{

StringBuilder decodeText = new StringBuilder();

string Symbol = textBoxGronsf.Text;

string textOld = Convert.ToString(textBoxRez.Text).ToUpper();

try

{

if (!String.IsNullOrEmpty(textBoxRez.Text))

{

if (Regex.Match(Symbol, @"[1-9]").Success)

{

if (textBoxGronsf.Text.Length > 0)

{

int[] keys = Convert.ToString(textBoxGronsf.Text).Select(ch => (int)Char.GetNumericValue(ch)).ToArray();

textBoxRez.Text = "";

for (int i = 0; i < textOld.Length; i++)

{

try

{

if (alphabet.Contains(textOld[i]))

{ decodeText.Append(alphabet[(Array.IndexOf(alphabet, textOld[i]) - keys[i % keys.Length]) % alphabet.Length]);

}

else

{

decodeText.Append(textOld[i]);

}

}

catch

{

if (alphabet.Contains(textOld[i]))

{

decodeText.Append(alphabet[alphabet.Length - Math.Abs((Array.IndexOf(alphabet, textOld[i]) - keys[i % keys.Length]) % alphabet.Length)]);

}

else

{

decodeText.Append(textOld[i]);

}

}

}

textBoxRez.Text = Convert.ToString(decodeText);

string writePath = @"C:\Users\Bunina Alena\Downloads\Гронсфельд.txt";

string text = Convert.ToString(textBoxRez.Text);

try

{

using (StreamWriter sw = new StreamWriter(writePath, false, Encoding.Default))

{

sw.WriteLine(text);

}

}

catch (Exception exp)

{

MessageBox.Show(exp.Message, "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

label2.Visible = true;

textBoxVijener.Visible = true;

buttonVijener.Visible = true;

}

else

{

MessageBox.Show("Введите ключевое слово!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

}

else

{

MessageBox.Show("Неверный формат ключа.", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

}

else

{

MessageBox.Show("Введите текст для дешифровки!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

}

catch (Exception exp)

{

MessageBox.Show(exp.Message, "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

}

private void buttonVijener_Click_1(object sender, EventArgs e)

{

StringBuilder decodeText = new StringBuilder();

string textOld = Convert.ToString(textBoxRez.Text).ToUpper();

string keys = Convert.ToString(textBoxVijener.Text).ToUpper();

int keys_index = 0;

if (!String.IsNullOrEmpty(textBoxRez.Text))

{

string Symbol = textBoxVijener.Text;

if (Regex.Match(Symbol, @"[а-яА-Я]").Success)

{

if (textBoxVijener.Text.Length > 0)

{

textBoxRez.Text = "";

for (int i = 0; i < textOld.Length; i++)

{

if (alphabet.Contains(textOld[i]))

{

decodeText.Append(alphabet[(Array.IndexOf(alphabet, textOld[i]) + alphabet.Length - Array.IndexOf(alphabet, keys[keys_index])) % alphabet.Length]);

if ((keys_index + 1) == keys.Length)

{

keys_index = 0;

}

else

{

keys_index++;

}

}

else

{

decodeText.Append(textOld[i]);

}

}

textBoxRez.Text = Convert.ToString(decodeText);

string writePath = @"C:\Users\Bunina Alena\Downloads\Виженер.txt";

string text = Convert.ToString(textBoxRez.Text);

try

{

using (StreamWriter sw = new StreamWriter(writePath, false, Encoding.Default))

{

sw.WriteLine(text);

}

}

catch (Exception exp)

{

MessageBox.Show(exp.Message, "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

}

else

{

MessageBox.Show("Введите ключевое слово!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

}

else

{

MessageBox.Show("Неверный формат ключа.", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

}

else

{

MessageBox.Show("Введите текст для дешифровки!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

}

private void FormDecode_FormClosed(object sender, FormClosedEventArgs e)

{

Application.Exit();

}

private void ToolStripMenuItemOpen_Click(object sender, EventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = "*.txt file|*.txt";

if (ofd.ShowDialog() == DialogResult.OK)

{

textBoxRez.Text = File.ReadAllText(ofd.FileName, Encoding.Default).ToUpper();

}

if (textBoxRez.Text == "")

{

MessageBox.Show("Выберите файл.", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

else

{

label1.Visible = true;

textBoxGronsf.Visible = true;

buttonGronsf.Visible = true;

}

}

private void ToolStripMenuItemSave_Click(object sender, EventArgs e)

{

SaveFileDialog saveFileDialog = new SaveFileDialog();

saveFileDialog.Filter = "Текстовый документ (*.txt)|*.txt|Все файлы (*.*)|*.*";

if (saveFileDialog.ShowDialog() == DialogResult.OK)

{

StreamWriter streamWriter = new StreamWriter(saveFileDialog.FileName);

streamWriter.WriteLine(textBoxRez.Text);

streamWriter.Close();

}

}

private void ToolStripMenuItemNavi_Click(object sender, EventArgs e)

{

Form formNavigation = Application.OpenForms[1];

formNavigation.Show();

this.Hide();

}

private void ToolStripMenuItemInfo_Click(object sender, EventArgs e)

{

FormInfo formInfo = new FormInfo();

formInfo.Show();

this.Hide();

}

private void materialRaisedButtonBack_Click(object sender, EventArgs e)

{

Form formNavigation = Application.OpenForms[1];

formNavigation.Show();

this.Hide();

}

}

}