Программа шифрования текста/файла методом одноразовых блокнотов

Автор работы: Пользователь скрыл имя, 09 Апреля 2013 в 09:51, курсовая работа

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

В курсовой работе требуется разработать программу шифрования и дешифрования методом одноразовых блокнотов. К программе предъявляются следующие требования:
- входными данными является произвольный текст или файл;
- возможность шифрования/дешифрования текста или файла;
- возможность генерации случайного текста или файла.

Содержание

Введение 4
1. Разработка алгоритма работы программы 5
2. Кодирование программы 7
3. Проверка и отладка программы 9
4. Оценка результатов работы программы 12
Заключение 13
Список литературы 14
Приложение А 15
Приложение Б 16

Прикрепленные файлы: 1 файл

Курсовая работа.docx

— 553.30 Кб (Скачать документ)

protected:

private:

System::ComponentModel::Container ^components;

static String^ alphavit = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЫЭЮЯ"+

"абвгдежзийклмнопрстуфхцчшщьъыэюя"+

"abcdefghijklmnopqrstuvwxyz"+

"ABCDEFGHIJKLMNOPQRSTUVWXYZ"+

"0123456789 .,/?|';:[]{}-_=+()*&^%$#@!~`";

 

 

Приложение Б

(продолжение)

 

#pragma region Windows Form Designer generated code

void InitializeComponent(void)  {

this->button1 = (gcnew System::Windows::Forms::Button());

this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());

this->label1 = (gcnew System::Windows::Forms::Label());

this->label2 = (gcnew System::Windows::Forms::Label());

this->textBox1 = (gcnew System::Windows::Forms::TextBox());

this->textBox2 = (gcnew System::Windows::Forms::TextBox());

this->button2 = (gcnew System::Windows::Forms::Button());

this->button3 = (gcnew System::Windows::Forms::Button());

this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());

this->label3 = (gcnew System::Windows::Forms::Label());

this->textBox3 = (gcnew System::Windows::Forms::TextBox());

this->label4 = (gcnew System::Windows::Forms::Label());

this->textBox4 = (gcnew System::Windows::Forms::TextBox());

this->button4 = (gcnew System::Windows::Forms::Button());

this->button5 = (gcnew System::Windows::Forms::Button());

this->SuspendLayout();

this->button1->Location = System::Drawing::Point(13, 13);

this->button1->Name = L"button1";

this->button1->Size = System::Drawing::Size(344, 23);

this->button1->TabIndex = 0;

this->button1->Text = L"Генерация блокнота";

this->button1->UseVisualStyleBackColor = true;

this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

this->radioButton1->AutoSize = true;

this->radioButton1->Checked = true;

this->radioButton1->Location = System::Drawing::Point(13, 43);

this->radioButton1->Name = L"radioButton1";

this->radioButton1->Size = System::Drawing::Size(213, 17);

this->radioButton1->TabIndex = 1;

this->radioButton1->TabStop = true;

this->radioButton1->Text = L"Шифрование/дешифрование файлов";

this->radioButton1->UseVisualStyleBackColor = true;

this->radioButton1->Click += gcnew System::EventHandler(this, &Form1::radioButton1_Click);

this->label1->AutoSize = true;

this->label1->Location = System::Drawing::Point(13, 71);

this->label1->Name = L"label1";

this->label1->Size = System::Drawing::Size(61, 13);

this->label1->TabIndex = 2;

this->label1->Text = L"Имя файла";

this->label2->AutoSize = true;

this->label2->Location = System::Drawing::Point(13, 98);

this->label2->Name = L"label2";

this->label2->Size = System::Drawing::Size(77, 13);

this->label2->TabIndex = 3;

this->label2->Text = L"Имя блокнота";

this->textBox1->Location = System::Drawing::Point(102, 64);

this->textBox1->Name = L"textBox1";

this->textBox1->Size = System::Drawing::Size(255, 20);

this->textBox1->TabIndex = 4;

this->textBox1->Text = L"111.txt";

this->textBox2->Location = System::Drawing::Point(102, 91);

this->textBox2->Name = L"textBox2";

this->textBox2->Size = System::Drawing::Size(255, 20);

this->textBox2->TabIndex = 5;

this->textBox2->Text = L"1.snb";

this->button2->Location = System::Drawing::Point(16, 115);

Приложение  Б

(продолжение)

 

this->button2->Name = L"button2";

this->button2->Size = System::Drawing::Size(167, 23);

this->button2->TabIndex = 6;

this->button2->Text = L"Зашифровать файл";

this->button2->UseVisualStyleBackColor = true;

this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);

this->button3->Location = System::Drawing::Point(190, 114);

this->button3->Name = L"button3";

this->button3->Size = System::Drawing::Size(167, 23);

this->button3->TabIndex = 7;

