如何用Python为MD5解密密码生成统计信息?
MD5密码破解工具统计功能实现方案
核心统计逻辑代码
假设你已经有破解后的结果列表crack_results,每个元素为(用户名, MD5哈希, 明文密码/None)(None表示未破解),以下是完整的统计与输出代码:
# 初始化统计容器 cracked_users = [] uncracked_users = [] plain_to_users = {} # key: 明文密码, value: 使用该密码的用户列表 hash_to_users = {} # key: 未破解哈希, value: 使用该哈希的用户列表 # 遍历破解结果,分类填充容器 for username, hash_str, plaintext in crack_results: if plaintext is not None: cracked_users.append((username, plaintext)) if plaintext not in plain_to_users: plain_to_users[plaintext] = [] plain_to_users[plaintext].append(username) else: uncracked_users.append((username, hash_str)) if hash_str not in hash_to_users: hash_to_users[hash_str] = [] hash_to_users[hash_str].append(username) # 计算各项统计值 total_cracked = len(cracked_users) # 计算使用重复已破解密码的用户数:总已破解数减去使用唯一密码的用户数 unique_cracked = sum(1 for users in plain_to_users.values() if len(users) == 1) repeated_cracked_users = total_cracked - unique_cracked # 生成前10重复明文密码(降序) top10_repeated = sorted(plain_to_users.items(), key=lambda x: len(x[1]), reverse=True)[:10] top10_formatted = [(p, len(u)) for p, u in top10_repeated] # 未破解相关统计 total_uncracked = len(uncracked_users) unique_uncracked = sum(1 for users in hash_to_users.values() if len(users) == 1) repeated_uncracked_users = total_uncracked - unique_uncracked # 写入统计文本文件 with open('crack_stats.txt', 'w', encoding='utf-8') as f: f.write("=== MD5密码破解统计报告 ===\n\n") f.write(f"1. 已破解密码数量(用户数):{total_cracked}\n") f.write(f"2. 使用重复已破解密码的用户数:{repeated_cracked_users}\n\n") f.write("3. 重复次数最多的前10个明文已破解密码及次数:\n") if not top10_formatted: f.write(" 无已破解密码\n") else: for idx, (plain, count) in enumerate(top10_formatted, 1): f.write(f" {idx}. {plain}:{count}次\n") f.write(f"\n4. 未破解密码数量(用户数):{total_uncracked}\n") f.write(f"5. 使用重复未破解密码的用户数:{repeated_uncracked_users}\n")
常见统计错误修正说明
- 混淆统计对象:避免把“已破解密码数量”当成不同明文的数量,实际需求是已破解的用户总数;
- 重复用户数计算错误:不要直接统计重复的密码数,正确逻辑是用总破解用户数减去使用唯一密码的用户数;
- 排序逻辑缺失:前10重复密码需要按使用次数降序排序后取前10,代码中通过
sorted的key参数实现按用户列表长度排序。
整合提示
将这段代码插入到你的破解流程之后(即生成crack_results列表之后),确保crack_results的结构符合代码预期即可运行。
内容的提问来源于stack exchange,提问作者Cotten32
相关产品推荐
相关产品推荐

