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


#pragma once

namespace shi {

    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::TextBox^ txtInputFile;
        System::Windows::Forms::TextBox^ txtOutputFile;
        System::Windows::Forms::Button^ btnBrowseInput;
        System::Windows::Forms::Button^ btnBrowseOutput;
        System::Windows::Forms::Button^ btnAction;
        System::Windows::Forms::RadioButton^ rbEncrypt;
        System::Windows::Forms::RadioButton^ rbDecrypt;

        const unsigned char KEY = 0xAA;   // ← Фиксированный ключ XOR (можно изменить)

    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->txtInputFile = (gcnew System::Windows::Forms::TextBox());
            this->txtOutputFile = (gcnew System::Windows::Forms::TextBox());
            this->btnBrowseInput = (gcnew System::Windows::Forms::Button());
            this->btnBrowseOutput = (gcnew System::Windows::Forms::Button());
            this->btnAction = (gcnew System::Windows::Forms::Button());
            this->rbEncrypt = (gcnew System::Windows::Forms::RadioButton());
            this->rbDecrypt = (gcnew System::Windows::Forms::RadioButton());

            this->SuspendLayout();

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

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

            // TextBoxes
            this->txtInputFile->Location = System::Drawing::Point(170, 37);
            this->txtInputFile->Size = System::Drawing::Size(280, 20);

            this->txtOutputFile->Location = System::Drawing::Point(170, 77);
            this->txtOutputFile->Size = System::Drawing::Size(280, 20);

            // Кнопки Обзор
            this->btnBrowseInput->Location = System::Drawing::Point(460, 36);
            this->btnBrowseInput->Size = System::Drawing::Size(75, 23);
            this->btnBrowseInput->Text = L"Обзор...";
            this->btnBrowseInput->Click += gcnew System::EventHandler(this, &Form1::btnBrowseInput_Click);

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

            // Радиокнопки
            this->rbEncrypt->AutoSize = true;
            this->rbEncrypt->Location = System::Drawing::Point(170, 125);
            this->rbEncrypt->Text = L"Зашифровать";
            this->rbEncrypt->Checked = true;

            this->rbDecrypt->AutoSize = true;
            this->rbDecrypt->Location = System::Drawing::Point(300, 125);
            this->rbDecrypt->Text = L"Расшифровать";

            // Кнопка Выполнить
            this->btnAction->Location = System::Drawing::Point(170, 170);
            this->btnAction->Size = System::Drawing::Size(220, 45);
            this->btnAction->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Bold));
            this->btnAction->Text = L"ВЫПОЛНИТЬ";
            this->btnAction->Click += gcnew System::EventHandler(this, &Form1::btnAction_Click);

            // Форма
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(580, 240);
            this->Text = L"Простое XOR шифрование файлов (без пароля)";
            this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;

            this->Controls->Add(this->label1);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->txtInputFile);
            this->Controls->Add(this->txtOutputFile);
            this->Controls->Add(this->btnBrowseInput);
            this->Controls->Add(this->btnBrowseOutput);
            this->Controls->Add(this->btnAction);
            this->Controls->Add(this->rbEncrypt);
            this->Controls->Add(this->rbDecrypt);

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

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

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

        System::Void btnBrowseOutput_Click(System::Object^ sender, System::EventArgs^ e)
        {
            SaveFileDialog^ dlg = gcnew SaveFileDialog();
            dlg->Filter = "Все файлы (*.*)|*.*";
            if (dlg->ShowDialog() == System::Windows::Forms::DialogResult::OK)
                txtOutputFile->Text = dlg->FileName;
        }

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

            try
            {
                FileStream^ fsIn = gcnew FileStream(txtInputFile->Text, FileMode::Open, FileAccess::Read);
                FileStream^ fsOut = gcnew FileStream(txtOutputFile->Text, FileMode::Create, FileAccess::Write);

                while (true)
                {
                    int byteRead = fsIn->ReadByte();
                    if (byteRead == -1) break;

                    unsigned char b = static_cast<unsigned char>(byteRead);
                    b = b ^ KEY;                    // ← XOR с фиксированным ключом
                    fsOut->WriteByte(b);
                }

                fsIn->Close();
                fsOut->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);
            }
        }
    };
}