this->button3->Text = L"Расшифровать файл";

this->button3->UseVisualStyleBackColor = true;

this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);

this->radioButton2->AutoSize = true;

this->radioButton2->Location = System::Drawing::Point(16, 154);

this->radioButton2->Name = L"radioButton2";

this->radioButton2->Size = System::Drawing::Size(129, 17);

this->radioButton2->TabIndex = 8;

this->radioButton2->TabStop = true;

this->radioButton2->Text = L"Шифрование текста";

this->radioButton2->UseVisualStyleBackColor = true;

this->radioButton2->Click += gcnew System::EventHandler(this, &Form1::radioButton2_Click);

this->label3->AutoSize = true;

this->label3->Location = System::Drawing::Point(16, 178);

this->label3->Name = L"label3";

this->label3->Size = System::Drawing::Size(94, 13);

this->label3->TabIndex = 9;

this->label3->Text = L"Исходная строка";

this->textBox3->Location = System::Drawing::Point(16, 195);

this->textBox3->Name = L"textBox3";

this->textBox3->Size = System::Drawing::Size(341, 20);

this->textBox3->TabIndex = 10;

this->label4->AutoSize = true;

this->label4->Location = System::Drawing::Point(19, 222);

this->label4->Name = L"label4";

this->label4->Size = System::Drawing::Size(101, 13);

this->label4->TabIndex = 11;

this->label4->Text = L"Текст из блокнота";

this->textBox4->Location = System::Drawing::Point(16, 239);

this->textBox4->Name = L"textBox4";

this->textBox4->Size = System::Drawing::Size(341, 20);

this->textBox4->TabIndex = 12;

this->button4->Location = System::Drawing::Point(16, 266);

this->button4->Name = L"button4";

this->button4->Size = System::Drawing::Size(167, 23);

this->button4->TabIndex = 13;

this->button4->Text = L"Зашифровать строку";

this->button4->UseVisualStyleBackColor = true;

this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);

this->button5->Location = System::Drawing::Point(190, 266);

this->button5->Name = L"button5";

this->button5->Size = System::Drawing::Size(167, 23);

this->button5->TabIndex = 14;

this->button5->Text = L"Расшифровать строку";

this->button5->UseVisualStyleBackColor = true;

Приложение  Б

(продолжение)

 

this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

this->ClientSize = System::Drawing::Size(369, 299);

this->Controls->Add(this->button5);

this->Controls->Add(this->button4);

this->Controls->Add(this->textBox4);

this->Controls->Add(this->label4);

this->Controls->Add(this->textBox3);

this->Controls->Add(this->label3);

this->Controls->Add(this->radioButton2);

this->Controls->Add(this->button3);

this->Controls->Add(this->button2);

this->Controls->Add(this->textBox2);

this->Controls->Add(this->textBox1);

this->Controls->Add(this->label2);

this->Controls->Add(this->label1);

this->Controls->Add(this->radioButton1);

this->Controls->Add(this->button1);

this->Name = L"Form1";

this->Text = L"Form1";

this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);

this->ResumeLayout(false);

this->PerformLayout();

}

#pragma endregion

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

Form2^ f2 = gcnew Form2();

f2->ShowDialog();

}

private: System::Void radioButton1_Click(System::Object^  sender, System::EventArgs^  e) {

 this->textBox3->Enabled = false;

 this->textBox4->Enabled = false;

 this->button4->Enabled = false;

 this->button5->Enabled = false;

 this->textBox1->Enabled = true;

 this->textBox2->Enabled = true;

 this->button2->Enabled = true;

 this->button3->Enabled = true;

}

private: System::Void radioButton2_Click(System::Object^  sender, System::EventArgs^  e) {

 this->textBox3->Enabled = true;

 this->textBox4->Enabled = true;

 this->button4->Enabled = true;

 this->button5->Enabled = true;

 this->textBox1->Enabled = false;

 this->textBox2->Enabled = false;

 this->button2->Enabled = false;

 this->button3->Enabled = false;

}

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {

 this->textBox3->Enabled = false;

 this->textBox4->Enabled = false;

 this->button4->Enabled = false;

 this->button5->Enabled = false;

Приложение  Б

(продолжение)

 

 this->textBox1->Enabled = true;

 this->textBox2->Enabled = true;

 this->button2->Enabled = true;

 this->button3->Enabled = true;

}

private: Int32 InPos(Int32 data){

Int32 count = 0;

 for(;count<alphavit->Length;count++){

 if(data == alphavit[count])break;

}

 return count;

}

