我正在开发一个小型 python 程序,该程序本质上会使用 word 文件暴力破解 md5 哈希值。该程序会获取您的哈希值,然后您可以选择一个文件用作单词列表。然后它会在文件中逐行生成一个 md5 哈希版本来与您输入的版本进行检查。如果它们匹配,那么它会告诉您产生该哈希值的单词。问题在于,当程序将行转换为哈希值时,它不会生成正确的可识别 md5 哈希值。例如,它表示 test 的 md5 哈希值是 d8e8fca2dc0f896fd7cb4cb0031ba249。我尝试了多种对文本进行编码的方法等,但找不到正确的答案。我做错了什么?
import hashlib
mainhash = raw_input("What hash would you like to try and break?")
filename = raw_input("What file would you like to use?")
times = 0
if filename == "":
print "A list file is required."
exit()
f = open(filename)
for line in iter(f):
times = times + 1
word = line
line = hashlib.md5(line.encode("utf")).hexdigest()
print line
if line == mainhash:
print "Matching Hash found. Word is:"
print word
print times
exit()
f.close()
print "Sorry no match was found. Please try a different word file or make sure the hash is md5."
print times
最佳答案
line
包括行末尾的换行符。替换:
line = hashlib.md5(line.encode("utf")).hexdigest()
与:
line = hashlib.md5(line.encode("utf").strip()).hexdigest()
即使字符串末尾有一个换行符也会完全改变哈希值。
关于Python hashlib 没有为 md5 生成正确的哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31999669/