#pragma once
#include <fstream>
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;
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->SuspendLayout();
// txtInput
this->txtInput->Location = System::Drawing::Point(20, 20);
this->txtInput->Width = 250;
this->txtInput->PlaceholderText = "Входной файл";
// txtOutput
this->txtOutput->Location = System::Drawing::Point(20, 60);
this->txtOutput->Width = 250;
this->txtOutput->PlaceholderText = "Выходной файл";
// txtPassword
this->txtPassword->Location = System::Drawing::Point(20, 100);
this->txtPassword->Width = 250;
this->txtPassword->PlaceholderText = "Пароль";
// btnRun
this->btnRun->Text = "Шифровать / Дешифровать";
this->btnRun->Location = System::Drawing::Point(20, 140);
this->btnRun->Click += gcnew EventHandler(this, &Form1::RunCrypto);
// Form
this->ClientSize = System::Drawing::Size(300, 200);
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)
{
string inFile = msclr::interop::marshal_as<string>(txtInput->Text);
string outFile = msclr::interop::marshal_as<string>(txtOutput->Text);
string password = msclr::interop::marshal_as<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("Готово!");
}
};
}