【问题标题】:MD5 Python Brute Force ErrorMD5 Python蛮力错误
【发布时间】:2014-11-29 01:34:07
【问题描述】:

我已经用 python 编码了一段时间,并决定制作一个 MD5 暴力破解器。这是我的代码

import hashlib
import sys
import os
import time
import urllib2
import urllib
import re
import string 
import random
from itertools import islice

string = raw_input("HASH TO DECRYPT: ")

size = input("PASSWORD LENGTH: ")

char_list = ("a")

def random_char(size):
    selection = iter(lambda: random.choice(char_list),object())
    while True:
        yield ''.join(islice(selection, size))
random_gen = random_char(size) 

def encrypt(random_gen):
    algorithim = hashlib.md5()
    algorithim.update(random_gen)
    encrypted=algorithim.hexdigest()



def dosethestringexisit():
    if encrypt(random_gen) == string :
        print next(random_char)
    else:
        print("pass not found")

print dosethestringexisit()

我遇到了几个问题,比如这个错误

  algorithim.update(random_gen)
TypeError: must be string or buffer, not generator

我不知道如何更改随机生成的字符串以适应 algorithm.update()。

请注意,字符列表只有“a”,因为我使用 md5 哈希“a”只是为了测试代码是否有效。

我还想知道如果随机散列不等于需要解密的散列,如何创建一个while循环来重复代码。非常感谢。

【问题讨论】:

    标签: python python-2.7 hash md5


    【解决方案1】:

    要从生成器中获取项目,您需要使用next 函数:

    def encrypt(random_gen):
        algorithim = hashlib.md5()
        algorithim.update(next(random_gen))
        encrypted=algorithim.hexdigest()
    

    您也可以使用next 方法(或Python 3.x 中的__next__ 方法):

    algorithim.update(random_gen.next())
    

    以上将解决异常。

    顺便说一句,代码使用string 作为变量名。这会影响模块string;阻止使用该模块。使用不同的变量名。


    蛮力意味着尝试所有可能的输入;而不是使用随机,使用itertools.product,你可以获得所有字符串:

    >>> import hashlib
    >>> import itertools
    >>> import string
    >>>
    >>> def dosethestringexisit(target, size):
    ...     for xs in itertools.product(string.ascii_letters, repeat=size):
    ...         s = ''.join(xs)
    ...         if hashlib.md5(s).hexdigest() == target:
    ...             return s
    ...
    >>> dosethestringexisit('912ec803b2ce49e4a541068d495ab570', 4)
    'asdf'
    

    【讨论】:

    • 我修复了前两个问题,但我不明白你给我的最后一点信息。使用您提到的 itertools.product 代码,代码会是什么样子。
    • @Amedeo,我更新了答案以包括itertools.product。你看到我的答案的旧版本吗?尝试for xs in itertools.product('abc', repeat=2): print(''.join(xs)) 了解itertools.product 的作用。
    • 我看到你用 inter tools.product 回答,但我不明白在我的代码中放置它的位置。
    • @Amedeo,函数dosethestringexisit 本身就是你想要的。不是吗?如果找不到匹配项,该函数将返回原始字符串或None
    • 我编辑了我的原始问题并更改了代码,代码应该是这样的吗?因为如果它产生的结果没有?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 2015-03-23
    相关资源
    最近更新 更多