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


#pragma once

namespace FileEncryptor {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::IO;

    public ref class Form1 : public System::Windows::Forms::Form
    {
    private:
        System::Windows::Forms::Label^ label1;
        System::Windows::Forms::Label^ label2;
        System::Windows::Forms::Label^ label3;
        System::Windows::Forms::TextBox^ txtInputFile;
        System::Windows::Forms::TextBox^ txtOutputFile;
        System::Windows::Forms::TextBox^ txtPassword;
        System::Windows::Forms::Button^ btnBrowseInput;
        System::Windows::Forms::Button^ btnBrowseOutput;
        System::Windows::Forms::Button^ btnEncrypt;
        System::Windows::Forms::Button^ btnDecrypt;
        System::Windows::Forms::CheckBox^ chkShowPassword;
        System::Windows::Forms::RadioButton^ rbEncrypt;
        System::Windows::Forms::RadioButton^ rbDecrypt;

    public:
        Form1(void) { InitializeComponent(); }

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

    private:
        System::ComponentModel::Container^ components;

#pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->label3 = (gcnew System::Windows::Forms::Label());
            this->txtInputFile = (gcnew System::Windows::Forms::TextBox());
            this->txtOutputFile = (gcnew System::Windows::Forms::TextBox());
            this->txtPassword = (gcnew System::Windows::Forms::TextBox());
            this->btnBrowseInput = (gcnew System::Windows::Forms::Button());
            this->btnBrowseOutput = (gcnew System::Windows::Forms::Button());
            this->btnEncrypt = (gcnew System::Windows::Forms::Button());
            this->btnDecrypt = (gcnew System::Windows::Forms::Button());
            this->chkShowPassword = (gcnew System::Windows::Forms::CheckBox());
            this->rbEncrypt = (gcnew System::Windows::Forms::RadioButton());
            this->rbDecrypt = (gcnew System::Windows::Forms::RadioButton());

            this->SuspendLayout();

            // Labels
            this->label1->Text = L"Исходный файл:";
            this->label1->Location = Point(30, 40);
            this->label1->AutoSize = true;

            this->label2->Text = L"Выходной файл:";
            this->label2->Location = Point(30, 80);
            this->label2->AutoSize = true;

            this->label3->Text = L"Пароль:";
            this->label3->Location = Point(30, 120);
            this->label3->AutoSize = true;

            // TextBoxes
            this->txtInputFile->Location = Point(170, 37); this->txtInputFile->Size = Size(280, 20);
            this->txtOutputFile->Location = Point(170, 77); this->txtOutputFile->Size = Size(280, 20);
            this->txtPassword->Location = Point(170, 117); this->txtPassword->Size = Size(200, 20);
            this->txtPassword->PasswordChar = '*';

            // Browse buttons
            this->btnBrowseInput->Text = L"Обзор..."; this->btnBrowseInput->Location = Point(460, 36);
            this->btnBrowseInput->Click += gcnew EventHandler(this, &Form1::btnBrowseInput_Click);

            this->btnBrowseOutput->Text = L"Обзор..."; this->btnBrowseOutput->Location = Point(460, 76);
            this->btnBrowseOutput->Click += gcnew EventHandler(this, &Form1::btnBrowseOutput_Click);

            // Radio buttons
            this->rbEncrypt->Text = L"Зашифровать"; this->rbEncrypt->Location = Point(170, 155); this->rbEncrypt->Checked = true;
            this->rbDecrypt->Text = L"Расшифровать"; this->rbDecrypt->Location = Point(300, 155);

            // Action buttons
            this->btnEncrypt->Text = L"ВЫПОЛНИТЬ";
            this->btnEncrypt->Location = Point(170, 190);
            this->btnEncrypt->Size = Size(200, 40);
            this->btnEncrypt->Font = gcnew Drawing::Font(L"Microsoft Sans Serif", 10, FontStyle::Bold);
            this->btnEncrypt->Click += gcnew EventHandler(this, &Form1::btnAction_Click);

            this->chkShowPassword->Text = L"Показать пароль";
            this->chkShowPassword->Location = Point(380, 118);
            this->chkShowPassword->CheckedChanged += gcnew EventHandler(this, &Form1::chkShowPassword_CheckedChanged);

            // Form settings
            this->Text = L"Шифрование файлов XOR — Visual Studio 2008";
            this->ClientSize = Size(580, 260);
            this->StartPosition = FormStartPosition::CenterScreen;

            this->Controls->AddRange(gcnew array<Control^> {
                label1, label2, label3, txtInputFile, txtOutputFile, txtPassword,
                btnBrowseInput, btnBrowseOutput, rbEncrypt, rbDecrypt,
                btnEncrypt, chkShowPassword
            });

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

        // ==================== Обработчики ====================

        System::Void btnBrowseInput_Click(Object^ sender, EventArgs^ e)
        {
            OpenFileDialog^ dlg = gcnew OpenFileDialog();
            if (dlg->ShowDialog() == System::Windows::Forms::DialogResult::OK)
                txtInputFile->Text = dlg->FileName;
        }

        System::Void btnBrowseOutput_Click(Object^ sender, EventArgs^ e)
        {
            SaveFileDialog^ dlg = gcnew SaveFileDialog();
            dlg->Filter = "All files (*.*)|*.*";
            if (dlg->ShowDialog() == System::Windows::Forms::DialogResult::OK)
                txtOutputFile->Text = dlg->FileName;
        }

        System::Void chkShowPassword_CheckedChanged(Object^ sender, EventArgs^ e)
        {
            txtPassword->PasswordChar = chkShowPassword->Checked ? '\0' : '*';
        }

        // Главная функция (работает и для шифрования, и для дешифровки)
        System::Void btnAction_Click(Object^ sender, EventArgs^ e)
        {
            if (String::IsNullOrEmpty(txtInputFile->Text) ||
                String::IsNullOrEmpty(txtOutputFile->Text) ||
                String::IsNullOrEmpty(txtPassword->Text))
            {
                MessageBox::Show("Заполните все поля!", "Ошибка", MessageBoxButtons::OK, MessageBoxIcon::Warning);
                return;
            }

            try
            {
                String^ pass = txtPassword->Text;
                array<Char>^ passChars = pass->ToCharArray();

                FileStream^ inFile = gcnew FileStream(txtInputFile->Text, FileMode::Open, FileAccess::Read);
                FileStream^ outFile = gcnew FileStream(txtOutputFile->Text, FileMode::Create, FileAccess::Write);

                int k = 0;
                while (true)
                {
                    int byteRead = inFile->ReadByte();
                    if (byteRead == -1) break;

                    unsigned char b = static_cast<unsigned char>(byteRead);
                    unsigned char p = static_cast<unsigned char>(passChars[k]);

                    b = b ^ p;                     // ← XOR (и шифрование, и дешифрование)

                    outFile->WriteByte(b);

                    k = (k + 1) % passChars->Length; // зацикливание пароля
                }

                inFile->Close();
                outFile->Close();

                String^ mode = rbEncrypt->Checked ? "зашифрован" : "расшифрован";
                MessageBox::Show("Файл успешно " + mode + "!\n\n" + txtOutputFile->Text,
                    "Успешно", MessageBoxButtons::OK, MessageBoxIcon::Information);
            }
            catch (Exception^ ex)
            {
                MessageBox::Show("Ошибка: " + ex->Message, "Ошибка", MessageBoxButtons::OK, MessageBoxIcon::Error);
            }
        }
    };
}