Загрузка данных
#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^ 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->btnExit = gcnew Button();
// Панель логина
this->panelLogin->Location = Point(0, 0);
this->panelLogin->Size = 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->Location = Point(90, 40);
this->lblTitle->Text = L"Парольная защита";
this->lblTitle->Font = gcnew Drawing::Font(L"Microsoft Sans Serif", 14, FontStyle::Bold);
this->lblTitle->AutoSize = true;
this->lblPassword->Location = Point(50, 100);
this->lblPassword->Text = L"Введите пароль:";
this->lblPassword->AutoSize = true;
this->txtPassword->Location = Point(50, 130);
this->txtPassword->Size = Size(280, 25);
this->txtPassword->PasswordChar = '*';
this->btnOK->Location = Point(50, 180);
this->btnOK->Size = Size(110, 35);
this->btnOK->Text = L"Войти";
this->btnOK->Click += gcnew System::EventHandler(this, &Form1::btnOK_Click);
this->btnCancel->Location = Point(180, 180);
this->btnCancel->Size = Size(110, 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(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->Location = Point(30, 20);
this->lblSecretTitle->Text = L"Доступ разрешён!\nСекретная информация:";
this->lblSecretTitle->Font = gcnew Drawing::Font(L"Microsoft Sans Serif", 12, FontStyle::Bold);
this->lblSecretTitle->AutoSize = true;
this->txtSecret->Location = Point(30, 70);
this->txtSecret->Size = Size(340, 130);
this->txtSecret->Multiline = true;
this->txtSecret->ScrollBars = ScrollBars::Vertical;
this->txtSecret->ReadOnly = true;
this->btnExit->Location = Point(140, 210);
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(400, 260);
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^ 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(String::Concat("Неверный пароль!\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();
}
};
}