Скачиваний:
36
Добавлен:
01.05.2014
Размер:
349.7 Кб
Скачать

Приложение 1: Листинг программы Главная программа:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Security.Cryptography;

using System.IO;

using GOST;

using Gost2;

namespace Gost2

{

public partial class MainForm : Form

{

public byte[] plain_text;

public byte[] strbytes;

byte[] key = new Byte[32] { 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0 };

byte[] iv = new Byte[8];

byte[] decrypted_text;

byte[] cipher_text;

public MainForm()

{

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

{

Random x = new Random();

iv[i] = (byte)x.Next();

}

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

{

Random x2 = new Random();

//key[i] = (byte)x2.Next();

}

InitializeComponent();

}

private void MainForm_Load(object sender, EventArgs e)

{

}

private void label1_Click(object sender, EventArgs e)

{

}

private void buttonShifrate_Click(object sender, EventArgs e)

{

if (textBoxSource.Text == "" )//|| textBoxKey.Text == "")

{

MessageBox.Show("Сперва введите сообщение", "Л.Р.2 ГОСТ 28147-89",

MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

}

else

{

this.plain_text = new Byte[512];

System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

this.strbytes = enc.GetBytes(textBoxSource.Text);

Array.Copy(strbytes, 0, plain_text, 0, strbytes.Length);

//Console.WriteLine("Plain text to encrypt: \""+plain_text.ToString()+"\"");

textBoxSrcCode.Text = Program.PrintByteArray(plain_text);

GOSTsymmManaged gost = new GOSTsymmManaged();

gost.LoadTestSBoxes(2);

gost.Key = key;

gost.IV = iv;

gost.Mode = CipherMode.CBC;

GOSTsymmTransform ct_e = (GOSTsymmTransform)gost.CreateEncryptor();

ct_e.UseExpandedSBoxes = false;

GOSTsymmTransform ct_d = (GOSTsymmTransform)gost.CreateDecryptor();

MemoryStream ms1 = new MemoryStream();

MemoryStream ms2 = new MemoryStream();

CryptoStream cs1 = new CryptoStream(ms1, ct_e, CryptoStreamMode.Write);

cs1.Write(plain_text, 0, plain_text.Length);

cs1.Close();

cipher_text = ms1.ToArray();

//Console.WriteLine("Cipher text:" + cipher_text.ToString() + "\"");

textBoxShifr.Text = Program.PrintByteArray(cipher_text);

CryptoStream cs2 = new CryptoStream(ms2, ct_d, CryptoStreamMode.Write);

cs2.Write(cipher_text, 0, cipher_text.Length);

cs2.Close();

decrypted_text = ms2.ToArray();

//Console.WriteLine("DecryptedText:"+decrypted_text.ToString()+"\"");

//textBoxDeshifrated.Text = Program.PrintByteArray(decrypted_text);

//Console.WriteLine("Done.");

gost.GenerateKey();

textBoxKey.Text = Program.PrintByteArray(gost.getKey());

}

}

private void buttonExit_Click(object sender, EventArgs e)

{

if (MessageBox.Show("Действительно выйти?", "Л.Р.2 ГОСТ 28147-89",

MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)

this.Close();

}

private void buttonClear_Click(object sender, EventArgs e)

{

plain_text=null;

strbytes = null ;

decrypted_text=null;

cipher_text=null;

textBoxSource.Text = "";

textBoxSrcCode.Text = "";

textBoxShifr.Text = "";

textBoxDeshifrated.Text = "";

textBoxKey.Text = "";

}

private void buttonDeshifrate_Click(object sender, EventArgs e)

{

textBoxDeshifrated.Text = Program.PrintByteArray(decrypted_text);

}

private void label6_Click(object sender, EventArgs e)

{

}

}

}