PyCrypto 脚本生成错误的 MD2 哈希值

PyCrypto script producing wrong MD2 hashes

我正在尝试使用 PyCrypto 计算 MD2 哈希值,直到找到一个或多个以给定字符串开头的哈希值。 (请不要问为什么:=)

我能找到几个哈希值。如果我通过在线工具检查我的哈希计算的正确性,我将不会得到相同的哈希值。

代码:

import itertools
from Crypto.Hash import MD2

charset = 'abcdefghijklmnopqrstuvwxyz0123456789'
md2hasher = MD2.new()

res = itertools.product(charset, repeat=6)
for i in res: 
    md2hasher.update(bytes(i))
    strMD2 = md2hasher.hexdigest()
    if strMD2.startswith('757c47'):
        print i
        print strMD2

示例输出:

('a', 'e', 's', '1', 'x', 'e')
757c47bf59afdcd8d05bd4c5d571ef5d
('a', 'i', 'p', '3', 'v', '4')
757c4758262eb9a3ce3a021728f0a842
('a', 'j', '3', 'j', 'p', '3')
757c475ffc257d31026674cb6b346094

在线验证:

http://md5hashing.net/hash/md2/d25e0cd52f62792daff6f76c5a640b4c

(d25e0cd52f62792daff6f76c5a640b4c)

我做错了什么?

您正在使用 Python2 - bytesstr 同义。 str(i) returns 字节串 "('a', 'e', 's', '1', 'x', 'e')" 而不是 'aes1xe';要获得后者,请使用 ''.join(i)

您还重复使用了散列,它是 nono。您必须创建一个新的散列对象,除非您想要连接。

因此我们得到:

charset = "abcdefghijklmnopqrstuvwxyz0123456789"
for i in itertools.product(charset, repeat=6): 
    strMD2 = MD2.new("".join(i)).hexdigest()
    if strMD2.startswith("757c47"):
        print strMD2

我有类似的问题,我尝试检查相同的字符串,我的代码看起来有点不同:

from Crypto.Hash import MD2
import itertools

letters = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789\"_$&#@"
h = MD2.new()

for guess in itertools.product(letters, repeat=6):
    h.update(''.join(guess).encode())    
    if(h.hexdigest()[0:6:1] == "757c47"):
        print ("String: " + ''.join(guess))
        print (h.hexdigest())

我试过上面的解决方案,效果很好。但是我不知道我的代码有什么错误。

输出:

String: aa8LVM
757c4765c5a45c70128c02766c63255b
String: abto9L
757c47e274e0d7e3a5a0a0574f154c7e
String: abFjlK
....