Режимы работы блочных шифров

Автор: Пользователь скрыл имя, 17 Ноября 2012 в 15:07, контрольная работа

Краткое описание

В данной контрольной работе рассматривается реализация программ блочного шифрования данных на языке C# в пяти режимах (ECB, CBC, OFB, CTR, CFB).
В качестве среды разработки выбрана Microsoft Visual Studio 2010.

Оглавление

ВВЕДЕНИЕ 5
1 ОПИСАНИЕ ПРОГРАММЫ 6
2 ПРОЕКТИРОВАНИЕ ПРИЛОЖЕНИЯ 7
3 СХЕМА РАБОТЫ ПРОГРАММЫ 9
3.1 Принцип работы программы 10
ЗАКЛЮЧЕНИЕ 11
СПИСОК ИСПОЛЬЗОВАННЫХ ИСТОЧНИКОВ 12
ПРИЛОЖЕНИЕ А 13

Файлы: 1 файл

контрольная по ИБ.docx

— 149.59 Кб (Скачать)

                        vi[15] = ++count;

 

                        location++;

                    }

 

                }

                my_aes.Cipher(vi, bufEncData);

                for (int z = 0; z < 16; z++)

                {

                    buff[z] = (byte)(buff[z] ^ bufEncData[z]);

                }

                buff.CopyTo(encData, location * 16);

                tbx_shifrText.Text += Encoding.Unicode.GetString(buff);

                Array.Clear(buff, 0, buff.Length);

                flag = 4;

            }

            //Шифр CFB

            if (chk_CFB.Checked)

            {

                tbx_shifrText.Clear();

                tbx_deshifrText.Clear();

                int location = 0;

                byte[] vi = new byte[16];

                iv.CopyTo(vi, 0);

 

                for (int i = 0; i <= tbx_enterText.Text.Length * 2; i++)

                {

                    buff[i % 16] = inData[i];

 

                    if ((i + 1) % 16 == 0)

                    {

                        my_aes.Cipher(vi, bufEncData);

                        for (int z = 0; z < 16; z++)

                        {

                            buff[z] = (byte)(buff[z] ^ bufEncData[z]);

                        }

                        buff.CopyTo(encData, location * 16);

                        buff.CopyTo(vi, 0);

                        tbx_shifrText.Text += Encoding.Unicode.GetString(buff);

                        Array.Clear(buff, 0, buff.Length);

 

                        location++;

                    }

                }

                my_aes.Cipher(vi, bufEncData);

                for (int z = 0; z < 16; z++)

                {

                    buff[z] = (byte)(buff[z] ^ bufEncData[z]);

                }

                buff.CopyTo(encData, location * 16);

                tbx_shifrText.Text += Encoding.Unicode.GetString(buff);

                Array.Clear(buff, 0, buff.Length);

                flag = 5;

            }

 

        }

