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

2024-09-28 01:25:15 发布

您现在位置: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

Tags: oftokey字符串in密码for字母
1条回答
网友
1楼 · 发布于 2024-09-28 01:25:15

对于整个空间的非重复枚举,可能需要itertools.排列. 你知道吗

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

相关问题 更多 >

    热门问题