我有这个错误:
Traceback (most recent call last):
File "python_md5_cracker.py", line 27, in <module>
m.update(line)
TypeError: Unicode-objects must be encoded before hashing
当我尝试在 Python 3.2.2 中执行此代码时:
import hashlib, sys
m = hashlib.md5()
hash = ""
hash_file = input("What is the file name in which the hash resides? ")
wordlist = input("What is your wordlist? (Enter the file name) ")
try:
hashdocument = open(hash_file, "r")
except IOError:
print("Invalid file.")
raw_input()
sys.exit()
else:
hash = hashdocument.readline()
hash = hash.replace("\n", "")
try:
wordlistfile = open(wordlist, "r")
except IOError:
print("Invalid file.")
raw_input()
sys.exit()
else:
pass
for line in wordlistfile:
# Flush the buffer (this caused a massive problem when placed
# at the beginning of the script, because the buffer kept getting
# overwritten, thus comparing incorrect hashes)
m = hashlib.md5()
line = line.replace("\n", "")
m.update(line)
word_hash = m.hexdigest()
if word_hash == hash:
print("Collision! The word corresponding to the given hash is", line)
input()
sys.exit()
print("The hash given does not correspond to any supplied word in the wordlist.")
input()
sys.exit()
它可能正在寻找来自
wordlistfile
的字符编码。
wordlistfile = open(wordlist,"r",encoding='utf-8')
或者,如果您正在逐行工作:
line.encode('utf-8')
根据下面的评论和这个答案。
我上面的回答假设所需的输出是来自
str
文件的 wordlist
。如果您习惯在 bytes
中工作,那么您最好使用 open(wordlist, "rb")
。但重要的是要记住,如果您将其与 hashfile
的输出进行比较,您的 rb
不应该使用
hexdigest
。
hashlib.md5(value).hashdigest()
输出
str
并且不能直接与字节对象进行比较:
'abc' != b'abc'
。 (这个话题还有很多,但我没有时间 ATM)。还应该注意这一行:
line.replace("\n", "")
应该是
line.strip()
这适用于字节和字符串。但如果您决定简单地转换为 bytes
,那么您可以将该行更改为:
line.replace(b"\n", b"")
encoding format
一样定义
utf-8
, 试试这个简单的方法,此示例使用 SHA256 算法生成随机数:
>>> import hashlib
>>> hashlib.sha256(str(random.getrandbits(256)).encode('utf-8')).hexdigest()
'cd183a211ed2434eac4f31b317c573c50e6c24e3a28b82ddcb0bf8bedf387a9f'
import hashlib
string_to_hash = '123'
hash_object = hashlib.sha256(str(string_to_hash).encode('utf-8'))
print('Hash', hash_object.hexdigest())
bytes
,例如与
line.encode('utf-8')
。
import hashlib, os
password_salt = os.urandom(32).hex()
password = '12345'
hash = hashlib.sha512()
hash.update(('%s%s' % (password_salt, password)).encode('utf-8'))
password_hash = hash.hexdigest()
m.update(line.encode('utf-8'))
那个答案。
现在,错误消息很明确:您只能使用字节,而不能使用 Python 字符串(Python 中曾经是unicode
、
utf-32
< 3), so you have to encode the strings with your preferred encoding: 、
utf-16
、
utf-8
,甚至是受限制的 8 位编码之一(有些人可能称之为代码页)。当您从文件中读取时,Python 3 会自动将单词列表文件中的字节解码为 Unicode。我建议你这样做:
m.update(line.encode(wordlistfile.encoding))
以便推送到 md5 算法的编码数据与底层文件完全一样编码。
import hashlib
with open(hash_file) as file:
control_hash = file.readline().rstrip("\n")
wordlistfile = open(wordlist, "rb")
# ...
for line in wordlistfile:
if hashlib.md5(line.rstrip(b'\n\r')).hexdigest() == control_hash:
# collision
variable = b"This is a variable"
或
variable2 = B"This is also a variable"
我从以下链接下载了英语词典
https://github.com/dwyl/english-words
# md5cracker.py
# English Dictionary https://github.com/dwyl/english-words
import hashlib, sys
hash_file = 'exercise\hashed.txt'
wordlist = 'data_sets\english_dictionary\words.txt'
try:
hashdocument = open(hash_file,'r')
except IOError:
print('Invalid file.')
sys.exit()
else:
count = 0
for hash in hashdocument:
hash = hash.rstrip('\n')
print(hash)
i = 0
with open(wordlist,'r') as wordlistfile:
for word in wordlistfile:
m = hashlib.md5()
word = word.rstrip('\n')
m.update(word.encode('utf-8'))
word_hash = m.hexdigest()
if word_hash==hash:
print('The word, hash combination is ' + word + ',' + hash)
count += 1
break
i += 1
print('Itiration is ' + str(i))
if count == 0:
print('The hash given does not correspond to any supplied word in the wordlist.')
else:
print('Total passwords identified is: ' + str(count))
sys.exit()