如何加密所定义的字符集中的所有可能的字符串python? [英] How to encrypt all possible strings in a defined character set python?
问题描述
我试图加密已定义字符集中所有可能的字符串,然后将它们与用户输入提供的散列进行比较。
这就是我目前所拥有的
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时遇到了麻烦。
同样作为@martineau 指出 From 文档: 由于几个crypt(3 )扩展允许不同的值,盐中不同的 要从定义的字符集中找到纯文本,并给定其加密形式:salt加hash,您可以: 断言行确保用 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 Obviously its not finished yet but I am having trouble encrypting "wordchars" Any help is appreciated Also as @martineau pointed out From the docs: Since a few crypt(3) extensions allow different values, with different
sizes in the salt, it is recommended to use the full crypted password
as salt when checking for a password. To find a plain text from a defined character set given its crypted form: salt plus hash, you could:
crypt.METHOD_CRYPT
可以用作 crypt.crypt()
函数。
wordchars
是一个元组,但您需要一个字符串传递给 crypt.crypt()
函数。
lockquote
大小,建议在检查密码时使用完整的加密密码
作为盐。
itertools的
从字符串imp导入产品
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
是盐。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)
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.wordchars
is a tuple but you need a string to pass to the crypt.crypt()
function.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!