How to generate passwords? [duplicate]
Possible Duplicate:
Get all possible word combinations
I have a string "abcdefgklmno0123456789"
I need to enumerate all the possible combinations with sizes ranging from 6 to 7 characters.
aaaaaa aaaaab aaaaac ... 999999
You could use Linq to do it:
string s = "abcdefgklmno0123456789";
var pwdWith6Chars =
from a in s
from b in s
from c in s
from d in s
from e in s
from f in s
select new string(new[] { a, b, c, d, e, f });
var pwdWith7Chars =
from a in s
from b in s
from c in s
from d in s
from e in s
from f in s
from g in s
select new string(new[] { a, b, c, d, e, f, g });
var passwords = pwdWith6Chars.Concat(pwdWith7Chars).ToList();
Please check out following link.
It will help you
http://www.codeproject.com/KB/recipes/Combinatorics.aspx
You could use a recursion to generate this list.
Pseudocode (more like C):
str = "abcdefgklmno0123456789"
res = empty string of 6 letters
results = list of string
function go(level) {
if (level == 6 or level == 7) {
add "res" to "results" list
if (level == 7) {
return
}
}
for(i=position; i<len(str); i++) {
res[level] = str[i]
go(level+1)
}
}
Then, you just call go(0).
Instead of use a result list, you could also use yield operator to create an iterator. Iterator has the advantage to save memory, because you will be yielding each combination instead of storing all of them.
精彩评论