给予上面的脚本,我希望最后的结果可以存储在txt文件中
时间: 2024-04-10 10:33:21 浏览: 13
当找到密码后,你可以将结果写入一个文本文件中。以下是修改过的示例代码:
```python
import hashlib
def crack_md5(target_hashes, wordlist_path, result_file):
with open(wordlist_path, 'r') as wordlist:
for password in wordlist:
password = password.strip()
md5_hash = hashlib.md5(password.encode()).hexdigest()
if md5_hash in target_hashes:
print(f"Password found: {password}")
target_hashes.remove(md5_hash)
with open(result_file, 'a') as result:
result.write(f"Password found: {password}\n")
if len(target_hashes) == 0:
print("All passwords cracked!")
return
print("Password not found in wordlist!")
# 示例用法
target_hashes = ["e10adc3949ba59abbe56e057f20f883e", "098f6bcd4621d373cade4e832627b4f6"]
wordlist_path = "wordlist.txt"
result_file = "results.txt"
crack_md5(target_hashes, wordlist_path, result_file)
```
在这个修改后的代码中,我们添加了一个额外的参数 `result_file`,用于指定结果存储的文本文件路径。当找到密码时,我们通过 `with open(result_file, 'a') as result:` 语句打开结果文件,并将找到的密码写入文件中。
请确保指定的 `result_file` 是一个可写入的文本文件路径,并且具有适当的权限。