先安装Python3 在pip install rarfile 

新建一个txt的文本文件,在把尾缀改成.py

复制下面的代码,就可以运行了 

 

 

 

import rarfile
import itertools
import string

 

def extract_rar(rar_path, password):
try:
with rarfile.RarFile(rar_path) as rf:
rf.extractall(pwd=password)
return True
except rarfile.BadRarFile:
print("这是一个无效的 RAR 文件。")
return False
except rarfile.PasswordRequired:
return False # 密码错误继续尝试
except rarfile.NeedFirstVolume:
print("需要第一个卷。")
return False
except rarfile.Error as e:
if "bad password" in str(e).lower():
return False
else:
print(f"发生错误: {e}")
return False
except Exception as e:
print(f"发生错误: {e}")
return False

 

def password_cracker(characters, min_length, max_length):
rar_file_path = input("请输入要破解的 RAR 文件路径:")
file_size = None
try:
with rarfile.RarFile(rar_file_path) as rf:
file_size = rf.filelist[0].file_size if rf.filelist else None
except Exception as e:
print(f"获取文件大小失败: {e}")

for length in range(min_length, max_length + 1):
for password_tuple in itertools.product(characters, repeat=length):
password = ''.join(password_tuple)
print(f"尝试密码: {password}")
print(f"尝试路径: {rar_file_path}")

try:
if extract_rar(rar_file_path, password):
# 再次确认解压后文件大小是否与原始文件一致(如果能获取到原始文件大小)
if file_size is not None:
with rarfile.RarFile(rar_file_path) as rf:
extracted_file_size = rf.filelist[0].file_size if rf.filelist else None
if extracted_file_size == file_size:
print(f"成功破解密码: {password}")
return
else:
print("解压文件大小与原始文件不一致,可能密码错误或文件有问题。")
else:
print(f"extracted_file_size: {extracted_file_size}")
print(f"file_size: {file_size}")
print(f"成功破解密码: {password}")
return
except Exception as e:
print(f"尝试密码 {password} 时发生错误: {e}")

print("未能找到密码。")

 

if __name__ == "__main__":
characters = string.ascii_lowercase + string.ascii_uppercase + string.digits + "!@#$%^&*()"
min_length = int(input("请输入最小密码长度(例如 3):"))
max_length = int(input("请输入最大密码长度(例如 6):"))
password_cracker(characters, min_length, max_length)

 

 

Python3.13版本 运行的,亲测有效,如有错误麻烦指出

posted on 2024-11-04 00:16  假装新手  阅读(24)  评论(0编辑  收藏  举报