我想找到一个长度在1-4个字符之间的字母字符串。
我从迭代52个字母的列表开始:
letters = string.ascii_letters
然后,我需要遍历相同的列表,查看字符串的后3个字符,直到找到我要查找的字符串。
如果每个_代表52个字母的列表,我需要基本上这样做,同时在每次迭代时检查匹配:
_
_ _
_ _ _
_ _ _ _
我如何才能最好地构建一系列循环来实现这一目标?
如果问题的前提似乎令人困惑,那就是针对暴力破解的问题。我简单地提取了我正在努力解决的部分问题。
编辑:这是我到目前为止所处的地方。
#we know the salt is the 2-digit '50'
#we know the key is limited to 4 alphabetical letters
#cycle through all possibilities of the key till we match the hash
letters = string.ascii_letters
lcounter = 0
i = 0
j = 0
k = 0
l = 0
tryhash = "a"
word = [letters[i]]
while(tryhash != hash):
for c in letters:
word = [letters[i]] #this does not work as the additional letters need to be appended to word after the first lcounter loop
tryword = ''.join(word)
tryhash = crypt.crypt(tryword, "50")
if (tryhash == hash):
print(word)
break
i += 1
if (lcounter > 0) and (i == 52):
i = 0
if (lcounter == 1) and (j == 0):
word.insert(lcounter, letters[j])
j += 1
if (lcounter > 1) and (k == 52):
j = 0
if (lcounter == 2) and (k == 0):
word.insert(lcounter, letters[k])
k += 1
if (lcounter > 2) and (k == 52):
k = 0
if (lcounter == 3) and (l == 0):
word.insert(lcounter, letters[l])
l += 1
lcounter += 1
答案 0 :(得分:2)
您可以这样做:
import string import itertools data = string.ascii_lowecase for i in itertools.permutations(data, 4): if i == 'your_string': #do something else: pass
答案 1 :(得分:0)
或许这样的事情:
my_string = "some"
for letter1 in string.ascii_letters:
if letter1 == my_string:
print("success")
for letter2 in string.ascii_letters:
if letter1 + letter2 == my_string:
print("success")
for letter3 in string.ascii_letters:
if letter1 + letter2 + letter3 == my_string:
print("success")
for letter4 in string.ascii_letters:
if letter1 + letter2 + letter3 + letter4 == my_string
print("success")