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

时间:2015-03-30 13:08:26

标签: python pycrypto

我正在尝试使用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)

我做错了什么?

2 个答案:

答案 0 :(得分:1)

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

你也在重用哈希,这是非诺。除非要连接,否则必须创建一个新的哈希对象。

因此我们得到:

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

答案 1 :(得分:0)

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

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
....
相关问题