Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

нав3

.doc
Скачиваний:
11
Добавлен:
19.02.2016
Размер:
43.52 Кб
Скачать

Лабораторна робота №3

Ярослав Садловський ІАН-316

Скрін програми

Код програми

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Security.Cryptography;

namespace lab3

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

int gamma;

int randgam;

byte[] arraybytes;

int condition;

int[] blocks;

int nbg;

private void button1_Click(object sender, EventArgs e)

{

arraybytes = Encoding.ASCII.GetBytes(textBox1.Text);

nbg = (int)numericUpDown1.Value;

condition = arraybytes.Length % nbg == 0 ? arraybytes.Length / nbg : arraybytes.Length / nbg + 1;

blocks = new int[condition];

//Ділення на данних на блоки

DivisionByBlocks();

if (nbg==1)

{

gamma=blocks[0];

}

//Випадок випадкової гамми

if (checkBox1.Checked)

{

#region

RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();

byte[] rand = new byte[nbg];

rngCsp.GetNonZeroBytes(rand);

gamma = rand[0];

// Генерація випадкової гамми

for (int j = 1; j < nbg; j++)

{

gamma = (gamma << (8)) | rand[j];

}

randgam = gamma;

// Гамування випадковою гаммою

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

{

int n = blocks[i] ^ gamma;

gamma = (byte)(n);

arraybytes[i ] = Convert.ToByte(gamma);

}

#endregion

}

else

{

//Операція гамування

gamma = blocks[0];

for (int i = 0; i < blocks.Length - 1; i++)

{

int n = blocks[i + 1] ^ gamma;

gamma = (byte)(n);

arraybytes[i + 1] = Convert.ToByte(gamma);

}

}

DivisionByBlocks();

textBox1.Text = BitConverter.ToString(arraybytes);

}

private void DivisionByBlocks()

{

for (int k = 0; k < condition; k++)

{

if (k == arraybytes.Length / nbg && arraybytes.Length != 1)

{

gamma = arraybytes[nbg * k];

for (int j = 1; j < arraybytes.Length % nbg; j++)

{

gamma = (gamma << (8)) | arraybytes[j + nbg * k];

}

blocks[k] = gamma;

break;

}

gamma = arraybytes[nbg * k];

for (int j = 1; j < nbg; j++)

{

gamma = (gamma << (8)) | arraybytes[j + nbg * k];

}

blocks[k] = gamma;

}

}

private void button3_Click(object sender, EventArgs e)

{

textBox2.Text = Encoding.ASCII.GetString(arraybytes);

}

private void button2_Click(object sender, EventArgs e)

{

byte[] reveresegamma = new byte[arraybytes.Length];

arraybytes.CopyTo(reveresegamma,0);

if (checkBox1.Checked)

{

#region

//Зворотнє гамування випадковою гаммою

int h = blocks[0] ^ randgam;

gamma = (byte)(h);

reveresegamma[0] = Convert.ToByte(gamma);

for (int i = 1; i < blocks.Length; i++)

{

int n = blocks[i] ^ blocks[i-1];

gamma = (byte)(n);

reveresegamma[i] = Convert.ToByte(gamma);

}

#endregion

}

else

{

//Операція зворотнього гамування

for (int i = 0; i < blocks.Length - 1; i++)

{

int n = blocks[i + 1] ^ blocks[i];

gamma = (byte)(n);

reveresegamma[i + 1] = Convert.ToByte(gamma);

}

}

textBox1.Text = BitConverter.ToString(reveresegamma);

reveresegamma.CopyTo(arraybytes, 0);

}

}

}