所有可能的 8 个符号字符串的生成器。暴力破解8符号密码。 Python

发布于 2024-11-14 09:49:55 字数 421 浏览 2 评论 0原文

我需要编写生成器来生成所有可能的 8 个符号字符串。 从这样的符号数组:

leters = ['1','2','3','4','5','6','7','8','9','0','q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m']

骨架看起来像这样:

def generator():
    """
    here algorithm
    """
    yield string

假设返回这样的列表 ['00000001','00000002','00000003', ......'mmmmmmmm']

I need to write generator which yield all possible 8 symbols strings.
From array of symbols like this:

leters = ['1','2','3','4','5','6','7','8','9','0','q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m']

The skeleton looks like this:

def generator():
    """
    here algorithm
    """
    yield string

suppose to return list like this ['00000001','00000002','00000003', ......'mmmmmmmm']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱&纷飞 2024-11-21 09:49:55

itertools.combinations()itertools.combinations_with_replacement() 返回一个生成器,

>>> letters = ['a', 'b', 'c']
>>> from itertools import combinations

我在示例中使用 print() 来说明输出。将其替换为 yield,以获得生成器。

>>> for c in combinations(letters, 2): 
        print(c)
... 
('a', 'b')
('a', 'c')
('b', 'c')

>>> for c in combinations(letters, 2): 
        print(''.join(c))
... 
ab
ac
bc
>>> 

>>> for c in itertools.combinations_with_replacement(letters, 2): 
        print(''.join(c))
... 
aa
ab
ac
bb
bc
cc

如果您对包含英文字母和数字的所有 8 个字母密码进行暴力破解,则需要迭代约 2.8 万亿个字符串

编辑
如果您以某种方式知道没有重复元素,请使用 permutations

>>> for c in itertools.permutations(letters, 2): 
        print(''.join(c))
... 
ab
ac
ba
bc
ca
cb

这会为您提供 abba

对于最常见的暴力序列,请使用 itertools.product() 如 Cosmologicon 的解决方案中所示

itertools.combinations() and itertools.combinations_with_replacement() return a generator

>>> letters = ['a', 'b', 'c']
>>> from itertools import combinations

I am using print() in the examples to illustrate the output. Substitute it with yield, to get a generator.

>>> for c in combinations(letters, 2): 
        print(c)
... 
('a', 'b')
('a', 'c')
('b', 'c')

>>> for c in combinations(letters, 2): 
        print(''.join(c))
... 
ab
ac
bc
>>> 

>>> for c in itertools.combinations_with_replacement(letters, 2): 
        print(''.join(c))
... 
aa
ab
ac
bb
bc
cc

If you brute force it for all 8 letter passwords containing english letters and digits, you're looking to iterate over ~ 2.8 trillion strings

EDIT
If you somehow know there are no repeated elements, use permutations

>>> for c in itertools.permutations(letters, 2): 
        print(''.join(c))
... 
ab
ac
ba
bc
ca
cb

this gives you both ab and ba

For the most general brute force sequence use itertools.product() as in Cosmologicon's solution

清风夜微凉 2024-11-21 09:49:55
itertools.product(leters, repeat=8)

编辑:让它给你字符串而不是元组:

def generator(leters):
    a = itertools.product(leters,repeat=3)
    while a:
        yield "".join(a.next())
itertools.product(leters, repeat=8)

EDIT: to have it give you strings rather than tuples:

def generator(leters):
    a = itertools.product(leters,repeat=3)
    while a:
        yield "".join(a.next())
还如梦归 2024-11-21 09:49:55
import itertools
itertools.combinations_with_replacement(leters, 8)

顺便说一句,字母有两个 T。

import itertools
itertools.combinations_with_replacement(leters, 8)

By the way, letters has two T's.

惟欲睡 2024-11-21 09:49:55

我也想知道如何做到这一点,这就是我想出的办法,我尝试了几种方法,但是当我这样写时,它比其他方法快得多......如果我没有看到,请lmk

import string

from itertools import permutations

[print(*p,sep='')for p in permutations(list(string.ascii_letters+string.punctuation+string.digits),8)]

I was wondering how to do this as well and this is what I came up with I tried it a couple ways but when I wrote it like this it went way quicker than the others...please lmk if I am not seeing

import string

from itertools import permutations

[print(*p,sep='')for p in permutations(list(string.ascii_letters+string.punctuation+string.digits),8)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文