没有类似字符的密码迭代?

时间:2016-02-24 09:17:47

标签: python

我正在阅读这篇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

1 个答案:

答案 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()
```