我需要生成从给定字符集到给定范围的所有可能组合。 喜欢,
charset=list(map(str,"abcdefghijklmnopqrstuvwxyz"))
range=10
输出应该是,
[a,b,c,d..................,zzzzzzzzzy,zzzzzzzzzz]
我知道我可以使用已经在使用的库来做到这一点。但我需要知道它们是如何工作的。如果有人能用Python或任何可编程语言给我一个这种算法的注释代码,我会非常感激。
答案 0 :(得分:46)
使用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。
不尝试生成长度为10的26个字符的内存列表;相反,迭代产生的结果:
for attempt in bruteforce(string.ascii_lowercase, 10):
# match it against your password, or whatever
if matched:
break
答案 1 :(得分:17)
如果你真的想暴力破解它,试试这个,但这会花费你很多时间:
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']
答案 2 :(得分:4)
我找到了另一种使用itertools创建字典的简单方法。
for password in generator:
''.join(password)
这将遍历' a'''' c'的所有组合。并且' d'并创建总长度为1到4的组合。 A,B,C,d,AA,AB .........,DDDC,DDDD。 generator是一个itertool对象,你可以像这样正常循环,
myRef.addValueEventListener(new ValueEventListener() {
public static final String TAG = "Testtttttttt";
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
if (value == "START") {
textView.setText(value);
}
}
每个密码实际上都是元组类型,你可以照常处理它们。
答案 3 :(得分:3)
itertools
非常适合这个:
itertools.chain.from_iterable((''.join(l)
for l in itertools.product(charset, repeat=i))
for i in range(1, maxlen + 1))
答案 4 :(得分:3)
如果你真的想要一个暴力算法,不要将任何大的列表保存在你的计算机内存中,除非你想要一个因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秒,并且已经解决了#39;)。您也可以使用现有的密码字典或使用诸如cupp(github)
之类的通行费生成密码字典答案 5 :(得分:1)
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()
答案 6 :(得分: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
答案 7 :(得分:0)
试试这个:
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)
答案 8 :(得分:0)
使用递归的解决方案:
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")
答案 9 :(得分:0)
# 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,并使用输入功能,因此您可以将字符限制设置为&#34; hiiworld&#34;。下面是相同的脚本,但使用字母,数字,符号和空格更流畅的字符集。
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()