C++密码管理器

时间:2025-03-30 08:35:24
#include <iostream> #include <fstream> #include <vector> #include <string> #include <iomanip> #include <cctype> #include <algorithm> #include <map> using namespace std; // 数据结构 struct Account { int id; string platform; string username; string password; }; // 密码管理器 class PasswordManager { private: string masterPassword; vector<Account> accounts; const string dataFileName = ""; const string passwordFileName = ""; const int maxAttempts = 3; // 密码安全性检查 string checkPasswordStrength(const string& password) { bool hasUpper = false, hasLower = false, hasDigit = false, hasSpecial = false; for (char ch : password) { if (isupper(ch)) hasUpper = true; else if (islower(ch)) hasLower = true; else if (isdigit(ch)) hasDigit = true; else hasSpecial = true; } int score = (hasUpper ? 1 : 0) + (hasLower ? 1 : 0) + (hasDigit ? 1 : 0) + (hasSpecial ? 1 : 0); if (score < 2) return "弱"; if (score == 2) return "中"; return "强"; } // 计算密码破解时间 double calculateCrackTime(const string& password) { // 假设每秒破解4000000000个密码 double possibilities = 1; for (char ch : password) { if (isdigit(ch) || isalpha(ch)) possibilities *= 62; // 数字 + 大小写字母 else possibilities *= 95; // 包含特殊符号 } return possibilities / 4000000000.0; } // 保存管理器密码到文件 void saveMasterPassword() { ofstream file(passwordFileName); if (file.is_open()) { file << masterPassword << endl; file.close(); } } // 从文件加载管理器密码 void loadMasterPassword() { ifstream file(passwordFileName); if (file.is_open()) { getline(file, masterPassword); file.close(); } } // 检查密码是否匹配 bool verifyPassword(const string& password) { return password == masterPassword; } // 保存数据到文件 void saveData() { ofstream file(dataFileName); if (file.is_open()) { for (const auto& acc : accounts) { file << acc.id << " " << acc.platform << " " << acc.username << " " << acc.password << endl; } file.close(); } } // 显示所有弱密码 void displayWeakPasswords() { for (const auto& acc : accounts) { if (checkPasswordStrength(acc.password) == "弱") { cout << "ID: " << acc.id << ", 平台: " << acc.platform << ", 账号: " << acc.username << ", 密码: " << acc.password << " (弱)" << endl; } } } // 显示重复的密码 void displayDuplicatePasswords() { map<string, vector<Account>> platformAccounts; for (const auto& acc : accounts) { platformAccounts[acc.platform].push_back(acc); } for (const auto& pair : platformAccounts) { const auto& accList = pair.second; if (accList.size() > 1) { cout << "平台: " << pair.first << endl; for (const auto& acc : accList) { cout << "ID: " << acc.id << ", 账号: " << acc.username << ", 密码: " << acc.password << endl; } cout << "-----------------------------" << endl; } } } public: PasswordManager() { // 检查是否需要创建管理器密码 loadMasterPassword(); if (masterPassword.empty()) { cout << "首次启动,请设置管理器密码: "; cin >> masterPassword; saveMasterPassword(); } else { // 校验管理器密码 int attempts = 0; string password; while (attempts < maxAttempts) { cout << "输入管理器密码: "; cin >> password; if (verifyPassword(password)) { break; } else { cout << "密码错误!" << endl; attempts++; } } if (attempts == maxAttempts) { cout << "尝试次数过多,程序退出。" << endl; exit(1); } } // 从文件加载数据 ifstream file(dataFileName); if (file.is_open()) { string line; while (getline(file, line)) { Account acc; istringstream iss(line); iss >> acc.id >> acc.platform >> acc.username >> acc.password; accounts.push_back(acc); } file.close(); } } // 添加第三方账号密码数据 void addAccount() { Account acc; cout << "输入ID: "; cin >> acc.id; // 检查ID是否重复 for (const auto& a : accounts) { if (a.id == acc.id) { cout << "ID " << acc.id << " 已经存在,请使用其他ID。" << endl; return; } } cout << "输入平台: "; cin >> acc.platform; cout << "输入账号: "; cin >> acc.username; cout << "输入密码: "; cin >> acc.password; string strength = checkPasswordStrength(acc.password); double crackTime = calculateCrackTime(acc.password); cout << "密码强度: " << strength << endl; cout << "估计破解时间: " << fixed << setprecision(2) << crackTime << " 秒" << endl; accounts.push_back(acc); saveData(); } // 删除第三方账号密码数据 void deleteAccount() { int id; cout << "输入要删除的账号ID: "; cin >> id; auto it = remove_if(accounts.begin(), accounts.end(), [id](const Account& acc) { return acc.id == id; }); accounts.erase(it, accounts.end()); saveData(); } // 查找第三方账号密码数据 void findAccount() { string platform; cout << "输入平台名称: "; cin >> platform; for (const auto& acc : accounts) { if (acc.platform.find(platform) != string::npos) { cout << "ID: " << acc.id << ", 平台: " << acc.platform << ", 账号: " << acc.username << ", 密码: " << acc.password << endl; } } } // 修改第三方账号密码数据 void modifyAccount() { int id; cout << "输入要修改的账号ID: "; cin >> id; for (auto& acc : accounts) { if (acc.id == id) { cout << "输入新的平台: "; cin >> acc.platform; cout << "输入新的账号: "; cin >> acc.username; cout << "输入新的密码: "; cin >> acc.password; saveData(); return; } } cout << "未找到ID为" << id << "的账号。" << endl; } // 修改管理器密码 void changeMasterPassword() { string oldPassword, newPassword; cout << "输入当前密码: "; cin >> oldPassword; if (oldPassword != masterPassword) { cout << "密码错误。" << endl; return; } cout << "输入新密码: "; cin >> newPassword; masterPassword = newPassword; saveMasterPassword(); } // 显示所有弱密码 void showWeakPasswords() { displayWeakPasswords(); } // 显示重复的密码 void showDuplicatePasswords() { displayDuplicatePasswords(); } }; int main() { PasswordManager pm; int choice; while (true) { cout << "1. 添加第三方账号密码\n"; cout << "2. 删除第三方账号密码\n"; cout << "3. 查找第三方账号密码\n"; cout << "4. 修改第三方账号密码\n"; cout << "5. 修改管理器密码\n"; cout << "6. 显示所有弱密码\n"; cout << "7. 显示重复密码\n"; cout << "8. 退出\n"; cout << "请选择: "; cin >> choice; switch (choice) { case 1: pm.addAccount(); break; case 2: pm.deleteAccount(); break; case 3: pm.findAccount(); break; case 4: pm.modifyAccount(); break; case 5: pm.changeMasterPassword(); break; case 6: pm.showWeakPasswords(); break; case 7: pm.showDuplicatePasswords(); break; case 8: return 0; default: cout << "无效选项。" << endl; break; } } }