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


#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;

        // Объявляем все элементы правильно
        System::Windows::Forms::Label^ lblTitle;
        System::Windows::Forms::Label^ lblPassword;
        System::Windows::Forms::TextBox^ txtPassword;
        System::Windows::Forms::Button^ btnOK;
        System::Windows::Forms::Button^ btnCancel;

        System::Windows::Forms::Label^ lblSecretTitle;
        System::Windows::Forms::TextBox^ txtSecret;
        System::Windows::Forms::Button^ btnExit;

        System::Windows::Forms::Panel^ panelLogin;
        System::Windows::Forms::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 System::Windows::Forms::Panel();
            this->panelMain = gcnew System::Windows::Forms::Panel();
            this->lblTitle = gcnew System::Windows::Forms::Label();
            this->lblPassword = gcnew System::Windows::Forms::Label();
            this->txtPassword = gcnew System::Windows::Forms::TextBox();
            this->btnOK = gcnew System::Windows::Forms::Button();
            this->btnCancel = gcnew System::Windows::Forms::Button();
            this->lblSecretTitle = gcnew System::Windows::Forms::Label();
            this->txtSecret = gcnew System::Windows::Forms::TextBox();
            this->btnExit = gcnew System::Windows::Forms::Button();

            // Панель ввода пароля
            this->panelLogin->Location = System::Drawing::Point(0, 0);
            this->panelLogin->Size = System::Drawing::Size(400, 260);
            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 System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold);
            this->lblTitle->Location = System::Drawing::Point(90, 40);
            this->lblTitle->Text = L"Парольная защита";

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

            this->txtPassword->Location = System::Drawing::Point(50, 130);
            this->txtPassword->Size = System::Drawing::Size(280, 25);
            this->txtPassword->PasswordChar = '*';

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

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

            // Главная панель
            this->panelMain->Location = System::Drawing::Point(0, 0);
            this->panelMain->Size = System::Drawing::Size(400, 260);
            this->panelMain->Visible = false;
            this->panelMain->Controls->Add(this->lblSecretTitle);
            this->panelMain->Controls->Add(this->txtSecret);
            this->panelMain->Controls->Add(this->btnExit);

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

            this->txtSecret->Location = System::Drawing::Point(30, 70);
            this->txtSecret->Size = System::Drawing::Size(340, 130);
            this->txtSecret->Multiline = true;
            this->txtSecret->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
            this->txtSecret->ReadOnly = true;

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

            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->ClientSize = System::Drawing::Size(400, 260);
            this->Text = L"Парольная защита";
            this->StartPosition = System::Windows::Forms::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^ text = L"=== СЕКРЕТНАЯ ИНФОРМАЦИЯ ===\n\nWi-Fi: SuperNet2026!\nКарта: 4276 9876 5432 1098\nCVV: 987";
                array<Byte>^ bytes = System::Text::Encoding::UTF8->GetBytes(text);
                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);
            }
        }

        // ===================== Кнопки =====================
        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();
                }
                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 btnExit_Click(System::Object^ sender, System::EventArgs^ e)
        {
            Application::Exit();
        }
    };
}