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


#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::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^ btnAction;
        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->btnAction = (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();

            // 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"Выходной файл:";

            // label3
            this->label3->AutoSize = true;
            this->label3->Location = System::Drawing::Point(30, 120);
            this->label3->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->txtPassword->Location = System::Drawing::Point(170, 117);
            this->txtPassword->Size = System::Drawing::Size(200, 20);
            this->txtPassword->PasswordChar = '*';

            // Кнопки Обзор
            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, 155);
            this->rbEncrypt->Text = L"Зашифровать";
            this->rbEncrypt->Checked = true;

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

            // Кнопка Выполнить
            this->btnAction->Location = System::Drawing::Point(170, 190);
            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->chkShowPassword->AutoSize = true;
            this->chkShowPassword->Location = System::Drawing::Point(380, 118);
            this->chkShowPassword->Text = L"Показать пароль";
            this->chkShowPassword->CheckedChanged += gcnew System::EventHandler(this, &Form1::chkShowPassword_CheckedChanged);

            // Форма
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(580, 270);
            this->Text = L"Шифрование файлов XOR — Visual Studio 2008";
            this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;

            this->Controls->Add(this->label1);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->label3);
            this->Controls->Add(this->txtInputFile);
            this->Controls->Add(this->txtOutputFile);
            this->Controls->Add(this->txtPassword);
            this->Controls->Add(this->btnBrowseInput);
            this->Controls->Add(this->btnBrowseOutput);
            this->Controls->Add(this->btnAction);
            this->Controls->Add(this->chkShowPassword);
            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();
            dlg->Title = "Выберите файл";
            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 chkShowPassword_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
        {
            txtPassword->PasswordChar = chkShowPassword->Checked ? '\0' : '*';
        }

        // Главная функция шифрования / дешифрования
        System::Void btnAction_Click(System::Object^ sender, System::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^ fsIn = gcnew FileStream(txtInputFile->Text, FileMode::Open, FileAccess::Read);
                FileStream^ fsOut = gcnew FileStream(txtOutputFile->Text, FileMode::Create, FileAccess::Write);

                int k = 0;
                while (true)
                {
                    int byteRead = fsIn->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 — работает и для шифрования, и для расшифровки

                    fsOut->WriteByte(b);

                    k = (k + 1) % passChars->Length;
                }

                fsIn->Close();
                fsOut->Close();

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