//======================================================================================

        private void bt_Decoding_Click(object sender, EventArgs e)

        {

            if (chk_ECB.Checked)//расшифровать

            {

                if (flag != 1)

                    ErrMes();

                else

                {

                    for (int i = 0; i <= tbx_shifrText.Text.Length * 2; i++)

                    {

                        buff[i % 16] = encData[i];

                        if ((i + 1) % 16 == 0)

                        {

                            my_aes.InvCipher(buff, decData);

                            Array.Clear(buff, 0, buff.Length);

                            tbx_deshifrText.Text += Encoding.Unicode.GetString(decData);

                        }

 

                    }

                    my_aes.InvCipher(buff, decData);//расшифрование

                    Array.Clear(buff, 0, buff.Length);

                    //переводим указанный массив байтов в строку

                    tbx_deshifrText.Text += Encoding.Unicode.GetString(decData);

                    //удаляем все символы после заданного кол-ва

                    tbx_deshifrText.Text = tbx_deshifrText.Text.Remove(tbx_enterText.Text.Length);

 

                }

 

            }

           

            if (chk_CBC.Checked) //Расшифровать

            {

                if (flag != 2)

                    ErrMes();

                else

                {

                    byte[] vi = new byte[16];

                    iv.CopyTo(vi, 0);

                    for (int i = 0; i <= tbx_shifrText.Text.Length * 2; i++)

                    {

                        buff[i % 16] = encData[i];

 

                        if ((i + 1) % 16 == 0)

                        {

                            my_aes.InvCipher(buff, decData);

                            for (int z = 0; z < 16; z++)

                            {

                                decData[z] = (byte)(decData[z] ^ vi[z]);

                            }

 

                            buff.CopyTo(vi, 0);

                            Array.Clear(buff, 0, buff.Length);

 

                            tbx_deshifrText.Text += Encoding.Unicode.GetString(decData);

                        }

 

                    }

                    my_aes.InvCipher(buff, decData);

                    Array.Clear(buff, 0, buff.Length);

                    for (int z = 0; z < 16; z++)

                    {

                        decData[z] = (byte)(decData[z] ^ vi[z]);

                    }

                    tbx_deshifrText.Text += Encoding.Unicode.GetString(decData);

                    tbx_deshifrText.Text = tbx_deshifrText.Text.Remove(tbx_enterText.Text.Length);

 

                }

 

            }

            //Расшифровать

            if (chk_OFB.Checked)

            {

                if (flag != 3)

                    ErrMes();

                else

                {

                    byte[] vi = new byte[16];

                    iv.CopyTo(vi, 0);

                    for (int i = 0; i <= tbx_shifrText.Text.Length * 2; i++)

                    {

                        buff[i % 16] = encData[i];

 

                        if ((i + 1) % 16 == 0)

                        {

                            my_aes.Cipher(vi, decData);

                            for (int z = 0; z < 16; z++)

                            {

                                buff[z] = (byte)(buff[z] ^ decData[z]);

                            }

 

                            decData.CopyTo(vi, 0);

                            tbx_deshifrText.Text += Encoding.Unicode.GetString(buff);

                            Array.Clear(buff, 0, buff.Length);

 

                        }

 

                    }

                    my_aes.Cipher(vi, decData);

                    for (int z = 0; z < 16; z++)

                    {

                        buff[z] = (byte)(buff[z] ^ decData[z]);

                    }

                    tbx_deshifrText.Text += Encoding.Unicode.GetString(buff);

                    tbx_deshifrText.Text = tbx_deshifrText.Text.Remove(tbx_enterText.Text.Length);

                    Array.Clear(buff, 0, buff.Length);

 

                }

 

            }

            //Расшифровать

            if (chk_CTR.Checked)

            {

                if (flag != 4)

                    ErrMes();

                else

                {

                    byte[] vi = new byte[16];

                    byte count = 0;

                    iv.CopyTo(vi, 0);

                    vi[15] = count;

                    for (int i = 0; i <= tbx_shifrText.Text.Length * 2; i++)

                    {

                        buff[i % 16] = encData[i];

 

                        if ((i + 1) % 16 == 0)

                        {

                            my_aes.Cipher(vi, decData);

                            for (int z = 0; z < 16; z++)

                            {

                                buff[z] = (byte)(buff[z] ^ decData[z]);

                            }

                            tbx_deshifrText.Text += Encoding.Unicode.GetString(buff);

                            vi[15] = ++count;

                            Array.Clear(buff, 0, buff.Length);

 

                        }

 

                    }

                    my_aes.Cipher(vi, decData);

                    for (int z = 0; z < 16; z++)

                    {

                        buff[z] = (byte)(buff[z] ^ decData[z]);

                    }

                    tbx_deshifrText.Text += Encoding.Unicode.GetString(buff);

                    tbx_deshifrText.Text = tbx_deshifrText.Text.Remove(tbx_enterText.Text.Length);

                    Array.Clear(buff, 0, buff.Length);

 

                }

 

            }

            //Расшифровать

            if (chk_CFB.Checked)

            {

                if (flag != 5)

                    ErrMes();

                else

                {

                    byte[] vi = new byte[16];

                    iv.CopyTo(vi, 0);

 

                    for (int i = 0; i <= tbx_shifrText.Text.Length * 2; i++)

                    {

                        buff[i % 16] = encData[i];

 

                        if ((i + 1) % 16 == 0)

                        {

                            my_aes.Cipher(vi, decData);

                            buff.CopyTo(vi, 0);

                            for (int z = 0; z < 16; z++)

                            {

                                buff[z] = (byte)(buff[z] ^ decData[z]);

                            }

                            tbx_deshifrText.Text += Encoding.Unicode.GetString(buff);

 

                            Array.Clear(buff, 0, buff.Length);

                        }

                    }

                    my_aes.Cipher(vi, decData);

                    for (int z = 0; z < 16; z++)

                    {

                        buff[z] = (byte)(buff[z] ^ decData[z]);

                    }

                    tbx_deshifrText.Text += Encoding.Unicode.GetString(buff);

                    tbx_deshifrText.Text = tbx_deshifrText.Text.Remove(tbx_enterText.Text.Length);

                    Array.Clear(buff, 0, buff.Length);

                }

            }

        }

        private void btn_clear_Click(object sender, EventArgs e)

        {

            tbx_enterText.Text = "";

            tbx_shifrText.Text = "";

            tbx_deshifrText.Text = "";

        }

     

    }

}

 

 


Информация о работе Режимы работы блочных шифров