private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {

 if(textBox3->Text->Length > textBox4->Text->Length)System::

Windows::Forms::MessageBox::Show("Входная строка меньше чем в блокноте");

 else{

Int32 data;

String^ result = "";

 for(int i=0; i<textBox3->Text->Length; i++){

data  = InPos( textBox3->Text[i] );

data += InPos( textBox4->Text[i] );

 if( !(data<alphavit->Length) )data -= alphavit->Length;

result += alphavit[data];

}

textBox3->Text = result;

}

}

private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) {

 if(textBox3->Text->Length > textBox4->Text->Length)::System::

Windows::Forms::MessageBox::Show("Входная строка меньше чем в блокноте");

 else{

Int32 data;

String^ result = "";

 for(int i=0; i<textBox3->Text->Length; i++){

Char x = InPos( textBox3->Text[i] );

data  = InPos( textBox3->Text[i] );

  x = InPos( textBox4->Text[i] );

data -= InPos( textBox4->Text[i] );

 if( (data<0) )data += alphavit->Length;

  x = alphavit[data];

result += alphavit[data];

}

textBox3->Text = result;

}

}

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {

 using namespace IO;

 if( File::Exists(textBox1->Text) )

if( File::Exists(textBox2->Text) ){

FileStream^ fs1 = File::Open(textBox1->Text,FileMode::Open);

FileStream^ fs2 = File::Open(textBox2->Text,FileMode::Open);

Int32 fSize1 = fs1->Length;

Int32 fSize2 = fs2->Length;

if( fSize2 < fSize1 ){

fs1->Close();

fs2->Close();

Windows::Forms::MessageBox::Show("Файл меньше размера блокнота");

Приложение  Б

(продолжение)

 

}

else{

array<Byte>^ buff = gcnew array<Byte>(fSize1);

fs1->Read(buff,0,fSize1);

fs1->Close();

array<Byte>^ buff2 = gcnew array<Byte>(fSize1);

fs2->Read(buff2,0,fSize1);   

fs2->Close();

for(int i=0; i<fSize1; i++)buff[i] = buff[i] + buff2[i];

fs1 = File::Open(textBox1->Text,FileMode::Create);

fs1->Write(buff,0,fSize1);

fs1->Close();

Windows::Forms::MessageBox::Show("Шифрование успешно проведено");

}

}else Windows::Forms::MessageBox::Show("Файл блокнот не найден");

 else Windows::Forms::MessageBox::Show("Файл не найден");

}

private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {

 using namespace IO;

 if( File::Exists(textBox1->Text) )

if( File::Exists(textBox2->Text) ){

FileStream^ fs1 = File::Open(textBox1->Text,FileMode::Open);

FileStream^ fs2 = File::Open(textBox2->Text,FileMode::Open);

Int32 fSize1 = fs1->Length;

Int32 fSize2 = fs2->Length;

if( fSize2 < fSize1 ){

fs1->Close();

fs2->Close();

Windows::Forms::MessageBox::Show("Файл меньше размера блокнота");

}

else{

array<Byte>^ buff = gcnew array<Byte>(fSize1);

fs1->Read(buff,0,fSize1);

fs1->Close();

array<Byte>^ buff2 = gcnew array<Byte>(fSize1);

fs2->Read(buff2,0,fSize1);   

fs2->Close();

for(int i=0; i<fSize1; i++)buff[i] = buff[i] - buff2[i];

fs1 = File::Open(textBox1->Text,FileMode::Create);

fs1->Write(buff,0,fSize1);

fs1->Close();

Windows::Forms::MessageBox::Show("Расшифрование успешно проведено");

}

}else Windows::Forms::MessageBox::Show("Файл блокнот не найден");

 else Windows::Forms::MessageBox::Show("Файл не найден");

 }

};

}

 

Листинг файла «Form2.h»

#pragma once

using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

