1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
| import os import hashlib import base64 from kivy.app import App from kivy.uix.screenmanager import ScreenManager, Screen from kivy.uix.boxlayout import BoxLayout from kivy.uix.gridlayout import GridLayout from kivy.uix.button import Button from kivy.uix.textinput import TextInput from kivy.uix.label import Label from kivy.uix.popup import Popup from kivy.uix.scrollview import ScrollView from kivy.uix.spinner import Spinner from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC from cryptography.hazmat.backends import default_backend from cryptography.fernet import Fernet import sqlite3 import random import string import re from datetime import datetime
class User: def __init__(self, username, password): self.username = username self.salt = os.urandom(16) self.password_hash = self._hash_password(password)
def _hash_password(self, password): kdf = PBKDF2HMAC( algorithm=hashes.SHA256(), length=32, salt=self.salt, iterations=100000, backend=default_backend() ) return base64.urlsafe_b64encode(kdf.derive(password.encode()))
def verify_password(self, password): return self._hash_password(password) == self.password_hash
class Database: def __init__(self): self.conn = sqlite3.connect('passwords.db') self.create_tables()
def create_tables(self): cursor = self.conn.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS users (username TEXT PRIMARY KEY, password_hash TEXT, salt TEXT) ''') cursor.execute(''' CREATE TABLE IF NOT EXISTS bank_accounts (id INTEGER PRIMARY KEY, username TEXT, bank_name TEXT, account_number TEXT, login_password TEXT, payment_password TEXT, created_date TEXT, last_modified TEXT, history TEXT) ''') cursor.execute(''' CREATE TABLE IF NOT EXISTS website_logins (id INTEGER PRIMARY KEY, username TEXT, website TEXT, url TEXT, login_username TEXT, password TEXT, created_date TEXT, last_modified TEXT, history TEXT) ''') self.conn.commit()
def add_user(self, user): cursor = self.conn.cursor() cursor.execute('INSERT INTO users VALUES (?, ?, ?)', (user.username, user.password_hash, user.salt)) self.conn.commit()
def get_user(self, username): cursor = self.conn.cursor() cursor.execute('SELECT * FROM users WHERE username = ?', (username,)) user_data = cursor.fetchone() if user_data: user = User(user_data[0], '') user.password_hash = user_data[1] user.salt = user_data[2] return user return None
def add_bank_account(self, username, bank_name, account_number, login_password, payment_password): cursor = self.conn.cursor() now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") cursor.execute(''' INSERT INTO bank_accounts (username, bank_name, account_number, login_password, payment_password, created_date, last_modified, history) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ''', (username, bank_name, account_number, login_password, payment_password, now, now, '')) self.conn.commit()
def add_website_login(self, username, website, url, login_username, password): cursor = self.conn.cursor() now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") cursor.execute(''' INSERT INTO website_logins (username, website, url, login_username, password, created_date, last_modified, history) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ''', (username, website, url, login_username, password, now, now, '')) self.conn.commit()
def update_bank_account(self, id, login_password, payment_password): cursor = self.conn.cursor() now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") cursor.execute('SELECT login_password, payment_password, last_modified, history FROM bank_accounts WHERE id = ?', (id,)) old_data = cursor.fetchone() new_history = f"{old_data[2]}: Login={old_data[0]}, Payment={old_data[1]}\n{old_data[3]}" new_history = '\n'.join(new_history.split('\n')[:3]) cursor.execute(''' UPDATE bank_accounts SET login_password = ?, payment_password = ?, last_modified = ?, history = ? WHERE id = ? ''', (login_password, payment_password, now, new_history, id)) self.conn.commit()
def update_website_login(self, id, password): cursor = self.conn.cursor() now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") cursor.execute('SELECT password, last_modified, history FROM website_logins WHERE id = ?', (id,)) old_data = cursor.fetchone() new_history = f"{old_data[1]}: {old_data[0]}\n{old_data[2]}" new_history = '\n'.join(new_history.split('\n')[:3]) cursor.execute(''' UPDATE website_logins SET password = ?, last_modified = ?, history = ? WHERE id = ? ''', (password, now, new_history, id)) self.conn.commit()
def get_bank_accounts(self, username): cursor = self.conn.cursor() cursor.execute('SELECT * FROM bank_accounts WHERE username = ?', (username,)) return cursor.fetchall()
def get_website_logins(self, username): cursor = self.conn.cursor() cursor.execute('SELECT * FROM website_logins WHERE username = ?', (username,)) return cursor.fetchall()
db = Database()
def check_password_strength(password): score = 0 if len(password) >= 12: score += 1 if re.search(r"[A-Z]", password): score += 1 if re.search(r"[a-z]", password): score += 1 if re.search(r"\d", password): score += 1 if re.search(r"[!@#$%^&*(),.?\":{}|<>]", password): score += 1 return ["非常弱", "弱", "中等", "强", "非常强"][score]
def generate_strong_password(): chars = string.ascii_letters + string.digits + string.punctuation return ''.join(random.choice(chars) for _ in range(16))
def get_key(password, salt): kdf = PBKDF2HMAC( algorithm=hashes.SHA256(), length=32, salt=salt, iterations=100000, backend=default_backend() ) return base64.urlsafe_b64encode(kdf.derive(password.encode()))
def encrypt_password(password, key): f = Fernet(key) return f.encrypt(password.encode()).decode()
def decrypt_password(encrypted_password, key): f = Fernet(key) return f.decrypt(encrypted_password.encode()).decode()
class LoginScreen(Screen): def __init__(self, **kwargs): super().__init__(**kwargs) layout = BoxLayout(orientation='vertical', padding=10, spacing=10) self.username = TextInput(hint_text='用户名', multiline=False) self.password = TextInput(hint_text='密码', password=True, multiline=False) login_button = Button(text='登录') register_button = Button(text='注册') layout.add_widget(Label(text='密码管理器')) layout.add_widget(self.username) layout.add_widget(self.password) layout.add_widget(login_button) layout.add_widget(register_button) login_button.bind(on_press=self.login) register_button.bind(on_press=self.register) self.add_widget(layout) def login(self, instance): user = db.get_user(self.username.text) if user and user.verify_password(self.password.text): self.manager.current = 'password_manager' self.manager.get_screen('password_manager').current_user = user else: self.show_popup('错误', '用户名或密码错误')
def register(self, instance): if self.username.text and self.password.text: if db.get_user(self.username.text): self.show_popup('错误', '用户名已存在') else: user = User(self.username.text, self.password.text) db.add_user(user) self.show_popup('成功', '注册成功,请登录') else: self.show_popup('错误', '请输入用户名和密码')
def show_popup(self, title, content): popup = Popup(title=title, content=Label(text=content), size_hint=(None, None), size=(400, 200)) popup.open()
class PasswordManagerScreen(Screen): def __init__(self, **kwargs): super().__init__(**kwargs) self.current_user = None layout = BoxLayout(orientation='vertical', padding=10, spacing=10) self.password_type = Spinner(text='选择密码类型', values=('银行账户', '网站/邮箱登录')) self.bank_layout = GridLayout(cols=2, spacing=10) self.bank_name = TextInput(hint_text='银行名称', multiline=False) self.account_number = TextInput(hint_text='账户号码', multiline=False) self.login_password = TextInput(hint_text='登录密码', password=True, multiline=False) self.payment_password = TextInput(hint_text='支付密码', password=True, multiline=False) self.website_layout = GridLayout(cols=2, spacing=10) self.website = TextInput(hint_text='网站名称', multiline=False) self.url = TextInput(hint_text='网址', multiline=False) self.username = TextInput(hint_text='用户名', multiline=False) self.password = TextInput(hint_text='密码', password=True, multiline=False) self.bank_layout.add_widget(Label(text='银行名称:')) self.bank_layout.add_widget(self.bank_name) self.bank_layout.add_widget(Label(text='账户号码:')) self.bank_layout.add_widget(self.account_number) self.bank_layout.add_widget(Label(text='登录密码:')) self.bank_layout.add_widget(self.login_password) self.bank_layout.add_widget(Label(text='支付密码:')) self.bank_layout.add_widget(self.payment_password) self.website_layout.add_widget(Label(text='网站名称:')) self.website_layout.add_widget(self.website) self.website_layout.add_widget(Label(text='网址:')) self.website_layout.add_widget(self.url) self.website_layout.add_widget(Label(text='用户名:')) self.website_layout.add_widget(self.username) self.website_layout.add_widget(Label(text='密码:')) self.website_layout.add_widget(self.password) button_layout = BoxLayout(spacing=10) add_button = Button(text='添加密码') generate_button = Button(text='生成强密码') show_passwords_button = Button(text='显示所有密码') logout_button = Button(text='登出') button_layout.add_widget(add_button) button_layout.add_widget(generate_button) button_layout.add_widget(show_passwords_button) button_layout.add_widget(logout_button) self.strength_label = Label(text='密码强度: ') layout.add_widget(self.password_type) layout.add_widget(self.bank_layout) layout.add_widget(self.website_layout) layout.add_widget(self.strength_label) layout.add_widget(button_layout) add_button.bind(on_press=self.add_password) generate_button.bind(on_press=self.generate_password) show_passwords_button.bind(on_press=self.show_passwords) logout_button.bind(on_press=self.logout) self.password_type.bind(text=self.on_password_type_change) self.add_widget(layout) self.on_password_type_change(None, self.password_type.text) def on_password_type_change(self, instance, value): if value == '银行账户': self.bank_layout.opacity = 1 self.website_layout.opacity = 0 else: self.bank_layout.opacity = 0 self.website_layout.opacity = 1 def add_password(self, instance): if not self.current_user: self.show_popup('错误', '请先登录') return if self.password_type.text == '银行账户': if self.bank_name.text and self.account_number.text and self.login_password.text and self.payment_password.text: key = get_key(self.current_user.password_hash, self.current_user.salt) encrypted_login = encrypt_password(self.login_password.text, key) encrypted_payment = encrypt_password(self.payment_password.text, key) db.add_bank_account(self.current_user.username, self.bank_name.text, self.account_number.text, encrypted_login, encrypted_payment) self.show_popup('成功', '银行账户信息已添加') self.clear_inputs() else: self.show_popup('错误', '请填写所有银行账户信息') else: if self.website.text and self.url.text and self.username.text and self.password.text: key = get_key(self.current_user.password_hash, self.current_user.salt) encrypted_password = encrypt_password(self.password.text, key) db.add_website_login(self.current_user.username, self.website.text, self.url.text, self.username.text, encrypted_password) self.show_popup('成功', '网站登录信息已添加') self.clear_inputs() else: self.show_popup('错误', '请填写所有网站登录信息')
def generate_password(self, instance): password = generate_strong_password() if self.password_type.text == '银行账户': self.login_password.text = password self.payment_password.text = generate_strong_password() else: self.password.text = password self.update_password_strength()
def update_password_strength(self): if self.password_type.text == '银行账户': login_strength = check_password_strength(self.login_password.text) payment_strength = check_password_strength(self.payment_password.text) self.strength_label.text = f'密码强度: 登录密码 - {login_strength}, 支付密码 - {payment_strength}' else: strength = check_password_strength(self.password.text) self.strength_label.text = f'密码强度: {strength}'
def show_passwords(self, instance): if not self.current_user: self.show_popup('错误', '请先登录') return key = get_key(self.current_user.password_hash, self.current_user.salt) bank_accounts = db.get_bank_accounts(self.current_user.username) website_logins = db.get_website_logins(self.current_user.username) password_text = "银行账户:\n" for account in bank_accounts: password_text += f"银行: {account[2]}\n" password_text += f"账户: {account[3]}\n" password_text += f"登录密码: {decrypt_password(account[4], key)}\n" password_text += f"支付密码: {decrypt_password(account[5], key)}\n" password_text += f"创建日期: {account[6]}\n" password_text += f"最后修改: {account[7]}\n" password_text += f"修改历史: {account[8]}\n\n" password_text += "\n网站/邮箱登录:\n" for login in website_logins: password_text += f"网站: {login[2]}\n" password_text += f"网址: {login[3]}\n" password_text += f"用户名: {login[4]}\n" password_text += f"密码: {decrypt_password(login[5], key)}\n" password_text += f"创建日期: {login[6]}\n" password_text += f"最后修改: {login[7]}\n" password_text += f"修改历史: {login[8]}\n\n"
content = ScrollView(size_hint=(1, None), size=(400, 300)) content.add_widget(Label(text=password_text, size_hint_y=None, height=len(password_text.split('\n')) * 20))
popup = Popup(title='所有密码', content=content, size_hint=(None, None), size=(450, 400)) popup.open()
def logout(self, instance): self.current_user = None self.manager.current = 'login'
def show_popup(self, title, content): popup = Popup(title=title, content=Label(text=content), size_hint=(None, None), size=(400, 200)) popup.open()
def clear_inputs(self): self.bank_name.text = '' self.account_number.text = '' self.login_password.text = '' self.payment_password.text = '' self.website.text = '' self.url.text = '' self.username.text = '' self.password.text = '' self.strength_label.text = '密码强度: '
class PasswordManagerApp(App): def build(self): sm = ScreenManager() sm.add_widget(LoginScreen(name='login')) sm.add_widget(PasswordManagerScreen(name='password_manager')) return sm
if __name__ == '__main__': PasswordManagerApp().run()
|