大家好!欢迎来到奥饼饼智慧谷 🔐。前几天我们做了游戏和工具,今天来一个超级实用的安全工具——密码生成器!再也不用担心密码安全问题了!
🎯 密码生成器功能
一个好的密码生成器应该有这些功能:
💡 安全小贴士
密码至少 8 位以上,包含大小写字母、数字和符号才安全!
🔐 开始写代码
以下是完整的密码生成器代码:
import random import string def get_user_settings(): """获取用户的密码设置""" print("\n" + "="*40) print(" 🔐 密码生成器设置") print("="*40) # 获取密码长度 while True: try: length = int(input("\n请输入密码长度 (8-32): ").strip()) if 8 <= length <= 32: break print("❌ 请输入 8-32 之间的数字!") except ValueError: print("❌ 请输入有效的数字!") # 获取字符选项 print("\n请选择密码包含的内容:") use_upper = input("包含大写字母? (y/n): ").strip().lower() == 'y' use_digits = input("包含数字? (y/n): ").strip().lower() == 'y' use_special = input("包含特殊符号? (y/n): ").strip().lower() == 'y' # 获取生成数量 while True: try: count = int(input("\n生成几个密码? (1-10): ").strip()) if 1 <= count <= 10: break print("❌ 请输入 1-10 之间的数字!") except ValueError: print("❌ 请输入有效的数字!") return length, use_upper, use_digits, use_special, count def generate_password(length, use_upper, use_digits, use_special): """生成单个密码""" chars = string.ascii_lowercase # 基础: 小写字母 if use_upper: chars += string.ascii_uppercase if use_digits: chars += string.digits if use_special: chars += "!@#$%^&*()_+-=[]{}|;:,.<>?" # 确保至少包含一种字符 if not use_upper and not use_digits and not use_special: chars += string.ascii_uppercase # 如果只选了小写,至少加点大写 password = [] # 确保包含所选的每类字符 if use_upper: password.append(random.choice(string.ascii_uppercase)) if use_digits: password.append(random.choice(string.digits)) if use_special: password.append(random.choice("!@#$%^&*()_+-=[]{}|;:,.<>?")) # 填充剩余部分 while len(password) < length: password.append(random.choice(chars)) # 打乱顺序 random.shuffle(password) return ''.join(password) def check_strength(password, use_upper, use_digits, use_special): """评估密码强度""" score = 0 feedback = [] # 长度评分 if len(password) >= 16: score += 40 feedback.append("✅ 长度足够长!") elif len(password) >= 12: score += 30 feedback.append("✅ 长度还可以") elif len(password) >= 8: score += 20 feedback.append("⚠️ 长度有点短") else: feedback.append("❌ 长度太短了!") # 复杂度评分 complexity = 1 + use_upper + use_digits + use_special if complexity == 4: score += 50 feedback.append("✅ 四类字符都有!") elif complexity == 3: score += 35 feedback.append("✅ 三类字符,不错") elif complexity == 2: score += 20 feedback.append("⚠️ 只有两类字符") else: feedback.append("❌ 太单一了!") # 确定等级 if score >= 80: level = "🟢 超强" elif score >= 60: level = "🟡 强" elif score >= 40: level = "🟠 中等" else: level = "🔴 弱" return level, score, feedback def main(): print("="*40) print(" 🔐 欢迎使用密码生成器! 🔐") print("="*40) while True: # 获取用户设置 length, use_upper, use_digits, use_special, count = get_user_settings() print("\n" + "="*50) print(" 🎯 生成的密码") print("="*50) # 生成密码 for i in range(count): password = generate_password(length, use_upper, use_digits, use_special) level, score, feedback = check_strength(password, use_upper, use_digits, use_special) print(f"\n密码 {i+1}: {password}") print(f" 强度: {level} ({score}分)") print(f" 评估: {', '.join(feedback[:2])}") print("\n" + "="*50) # 询问是否继续 again = input("\n还要生成密码吗? (y/n): ").strip().lower() if again != 'y': print("\n👋 再见! 注意保管好密码!") break if __name__ == "__main__": main()
安全是一种习惯,一个好的密码是安全的第一步!
🚀 运行试试吧!
保存为 `password_generator.py` 然后运行:
python password_generator.py
根据提示选择设置,一键生成安全密码!
✨ 进阶玩法
想让密码生成器更强大?试试这些改进:
🎁 安全提醒
不要在多个网站使用相同密码!建议使用密码管理软件!
📚 知识要点
通过这个项目,我们学到了:
今天的密码生成器就到这里了!快去生成一些安全密码吧,保护好自己的账号!🔐
👋 感谢阅读 | 奥饼饼智慧谷
每天一个有趣的编程小项目,让学习变成乐趣!
💡 点赞 + 在看 + 分享,让更多人一起享受编程的快乐!