Загрузка данных


#pragma once

#include <fstream>
#include <msclr/marshal_cppstd.h>

namespace CryptoForm {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Windows::Forms;
	using namespace System::IO;
	using namespace std;

	public ref class Form1 : public Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
		}

	protected:
		~Form1()
		{
			if (components)
				delete components;
		}

	private:
		TextBox^ txtInput;
		TextBox^ txtOutput;
		TextBox^ txtPassword;
		Button^ btnRun;
		Label^ lbl1;
		Label^ lbl2;
		Label^ lbl3;

		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		void InitializeComponent(void)
		{
			this->txtInput = gcnew TextBox();
			this->txtOutput = gcnew TextBox();
			this->txtPassword = gcnew TextBox();
			this->btnRun = gcnew Button();
			this->lbl1 = gcnew Label();
			this->lbl2 = gcnew Label();
			this->lbl3 = gcnew Label();

			this->SuspendLayout();

			// Labels
			this->lbl1->Text = "Входной файл:";
			this->lbl1->Location = System::Drawing::Point(20, 5);

			this->lbl2->Text = "Выходной файл:";
			this->lbl2->Location = System::Drawing::Point(20, 45);

			this->lbl3->Text = "Пароль:";
			this->lbl3->Location = System::Drawing::Point(20, 85);

			// txtInput
			this->txtInput->Location = System::Drawing::Point(20, 20);
			this->txtInput->Width = 250;

			// txtOutput
			this->txtOutput->Location = System::Drawing::Point(20, 60);
			this->txtOutput->Width = 250;

			// txtPassword
			this->txtPassword->Location = System::Drawing::Point(20, 100);
			this->txtPassword->Width = 250;

			// btnRun
			this->btnRun->Text = "Шифровать / Дешифровать";
			this->btnRun->Location = System::Drawing::Point(20, 140);
			this->btnRun->Width = 250;
			this->btnRun->Click += gcnew EventHandler(this, &Form1::RunCrypto);

			// Form
			this->ClientSize = System::Drawing::Size(300, 200);
			this->Controls->Add(this->lbl1);
			this->Controls->Add(this->lbl2);
			this->Controls->Add(this->lbl3);
			this->Controls->Add(this->txtInput);
			this->Controls->Add(this->txtOutput);
			this->Controls->Add(this->txtPassword);
			this->Controls->Add(this->btnRun);
			this->Text = "XOR Шифратор";

			this->ResumeLayout(false);
			this->PerformLayout();
		}
#pragma endregion

	private:
		void RunCrypto(Object^ sender, EventArgs^ e)
		{
			std::string inFile = msclr::interop::marshal_as<std::string>(txtInput->Text);
			std::string outFile = msclr::interop::marshal_as<std::string>(txtOutput->Text);
			std::string password = msclr::interop::marshal_as<std::string>(txtPassword->Text);

			ifstream f_in(inFile.c_str(), ios::binary);
			ofstream f_out(outFile.c_str(), ios::binary);

			if (!f_in || !f_out)
			{
				MessageBox::Show("Ошибка открытия файла!");
				return;
			}

			unsigned char b;
			int k = 0;

			while (f_in.read((char*)&b, sizeof(b)))
			{
				b = b ^ password[k++];
				if (k >= password.length()) k = 0;

				f_out.write((char*)&b, sizeof(b));
			}

			f_in.close();
			f_out.close();

			MessageBox::Show("Готово!");
		}
	};
}