namespace Kurs {

Приложение  Б

(продолжение)

 

 

public ref class Form2 : public System::Windows::Forms::Form {

public:

Form2(void)  {

InitializeComponent();

}

protected:

~Form2()  {

if (components)   {

delete components;

}

}

private: System::Windows::Forms::RadioButton^  radioButton1;

private: System::Windows::Forms::Panel^  panel1;

private: System::Windows::Forms::Button^  button1;

private: System::Windows::Forms::TextBox^  textBox2;

private: System::Windows::Forms::Label^  label2;

private: System::Windows::Forms::TextBox^  textBox1;

private: System::Windows::Forms::Label^  label1;

private: System::Windows::Forms::Panel^  panel2;

private: System::Windows::Forms::Button^  button2;

private: System::Windows::Forms::TextBox^  textBox3;

private: System::Windows::Forms::Label^  label3;

private: System::Windows::Forms::TextBox^  textBox4;

private: System::Windows::Forms::Label^  label4;

private: System::Windows::Forms::RadioButton^  radioButton2;

private:

System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

void InitializeComponent(void)  {

this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());

this->panel1 = (gcnew System::Windows::Forms::Panel());

this->button1 = (gcnew System::Windows::Forms::Button());

this->textBox2 = (gcnew System::Windows::Forms::TextBox());

this->label2 = (gcnew System::Windows::Forms::Label());

this->textBox1 = (gcnew System::Windows::Forms::TextBox());

this->label1 = (gcnew System::Windows::Forms::Label());

this->panel2 = (gcnew System::Windows::Forms::Panel());

this->button2 = (gcnew System::Windows::Forms::Button());

this->textBox3 = (gcnew System::Windows::Forms::TextBox());

this->label3 = (gcnew System::Windows::Forms::Label());

this->textBox4 = (gcnew System::Windows::Forms::TextBox());

this->label4 = (gcnew System::Windows::Forms::Label());

this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());

this->panel1->SuspendLayout();

this->panel2->SuspendLayout();

this->SuspendLayout();

this->radioButton1->AutoSize = true;

this->radioButton1->Checked = true;

this->radioButton1->Location = System::Drawing::Point(13, 13);

this->radioButton1->Name = L"radioButton1";

this->radioButton1->Size = System::Drawing::Size(319, 17);

this->radioButton1->TabIndex = 1;

this->radioButton1->TabStop = true;

this->radioButton1->Text = L"Создание бинарного блокнота (для шифрования файлов)";

this->radioButton1->UseVisualStyleBackColor = true;

this->radioButton1->Click += gcnew System::EventHandler(this, &Form2::radioButton1_Click);

this->panel1->Controls->Add(this->button1);

Приложение  Б

(продолжение)

 

this->panel1->Controls->Add(this->textBox2);

this->panel1->Controls->Add(this->label2);

this->panel1->Controls->Add(this->textBox1);

this->panel1->Controls->Add(this->label1);

this->panel1->Location = System::Drawing::Point(13, 37);

this->panel1->Name = L"panel1";

this->panel1->Size = System::Drawing::Size(371, 91);

this->panel1->TabIndex = 2;

this->button1->Location = System::Drawing::Point(3, 54);

this->button1->Name = L"button1";

this->button1->Size = System::Drawing::Size(364, 23);

this->button1->TabIndex = 4;

this->button1->Text = L"Создать";

this->button1->UseVisualStyleBackColor = true;

this->button1->Click += gcnew System::EventHandler(this, &Form2::button1_Click);

this->textBox2->Location = System::Drawing::Point(150, 27);

this->textBox2->Name = L"textBox2";

this->textBox2->Size = System::Drawing::Size(218, 20);

this->textBox2->TabIndex = 3;

this->textBox2->Text = L"1000";

this->label2->AutoSize = true;

this->label2->Location = System::Drawing::Point(4, 27);

this->label2->Name = L"label2";

this->label2->Size = System::Drawing::Size(139, 13);

this->label2->TabIndex = 2;

this->label2->Text = L"Размера файла (в Байтах)";

this->textBox1->Location = System::Drawing::Point(72, 4);

this->textBox1->Name = L"textBox1";

this->textBox1->Size = System::Drawing::Size(296, 20);

this->textBox1->TabIndex = 1;

this->textBox1->Text = L"1.snb";

this->label1->AutoSize = true;

this->label1->Location = System::Drawing::Point(4, 4);

this->label1->Name = L"label1";

this->label1->Size = System::Drawing::Size(61, 13);

this->label1->TabIndex = 0;

this->label1->Text = L"Имя файла";

this->panel2->Controls->Add(this->button2);

this->panel2->Controls->Add(this->textBox3);

this->panel2->Controls->Add(this->label3);

this->panel2->Controls->Add(this->textBox4);

this->panel2->Controls->Add(this->label4);

this->panel2->Location = System::Drawing::Point(13, 156);

this->panel2->Name = L"panel2";

this->panel2->Size = System::Drawing::Size(371, 104);

this->panel2->TabIndex = 4;

this->button2->Location = System::Drawing::Point(4, 72);

this->button2->Name = L"button2";

this->button2->Size = System::Drawing::Size(364, 23);

this->button2->TabIndex = 4;

this->button2->Text = L"Генерировать и сохранить в буфере обмена";

this->button2->UseVisualStyleBackColor = true;

this->button2->Click += gcnew System::EventHandler(this, &Form2::button2_Click);

this->textBox3->Location = System::Drawing::Point(149, 46);

this->textBox3->Name = L"textBox3";

this->textBox3->Size = System::Drawing::Size(218, 20);

this->textBox3->TabIndex = 3;

this->textBox3->Text = L"10";

Приложение  Б

(продолжение)

 

this->label3->AutoSize = true;

this->label3->Location = System::Drawing::Point(3, 46);

this->label3->Name = L"label3";

Информация о работе Программа шифрования текста/файла методом одноразовых блокнотов