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


#pragma once

namespace Password {

    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:
        int attempts;
        String^ passFile;
        String^ secretFile;
        String^ decryptedFile;
        unsigned char KEY;

        Label^ lblTitle;
        Label^ lblPassword;
        TextBox^ txtPassword;
        Button^ btnOK;
        Button^ btnCancel;

        Label^ lblSecretTitle;
        TextBox^ txtSecret;
        Button^ btnChangePassword;
        Button^ btnExit;

        Panel^ panelLogin;
        Panel^ panelMain;

    public:
        Form1(void)
        {
            attempts = 3;
            passFile = "password.dat";
            secretFile = "secret.txt";
            decryptedFile = "secret_decrypted.txt";
            KEY = 0xAA;

            InitializeComponent();
            LoadOrCreatePassword();
            CreateSecretFileIfNotExists();
        }

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

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

#pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
            this->panelLogin = gcnew Panel();
            this->panelMain = gcnew Panel();

            this->lblTitle = gcnew Label();
            this->lblPassword = gcnew Label();
            this->txtPassword = gcnew TextBox();
            this->btnOK = gcnew Button();
            this->btnCancel = gcnew Button();

            this->lblSecretTitle = gcnew Label();
            this->txtSecret = gcnew TextBox();
            this->btnChangePassword = gcnew Button();
            this->btnExit = gcnew Button();

            // === Панель ввода пароля ===
            this->panelLogin->Location = Point(0, 0);
            this->panelLogin->Size = Size(420, 280);
            this->panelLogin->Controls->Add(this->lblTitle);
            this->panelLogin->Controls->Add(this->lblPassword);
            this->panelLogin->Controls->Add(this->txtPassword);
            this->panelLogin->Controls->Add(this->btnOK);
            this->panelLogin->Controls->Add(this->btnCancel);

            this->lblTitle->AutoSize = true;
            this->lblTitle->Font = gcnew Drawing::Font(L"Microsoft Sans Serif", 14, FontStyle::Bold);
            this->lblTitle->Location = Point(90, 40);
            this->lblTitle->Text = L"Парольная защита";

            this->lblPassword->AutoSize = true;
            this->lblPassword->Location = Point(50, 100);
            this->lblPassword->Text = L"Введите пароль:";

            this->txtPassword->Location = Point(50, 130);
            this->txtPassword->Size = Size(300, 25);
            this->txtPassword->PasswordChar = '*';

            this->btnOK->Location = Point(50, 180);
            this->btnOK->Size = Size(120, 35);
            this->btnOK->Text = L"Войти";
            this->btnOK->Click += gcnew System::EventHandler(this, &Form1::btnOK_Click);

            this->btnCancel->Location = Point(190, 180);
            this->btnCancel->Size = Size(120, 35);
            this->btnCancel->Text = L"Отмена";
            this->btnCancel->Click += gcnew System::EventHandler(this, &Form1::btnCancel_Click);

            // === Главная панель (после успешного входа) ===
            this->panelMain->Location = Point(0, 0);
            this->panelMain->Size = Size(420, 280);
            this->panelMain->Visible = false;
            this->panelMain->Controls->Add(this->lblSecretTitle);
            this->panelMain->Controls->Add(this->txtSecret);
            this->panelMain->Controls->Add(this->btnChangePassword);
            this->panelMain->Controls->Add(this->btnExit);

            this->lblSecretTitle->AutoSize = true;
            this->lblSecretTitle->Font = gcnew Drawing::Font(L"Microsoft Sans Serif", 12, FontStyle::Bold);
            this->lblSecretTitle->Location = Point(40, 20);
            this->lblSecretTitle->Text = L"Доступ разрешён!\nСекретная информация:";

            this->txtSecret->Location = Point(40, 70);
            this->txtSecret->Size = Size(340, 140);
            this->txtSecret->Multiline = true;
            this->txtSecret->ScrollBars = ScrollBars::Vertical;
            this->txtSecret->ReadOnly = true;

            this->btnChangePassword->Location = Point(40, 225);
            this->btnChangePassword->Size = Size(150, 35);
            this->btnChangePassword->Text = L"Сменить пароль";
            this->btnChangePassword->Click += gcnew System::EventHandler(this, &Form1::btnChangePassword_Click);

