问题描述
我试图加密已定义字符集中所有可能的字符串,然后将它们与用户输入提供的散列进行比较。
这就是我目前所拥有的
p>从itertools导入字符串
导入产品
导入crypt
def decrypt() :
hash1 = input(请输入哈希值)
salt = input(请输入salt:)
charSet = string.ascii_letters + string.digits
(charSet,repeat = 2):
hash2 = crypt.METHOD_CRYPT((wordchars),(salt))
print(hash2)
显然它还没有完成,但我在加密wordchars时遇到了麻烦。
crypt.METHOD_CRYPT 不可调用,因此您提供的回溯不符合您问题中的代码。 crypt.METHOD_CRYPT 可以用作。
同样作为指出 wordchars 是一个元组,但您需要一个字符串传递给 crypt.crypt()函数。
From :
lockquote
由于几个crypt(3 )扩展允许不同的值,盐中不同的
大小,建议在检查密码时使用完整的加密密码
作为盐。
要从定义的字符集中找到纯文本,并给定其加密形式:salt加hash,您可以:
ort ascii_letters,digits
def解密(crypted,charset = ascii_letters + digits):
#从字符集
#中找到所有4个字符串的哈希,并与给定的散列
候选人在地图(''。join,product(charset,repeat = 4)):
如果加密== crypt(候选人,被加密):
候选人
$ b $ h $> salt,hashed ='qb','1Y.qWr.DHs6'
print(decrypt(salt + hashed))
# - > e2e4
assert crypt('e2e4','qb')==(salt + hashed)
断言行确保用 e2e4 和salt qb调用 crypt 产生 qb1Y.qWr.DHs6 其中 qb 是盐。
I am trying to encrypt all possible strings in a defined character set then compare them to a hash given by user input.
This is what I currently have
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)
Obviously its not finished yet but I am having trouble encrypting "wordchars"
Any help is appreciated
crypt.METHOD_CRYPT is not callable so the traceback that you provided doesn't correspond to the code in your question. crypt.METHOD_CRYPT could be used as the second parameter for crypt.crypt() function.
Also as @martineau pointed out wordchars is a tuple but you need a string to pass to the crypt.crypt() function.
From the docs:
To find a plain text from a defined character set given its crypted form: salt plus hash, you could:
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
Example
salt, hashed = 'qb', '1Y.qWr.DHs6' print(decrypt(salt + hashed)) # -> e2e4 assert crypt('e2e4', 'qb') == (salt + hashed)
The assert line makes sure that calling crypt with the word e2e4 and the salt qb produces qb1Y.qWr.DHs6 where qb is the salt.
这篇关于如何加密所定义的字符集中的所有可能的字符串python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!