Загрузка данных
#pragma once
namespace PasswordProtection {
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 = 3;
String^ passFile = "password.dat";
String^ secretFile = "secret.txt";
String^ decryptedFile = "secret_decrypted.txt";
unsigned char KEY = 0xAA;
// Элементы управления
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^ btnChangePassword;
System::Windows::Forms::Button^ btnExit;
Panel^ panelLogin;
Panel^ panelMain;
public:
Form1(void)
{
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->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("Microsoft Sans Serif", 14, FontStyle::Bold);
this->lblTitle->Location = Point(50, 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->txtPassword->Font = gcnew Drawing::Font("Microsoft Sans Serif", 12);
this->btnOK->Location = Point(50, 180);
this->btnOK->Size = Size(120, 35);
this->btnOK->Text = L"Войти";
this->btnOK->Click += gcnew 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 EventHandler(this, &Form1::btnCancel_Click);
// ==================== Главная панель ====================
this->lblSecretTitle = gcnew Label();
this->txtSecret = gcnew TextBox();
this->btnChangePassword = gcnew Button();
this->btnExit = gcnew Button();
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("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->txtSecret->Font = gcnew Drawing::Font("Consolas", 10);
this->btnChangePassword->Location = Point(40, 225);
this->btnChangePassword->Size = Size(140, 35);
this->btnChangePassword->Text = L"Сменить пароль";
this->btnChangePassword->Click += gcnew EventHandler(this, &Form1::btnChangePassword_Click);
this->btnExit->Location = Point(200, 225);
this->btnExit->Size = Size(140, 35);
this->btnExit->Text = L"Выход";
this->btnExit->Click += gcnew EventHandler(this, &Form1::btnExit_Click);
// Форма
this->AutoScaleDimensions = SizeF(6, 13);
this->ClientSize = Size(420, 280);
this->Text = L"Парольная защита";
this->StartPosition = FormStartPosition::CenterScreen;
this->FormBorderStyle = 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";
SaveEncryptedPassword(defaultPass);
}
}
void SaveEncryptedPassword(String^ plainPass)
{
array<Byte>^ bytes = System::Text::Encoding::UTF8->GetBytes(plainPass);
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);
}
// ==================== Автосоздание secret.txt ====================
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"Файл автоматически создан и зашифрован.";
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)
{
String^ entered = txtPassword->Text;
String^ realPass = LoadDecryptedPassword();
if (entered == realPass)
{
panelLogin->Visible = false;
panelMain->Visible = true;
this->Text = L"Заставка программы — Доступ разрешён";
DecryptAndShowSecret();
}
else
{
attempts--;
if (attempts > 0)
{
MessageBox::Show("Неверный пароль!\nОсталось попыток: " + attempts,
"Ошибка", MessageBoxButtons::OK, MessageBoxIcon::Warning);
txtPassword->Clear();
txtPassword->Focus();
}
else
{
MessageBox::Show("Попытки исчерпаны. Программа закрывается.",
"Доступ запрещён", 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)
{
String^ newPass = Microsoft::VisualBasic::Interaction::InputBox(
"Введите новый пароль:", "Смена пароля", "", -1, -1);
if (!String::IsNullOrEmpty(newPass))
{
SaveEncryptedPassword(newPass);
MessageBox::Show("Пароль успешно изменён!", "Успешно",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
}
System::Void btnExit_Click(System::Object^ sender, System::EventArgs^ e)
{
Application::Exit();
}
};
}