            this->btnExit->Location = Point(210, 225);
            this->btnExit->Size = Size(120, 35);
            this->btnExit->Text = L"Выход";
            this->btnExit->Click += gcnew System::EventHandler(this, &Form1::btnExit_Click);

            this->AutoScaleDimensions = SizeF(6, 13);
            this->ClientSize = Size(420, 280);
            this->Text = L"Парольная защита";
            this->StartPosition = FormStartPosition::CenterScreen;
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
            this->MaximizeBox = false;

            this->Controls->Add(this->panelLogin);
            this->Controls->Add(this->panelMain);

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

        // ==================== Функции ====================
        void LoadOrCreatePassword()
        {
            if (!File::Exists(passFile))
            {
                String^ defaultPass = "12345";
                array<Byte>^ bytes = System::Text::Encoding::UTF8->GetBytes(defaultPass);
                for (int i = 0; i < bytes->Length; i++) bytes[i] ^= KEY;
                File::WriteAllBytes(passFile, bytes);
            }
        }

        String^ LoadDecryptedPassword()
        {
            if (!File::Exists(passFile)) return "";
            array<Byte>^ bytes = File::ReadAllBytes(passFile);
            for (int i = 0; i < bytes->Length; i++) bytes[i] ^= KEY;
            return System::Text::Encoding::UTF8->GetString(bytes);
        }

        void CreateSecretFileIfNotExists()
        {
            if (!File::Exists(secretFile))
            {
                String^ secretText = L"=== СЕКРЕТНАЯ ИНФОРМАЦИЯ ===\n\n"
                    L"Wi-Fi: SuperNet2026!\n"
                    L"Карта: 4276 9876 5432 1098\n"
                    L"CVV: 987\n"
                    L"Email: mysecret@mail.ru\n"
                    L"Пароль email: HiddenPass123\n\n"
                    L"Файл зашифрован XOR.";

                array<Byte>^ bytes = System::Text::Encoding::UTF8->GetBytes(secretText);
                for (int i = 0; i < bytes->Length; i++) bytes[i] ^= KEY;
                File::WriteAllBytes(secretFile, bytes);
            }
        }

        void DecryptAndShowSecret()
        {
            if (File::Exists(secretFile))
            {
                array<Byte>^ data = File::ReadAllBytes(secretFile);
                for (int i = 0; i < data->Length; i++) data[i] ^= KEY;
                txtSecret->Text = System::Text::Encoding::UTF8->GetString(data);

                File::WriteAllText(decryptedFile, txtSecret->Text);
            }
        }

        // ==================== Кнопки ====================
        System::Void btnOK_Click(System::Object^ sender, System::EventArgs^ e)
        {
            if (txtPassword->Text == LoadDecryptedPassword())
            {
                panelLogin->Visible = false;
                panelMain->Visible = true;
                this->Text = L"Доступ разрешён";
                DecryptAndShowSecret();
            }
            else
            {
                attempts--;
                if (attempts > 0)
                {
                    MessageBox::Show("Неверный пароль!\nОсталось попыток: " + attempts.ToString(), 
                        L"Ошибка", MessageBoxButtons::OK, MessageBoxIcon::Warning);
                    txtPassword->Clear();
                    txtPassword->Focus();
                }
                else
                {
                    MessageBox::Show(L"Попытки закончились!", L"Доступ запрещён", 
                        MessageBoxButtons::OK, MessageBoxIcon::Error);
                    Application::Exit();
                }
            }
        }

        System::Void btnCancel_Click(System::Object^ sender, System::EventArgs^ e)
        {
            Application::Exit();
        }

        System::Void btnChangePassword_Click(System::Object^ sender, System::EventArgs^ e)
        {
            MessageBox::Show(L"Функция смены пароля временно отключена\n(чтобы избежать ошибок компиляции)", 
                L"Информация", MessageBoxButtons::OK, MessageBoxIcon::Information);
            // При необходимости позже добавим отдельное окно
        }

        System::Void btnExit_Click(System::Object^ sender, System::EventArgs^ e)
        {
            Application::Exit();
        }
    };
}