news 2026/5/26 19:51:28

python语言使用全局变量+线程锁解密程序代码

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
python语言使用全局变量+线程锁解密程序代码
importosimportrarfileimportthreadingfromqueueimportQueueimporttime# 指定路径rarfile.UNRAR_TOOL=r"UnRAR.exe"# 创建一个线程安全的队列用于存储密码password_queue=Queue()# 创建一个线程安全的标志用于指示是否已找到密码password_found=threading.Event()# 存储找到的密码found_password=None# 创建一个锁用于线程安全的输出print_lock=threading.Lock()# ========== 新增:全局计数器 + 计数器锁 ==========global_password_count=0count_lock=threading.Lock()# 保证计数器修改的线程安全deftry_decrypt(file_path,password):"""尝试使用给定密码解压 RAR 文件"""try:withrarfile.RarFile(file_path)asrf:rf.extractall(pwd=password.encode())returnTrueexceptrarfile.BadRarFile:returnFalseexceptExceptionase:withprint_lock:print(f"Error with password{password}:{e}")returnFalsedefworker(file_path):"""工作线程函数,从队列中获取密码并尝试解密"""globalfound_password,global_password_count# 引用全局计数器whilenotpassword_found.is_set():# 从队列中获取密码,如果队列为空则阻塞password=password_queue.get()# 如果已经找到密码,退出循环ifpassword_found.is_set():password_queue.task_done()break# ========== 关键修改:更新全局计数器(加锁保证线程安全) ==========current_global_count=0withcount_lock:global_password_count+=1current_global_count=global_password_count# 获取当前全局计数(避免后续被其他线程修改)# 尝试解密iftry_decrypt(file_path,password):# 设置标志为已找到密码password_found.set()found_password=passwordwithprint_lock:print(f"\n✅ 找到密码:{password}(全局第{current_global_count}个密码)")# 标记任务已完成password_queue.task_done()# 输出当前尝试的密码(使用全局计数,加锁保证打印有序)ifnotpassword_found.is_set():withprint_lock:print(f"🔄 尝试第{current_global_count}个密码:{password}")defmain():# RAR 文件路径file_path=r"password.rar"# 密码字典文件路径,你需要将其替换为实际的密码字典文件路径dict_path=r"wordlist1.txt"# 线程数量num_threads=10# 检查文件是否存在ifnotos.path.exists(file_path):print(f"RAR 文件不存在:{file_path}")returnifnotos.path.exists(dict_path):print(f"密码字典文件不存在:{dict_path}")return# 读取密码字典try:withopen(dict_path,"r",encoding="utf8")asf:passwords=[line.strip()forlineinf.readlines()]ifnotpasswords:print("密码字典为空")returnexceptExceptionase:print(f"读取密码字典时出错:{e}")return# 将密码加入队列forpasswordinpasswords:password_queue.put(password)print(f"开始多线程破解 RAR 文件,使用{num_threads}个线程")print(f"RAR 文件:{file_path}")print(f"密码字典:{dict_path}")print(f"总密码数:{len(passwords)}")print("-"*50)start_time=time.time()# 创建并启动工作线程threads=[]for_inrange(num_threads):t=threading.Thread(target=worker,args=(file_path,))t.daemon=Truet.start()threads.append(t)# 等待队列中的所有任务完成或找到密码password_queue.join()elapsed_time=time.time()-start_time# 输出结果ifpassword_found.is_set():print(f"🎉 破解成功!密码为:{found_password}")else:print("😔 未能找到有效密码")print(f"耗时:{elapsed_time:.2f}秒")print(f"全局总尝试密码数:{global_password_count}")if__name__=="__main__":main()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/21 0:36:08

3天打造专属CLIP模型:从零到一的完整实战指南

3天打造专属CLIP模型:从零到一的完整实战指南 【免费下载链接】open_clip An open source implementation of CLIP. 项目地址: https://gitcode.com/GitHub_Trending/op/open_clip 你是否曾因开源模型效果不佳而苦恼?是否担心商业API的数据隐私问…

作者头像 李华
网站建设 2026/5/26 2:59:32

Java对接PLC与SCADA系统的逻辑中枢设计(工业4.0核心技术解密)

第一章:Java对接PLC与SCADA系统的意义与挑战在工业自动化系统中,可编程逻辑控制器(PLC)和监控与数据采集系统(SCADA)承担着核心的数据采集与控制任务。随着企业对生产过程可视化、远程监控及系统集成需求的…

作者头像 李华
网站建设 2026/5/20 21:08:07

JupyterHub企业级部署完整指南:从零搭建到生产级运维

JupyterHub作为多用户Jupyter notebook服务器,已经成为企业数据科学团队协作的首选平台。本指南将带您从基础环境准备到生产级部署,全面掌握JupyterHub的企业级应用技巧,帮助您快速搭建稳定可靠的数据科学协作环境。 【免费下载链接】jupyter…

作者头像 李华
网站建设 2026/5/22 3:37:59

Gumbo HTML5解析器深度实践:从入门到项目集成的完整指南

Gumbo是一款纯C99语言实现的HTML5解析器,专为构建高质量网页分析工具和库而设计。作为开发者,掌握这个轻量级但功能强大的解析器将为您的项目带来显著的效率提升。本文将从基础概念到高级应用,为您提供全面的技术指导。 【免费下载链接】gumb…

作者头像 李华
网站建设 2026/5/20 20:07:26

mybatisplus在管理lora-scripts训练任务后台系统中的集成思路

MyBatis-Plus 在 LoRA 训练任务管理系统中的集成实践 在当前 AIGC 技术迅猛发展的背景下,LoRA(Low-Rank Adaptation)作为一种轻量级模型微调方法,因其对计算资源要求低、适配速度快,已被广泛应用于 Stable Diffusion 图…

作者头像 李华