如何在定义的字符集python中加密所有可能的字符串?

如何在定义的字符集python中加密所有可能的字符串?,python,hash,python-3.x,Python,Hash,Python 3.x,我试图对定义的字符集中所有可能的字符串进行加密,然后将它们与用户输入给出的哈希进行比较 这就是我现在拥有的 import string from itertools import product import crypt def decrypt(): hash1 = input("Please enter the hash: ") salt = input("Please enter the salt: ") charSet = string.ascii_letters

我试图对定义的字符集中所有可能的字符串进行加密,然后将它们与用户输入给出的哈希进行比较

这就是我现在拥有的

import string
from itertools import product
import crypt

def decrypt():
    hash1 = input("Please enter the hash: ")
    salt = input("Please enter the salt: ")
    charSet = string.ascii_letters + string.digits
    for wordchars in product(charSet, repeat=2):
        hash2 = crypt.METHOD_CRYPT((wordchars), (salt))
        print (hash2)
很明显,它还没有完成,但是我在加密“wordchars”时遇到了问题


感谢您的帮助

嗯,使用bcrypt可能更好?
下面是一个简单的程序,可以满足您的要求:

def gen_word(charset, L):
    if L == 1: 
        for char in charset:
            yield char
        raise StopIteration
    for char in charset:
        for word in gen_word(charset, L - 1):
            yield char + word


def encrypt(word):
    '''Your encrypt function, replace with what you wish'''
    return word[::-1]


charset = ['1', '2', '3']


user_word = '12'
user_hash = encrypt(user_word)
max_length = 3


for length in range(1, max_length):
    for word in gen_word(charset, length):
        if encrypt(word) == user_hash:
            print 'Word found: %s' % word
基本上,它使用python生成器从固定长度的字符集生成单词。您可以用您想要的任何内容替换encrypt函数(在本例中,字符串反转用作散列)


请注意,使用实际的现代散列方法,解密普通密码将花费很长时间,因此我认为您实际上无法使用此密码。

以下是我基于J.F.Sebastian的完全不同的答案以及对我以前答案的评论。最重要的一点是,
crypt.METHOD\u crypt
不是可调用的,即使文档有点令人困惑地调用了,好像它是模块或实例的方法函数一样。它不是-,只要把它看作是
crypt
模块支持的各种加密之一的id或名称

因此,代码的问题有两方面:一是您试图将
wordchars
用作字符串,而实际上它是由
product()
生成的元组;二是您试图调用id
crypt.METHOD\u crypt

回答这个问题时,我有点不利,因为我没有运行Unix,没有安装PythonV3.3,也不完全理解您试图用代码实现什么。考虑到所有这些警告,我认为从您的代码中派生出的类似以下内容至少应该运行:

import string
from itertools import product
import crypt

def decrypt():
    hash1 = input("Please enter the hash: ")
    salt = input("Please enter the salt: ")
    charSet = string.ascii_letters + string.digits
    for wordchars in product(charSet, repeat=2):
        hash2 = crypt.crypt(''.join(wordchars), salt=salt)  # or salt=crypt.METHOD_CRYPT
        print(hash2)

crypt.METHOD\u crypt
不可调用,因此您提供的回溯与问题中的代码不对应
crypt。方法\u crypt
可以用作的第二个参数

正如前面所指出的,
wordchars
是一个元组,但是需要一个字符串来传递给
crypt.crypt()
函数

发件人:

因为一些crypt(3)扩展允许不同的值,具有不同的 在盐的大小上,建议使用完全加密的密码 在检查密码时将其视为盐

要从给定加密形式的已定义字符集(salt+hash)中查找纯文本,您可以:

from crypt import crypt
from itertools import product
from string import ascii_letters, digits

def decrypt(crypted, charset=ascii_letters + digits):
    # find hash for all 4-char strings from the charset
    # and compare with the given hash
    for candidate in map(''.join, product(charset, repeat=4)):
        if crypted == crypt(candidate, crypted):
            return candidate
例子
断言行确保用单词
e2e4
和salt
qb
调用
crypt
生成
qb1Y.qWr.DHs6
其中
qb
是salt。

您的“麻烦”是什么?否则这不是一个真正的问题。问题是它不起作用。我的意思是它在什么意义上不起作用?我收到这个错误消息
回溯(最近一次调用):文件“”,文件“”中的第1行,解密类型中的第4行错误:必须是字符串,非元组
您可以将您的问题和格式正确的回溯放在不可调用的方法\u CRYPT中。看到精彩了,非常感谢。还有一个小问题,我可以设置“repeat”位来搜索一个范围,这样它就可以找到1-10之间字符串的哈希值example@LWH91:注意:对于
repeat=10
而言,如果
repeat=4
在一秒钟内执行,则需要大约一个世纪的时间。否则它是
itertools.chain.from_iterable(product(charset,repeat=r)对于范围(8)内的r)
对于0-7的情况。您好,您的解决方案对于您给出的哈希很好,但是当我尝试任何哈希时,它都无法破解它。我的哈希都是这样的格式:aaacT.VSMxhms。假设salt是“aa”,哈希字符串是“acT.VSMxhms”,对吗。我知道这个杂烩就是“飞蛾”这个词。您在示例中使用的哈希格式是什么?断言行的作用是什么?@LWH91:run
crypt(“moth”,“aa”)
并查看它返回什么。我已经更新了答案来描述断言。在终端中运行它是什么?谢谢你清理了断言行
salt, hashed = 'qb', '1Y.qWr.DHs6'
print(decrypt(salt + hashed))
# -> e2e4
assert crypt('e2e4', 'qb') == (salt + hashed)