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


#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::CheckBox^ chkShowPassword;

    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->chkShowPassword = (gcnew System::Windows::Forms::CheckBox());

            this->SuspendLayout();

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

            // label2
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(30, 80);
            this->label2->Size = System::Drawing::Size(140, 13);
            this->label2->Text = L"Зашифрованный файл:";

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

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

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

            // txtPassword
            this->txtPassword->Location = System::Drawing::Point(170, 117);
            this->txtPassword->Size = System::Drawing::Size(200, 20);
            this->txtPassword->PasswordChar = '*';

            // btnBrowseInput
            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);

            // btnBrowseOutput
            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);

            // btnEncrypt
            this->btnEncrypt->Location = System::Drawing::Point(170, 170);
            this->btnEncrypt->Size = System::Drawing::Size(200, 35);
            this->btnEncrypt->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Bold));
            this->btnEncrypt->Text = L"Зашифровать / Расшифровать";
            this->btnEncrypt->Click += gcnew System::EventHandler(this, &Form1::btnEncrypt_Click);

            // chkShowPassword
            this->chkShowPassword->AutoSize = true;
            this->chkShowPassword->Location = System::Drawing::Point(380, 119);
            this->chkShowPassword->Size = System::Drawing::Size(120, 17);
            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, 230);
            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->btnEncrypt);
            this->Controls->Add(this->chkShowPassword);

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

    private:
        // Обзор исходного файла
        System::Void btnBrowseInput_Click(System::Object^ sender, System::EventArgs^ e)
        {
            OpenFileDialog^ openFile = gcnew OpenFileDialog();
            openFile->Title = "Выберите файл для шифрования";
            if (openFile->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                txtInputFile->Text = openFile->FileName;
            }
        }

        // Обзор для выходного файла
        System::Void btnBrowseOutput_Click(System::Object^ sender, System::EventArgs^ e)
        {
            SaveFileDialog^ saveFile = gcnew SaveFileDialog();
            saveFile->Title = "Сохранить зашифрованный файл как...";
            saveFile->Filter = "All files (*.*)|*.*";
            if (saveFile->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                txtOutputFile->Text = saveFile->FileName;
            }
        }

        // Показать/скрыть пароль
        System::Void chkShowPassword_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
        {
            txtPassword->PasswordChar = chkShowPassword->Checked ? '\0' : '*';
        }

        // Основная функция шифрования/дешифрования
        System::Void btnEncrypt_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^ password = txtPassword->Text;
                array<Char>^ passChars = password->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);

                    if (k >= passChars->Length) k = 0; // зацикливаем пароль
                }

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

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