我需要生成从给定字符集到给定范围的所有可能组合。像,
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
如果你真的想暴力破解,试试这个,但这会花费你大量的时间:
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
在一个较小的示例中,其中 list = '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。generator 是一个 itertool 对象,您可以像这样正常循环,
for password in generator:
''.join(password)
每个密码实际上都是元组类型,您可以像往常一样处理它们。
itertools
非常适合这个:
itertools.chain.from_iterable((''.join(l)
for l in itertools.product(charset, repeat=i))
for i in range(1, maxlen + 1))
使用递归的解决方案:
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")
如果您真的想要一个蛮力算法,请不要在您的计算机内存中保存任何大列表,除非您想要一个因 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')]
注意:
你想要一个蛮力算法,所以我给了你。现在,当密码开始变大时,这是一个非常长的方法,因为它呈指数增长(找到“已解决”这个词需要 62 秒)。
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
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()
# 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()
我将输出通过管道传输到一个文件以保存 ram,并使用了输入函数,因此您可以将字符限制设置为“hiiworld”之类的内容。下面是相同的脚本,但使用字母、数字、符号和空格的字符集更加流畅。
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()
试试这个:
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)