我正在阅读这篇thread,这激发了我一些研究如何优化简单生成密码的暴力破解。
我试图从key=''.join(random.sample(string.uppercase, 7))
迭代所有可用的密码组合。如果我已经阅读了正确的文档:key
在这种情况下,不能包含两个相似或相等的字母。它总是(在这种情况下)唯一的7长大写字符串。
str(key)
无法生成:AARGHJU
如何更改while语句,使其不会迭代包含相似字母的字符串?
chars = 'ABCDEFGHIJKLMNOPQRSTUVXYZ' # chars to look for
try:
while 1:
# Iteration processess of possibel keys
for length in range(7,8): # only do length of 7
to_attempt = product(chars, repeat=length)
for attempt in to_attempt:
print(''.join(attempt))
except KeyboardInterrupt:
print "Keybord interrupt, exiting gracefully anyway."
sys.exit()
生成ex会产生不必要的时间和力量:
AAAFXEL
AAAFXEM
AAAFXEN
AAAFXEO
AAAFXEP
AAAFXEQ
AAAFXER
AAAFXES
答案 0 :(得分:0)
对于整个空间的非重复枚举,也许你想要itertools.permutations。
```python
from itertools import permutations
chars = 'ABCDEFGHIJKLMNOPQRSTUVXYZ' # chars to look for
try:
while 1:
# Iterate through possible keys
for length in range(7,8):
to_attempt = permutations(chars, length)
for attempt in to_attempt:
print(''.join(attempt))
except KeyboardInterrupt:
print "Keyboard interrupt, exiting gracefully anyway."
sys.exit()
```