我需要根据给定的字符集和范围生成所有可能的组合。
charset=list(map(str,"abcdefghijklmnopqrstuvwxyz"))
range=10
输出应该是这样的:
[a,b,c,d..................,zzzzzzzzzy,zzzzzzzzzz]
我知道我可以使用已经存在的库来完成这个任务。但我需要知道它们是如何工作的。如果有人可以给我一个带注释的Python或任何可读的编程语言的代码段,我将不胜感激。
我需要根据给定的字符集和范围生成所有可能的组合。
charset=list(map(str,"abcdefghijklmnopqrstuvwxyz"))
range=10
输出应该是这样的:
[a,b,c,d..................,zzzzzzzzzy,zzzzzzzzzz]
我知道我可以使用已经存在的库来完成这个任务。但我需要知道它们是如何工作的。如果有人可以给我一个带注释的Python或任何可读的编程语言的代码段,我将不胜感激。
使用itertools.product
与itertools.chain
组合,将各个长度拼接在一起:
from itertools import chain, product
def bruteforce(charset, maxlength):
return (''.join(candidate)
for candidate in chain.from_iterable(product(charset, repeat=i)
for i in range(1, maxlength + 1)))
演示:
>>> list(bruteforce('abcde', 2))
['a', 'b', 'c', 'd', 'e', 'aa', 'ab', 'ac', 'ad', 'ae', 'ba', 'bb', 'bc', 'bd', 'be', 'ca', 'cb', 'cc', 'cd', 'ce', 'da', 'db', 'dc', 'dd', 'de', 'ea', 'eb', 'ec', 'ed', 'ee']
这将高效地使用输入集生成逐渐增长的单词,直到长度达到最大值maxlength。
不要尝试生成一个内存中包含26个字符的列表,直到长度为10;相反,应迭代生成的结果:
for attempt in bruteforce(string.ascii_lowercase, 10):
# match it against your password, or whatever
if matched:
break
itertools.product
方法,文档中有其实现列表,因此您可以轻松理解我们如何生成所有可能的组合。我更喜欢什么?在生成这么多数据时,确实需要使用像这样的生成器。 - Martijn Pieters如果您真的想要暴力破解它,请尝试这个方法,但是这将需要您花费荒谬的时间:
your_list = 'abcdefghijklmnopqrstuvwxyz'
complete_list = []
for current in xrange(10):
a = [i for i in your_list]
for y in xrange(current):
a = [x+i for i in your_list for x in a]
complete_list = complete_list+a
在一个更小的例子中,如果列表为'ab',并且我们只处理到5,则会打印以下内容:
['a', 'b', 'aa', 'ba', 'ab', 'bb', 'aaa', 'baa', 'aba', 'bba', 'aab', 'bab', 'abb', 'bbb', 'aaaa', 'baaa', 'abaa', 'bbaa', 'aaba', 'baba', 'abba', 'bbba', 'aaab', 'baab', 'abab', 'bbab', 'aabb', 'babb', 'abbb', 'bbbb', 'aaaaa', 'baaaa', 'abaaa', 'bbaaa', 'aabaa', 'babaa', 'abbaa', 'bbbaa', 'aaaba','baaba', 'ababa', 'bbaba', 'aabba', 'babba', 'abbba', 'bbbba', 'aaaab', 'baaab', 'abaab', 'bbaab', 'aabab', 'babab', 'abbab', 'bbbab', 'aaabb', 'baabb', 'ababb', 'bbabb', 'aabbb', 'babbb', 'abbbb', 'bbbbb']
我发现使用itertools创建字典的另一种非常简单的方法。
generator=itertools.combinations_with_replacement('abcd', 4 )
这将迭代所有由'a'、'b'、'c'和'd'组成的组合,并创建总长度为1到4的组合,即a、b、c、d、aa、ab……dddc、dddd。生成器是一个itertools对象,您可以像这样正常循环遍历:
for password in generator:
''.join(password)
实际上,每个密码都是元组类型,您可以像通常一样处理它们。
aa
、ba
、ab
和bb
。现在,使用combinations_with_replacement('ab', 2)
,itertools只会返回3种可能性,如下:aa
、ab
、bb
! - Daniel Biegler如果您真的想要一个暴力算法,不要在计算机内存中保存任何大型列表,除非您想要一个速度慢且会因MemoryError而崩溃的算法。
您可以尝试使用itertools.product,像这样:
from string import ascii_lowercase
from itertools import product
charset = ascii_lowercase # abcdefghijklmnopqrstuvwxyz
maxrange = 10
def solve_password(password, maxrange):
for i in range(maxrange+1):
for attempt in product(charset, repeat=i):
if ''.join(attempt) == password:
return ''.join(attempt)
solved = solve_password('solve', maxrange) # This worked for me in 2.51 sec
itertools.product(*iterables)
函数返回输入的可迭代对象的笛卡尔积。
[i for i in product('bar', (42,))]
返回例如[('b', 42), ('a', 42), ('r', 42)]
repeat
参数允许您精确地制作所需的内容:
[i for i in product('abc', repeat=2)]
[('a', 'a'),
('a', 'b'),
('a', 'c'),
('b', 'a'),
('b', 'b'),
('b', 'c'),
('c', 'a'),
('c', 'b'),
('c', 'c')]
注意:
你需要一个暴力算法,所以我给了你。但是,当密码变得更长时,它会呈指数增长,因此这是一种非常耗时的方法(找到单词“solved”花费了62秒)。
def brute(string, length, charset):
if len(string) == length:
return
for char in charset:
temp = string + char
print(temp)
brute(temp, length, charset)
使用方法:
brute("", 4, "rce")
itertools
非常适合这种情况:
itertools.chain.from_iterable((''.join(l)
for l in itertools.product(charset, repeat=i))
for i in range(1, maxlen + 1))
import string, itertools
#password = input("Enter password: ")
password = "abc"
characters = string.printable
def iter_all_strings():
length = 1
while True:
for s in itertools.product(characters, repeat=length):
yield "".join(s)
length +=1
for s in iter_all_strings():
print(s)
if s == password:
print('Password is {}'.format(s))
break
# modules to easily set characters and iterate over them
import itertools, string
# character limit so you don't run out of ram
maxChar = int(input('Character limit for password: '))
# file to save output to, so you can look over the output without using so much ram
output_file = open('insert filepath here', 'a+')
# this is the part that actually iterates over the valid characters, and stops at the
# character limit.
x = list(map(''.join, itertools.permutations(string.ascii_lowercase, maxChar)))
# writes the output of the above line to a file
output_file.write(str(x))
# saves the output to the file and closes it to preserve ram
output_file.close()
import itertools, string
maxChar = int(input('Character limit for password: '))
output_file = open('insert filepath here', 'a+')
x = list(map(''.join, itertools.permutations(string.printable, maxChar)))
x.write(str(x))
x.close()
from random import choice
sl = 4 #start length
ml = 8 #max length
ls = '9876543210qwertyuiopasdfghjklzxcvbnm' # list
g = 0
tries = 0
file = open("file.txt",'w') #your file
for j in range(0,len(ls)**4):
while sl <= ml:
i = 0
while i < sl:
file.write(choice(ls))
i += 1
sl += 1
file.write('\n')
g += 1
sl -= g
g = 0
print(tries)
tries += 1
file.close()
试试这个:
import os
import sys
Zeichen=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s",";t","u","v","w","x","y","z"]
def start(): input("Enter to start")
def Gen(stellen): if stellen==1: for i in Zeichen: print(i) elif stellen==2: for i in Zeichen: for r in Zeichen: print(i+r) elif stellen==3: for i in Zeichen: for r in Zeichen: for t in Zeichen: print(i+r+t) elif stellen==4: for i in Zeichen: for r in Zeichen: for t in Zeichen: for u in Zeichen: print(i+r+t+u) elif stellen==5: for i in Zeichen: for r in Zeichen: for t in Zeichen: for u in Zeichen: for o in Zeichen: print(i+r+t+u+o) else: print("done")
#*********************
start()
Gen(1)
Gen(2)
Gen(3)
Gen(4)
Gen(5)
list()
返回列表。map()
也返回一个列表。如果您的输入确实需要是一个列表(我怀疑),请使用charset = list(string.lowercase)
。 - kojiro