news 2026/9/3 1:52:59

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/9/2 22:05:37

python语言百家姓+手机号密码本程序代码

import random# 第一步:定义纯英文百家姓列表(传统顺序,前100个常见姓氏) chinese_surnames_english ["Zhao", "Qian", "Sun", "Li","Zhou", "Wu", "Zheng&qu…

作者头像 李华
网站建设 2026/9/2 22:04:54

激光雷达与相机标定完整指南:如何实现精准传感器融合

激光雷达与相机标定完整指南:如何实现精准传感器融合 【免费下载链接】lidar_camera_calibration ROS package to find a rigid-body transformation between a LiDAR and a camera for "LiDAR-Camera Calibration using 3D-3D Point correspondences" …

作者头像 李华
网站建设 2026/9/2 22:42:24

snnTorch深度解析:开启脉冲神经网络开发新纪元

snnTorch深度解析:开启脉冲神经网络开发新纪元 【免费下载链接】snntorch Deep and online learning with spiking neural networks in Python 项目地址: https://gitcode.com/gh_mirrors/sn/snntorch 在人工智能技术日新月异的今天,snnTorch作为…

作者头像 李华
网站建设 2026/9/2 14:51:55

开源RAW处理器darktable终极指南:如何打造专业摄影工作流

开源RAW处理器darktable终极指南:如何打造专业摄影工作流 【免费下载链接】darktable darktable is an open source photography workflow application and raw developer 项目地址: https://gitcode.com/GitHub_Trending/da/darktable 作为一名长期使用dark…

作者头像 李华
网站建设 2026/9/2 22:48:38

快速上手Vue 3富文本编辑器:5步实现专业级文本编辑

快速上手Vue 3富文本编辑器:5步实现专业级文本编辑 【免费下载链接】vue-quill Rich Text Editor Component for Vue 3. 项目地址: https://gitcode.com/gh_mirrors/vu/vue-quill VueQuill是一个专为Vue 3设计的富文本编辑器组件,它结合了Vue的响…

作者头像 李华
网站建设 2026/9/2 22:49:06

Axolotl终极指南:5步掌握AI模型微调全流程

Axolotl终极指南:5步掌握AI模型微调全流程 【免费下载链接】axolotl 项目地址: https://gitcode.com/GitHub_Trending/ax/axolotl 想要快速上手AI模型微调却苦于复杂的配置和繁琐的流程?Axolotl作为一款功能全面的开源AI训练平台,为你…

作者头像 李华