1

I'm making a password brute forcing tool as a learning exercise, and I want it to be resumable.

So, what I want is to be able to say, this is the set of possible characters, if I computed the Cartesian set of every possible combination of this set up to length n, what is the set at point x?

However, I want to do this without computing the entire set. I've seen similar logic in one place online but I was unable to generalise this to fit.

Any help would be fantastic, thanks! I'm fluent in C# if that helps.

Edit: Here's the question I mentioned earlier: How to select specific item from cartesian product without calculating every other item

Edit: here's an example of what I mean:

Char set = [abcd]

Length n = 4

Permutations:

[aaaa]
[aaab]
[aaac]
[aaad]
[aaba]
....
[dddd]

So if I'm searching for the set at 4, I'd get [aaad]. But if I'm searching for element 7000, then it takes a long time to get to that point.

Community
  • 1
  • 1
Transmission
  • 1,219
  • 1
  • 10
  • 11

1 Answers1

1

This implements the answer to the question you link:

static string Get(string chars, int n, int i)
{
    string ret = "";
    int sizes = 1;
    for (int j = 0; j < n; j++) {
        ret = chars[(i / sizes) % chars.Length] + ret;
        sizes *= chars.Length;
    }
    return ret;
}

Example:

string chars = "abcd";
int n = 3;

for (int i = 0; i < Math.Pow(chars.Length, n); i++)
    Console.WriteLine(i + "\t" + Get(chars, n, i));
0       aaa
1       aab
2       aac
3       aad
...
61      ddb
62      ddc
63      ddd
Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
  • Thanks for this, it's perfect, exactly what I was after! I can't upvote you until I have 15 rep, but I marked you as correct, I hope that's okay? – Transmission Mar 08 '14 at 16:36
  • By the way, when I'm running your method with a char length of 62, an n of 900000, and an i of 500,000,000, it's taking about 23 minutes to complete, if that reasonable, or do you think I've introduced a bug? – Transmission Mar 08 '14 at 20:01
  • 1
    @Transmission 23 minutes for a single call to the method? – Julián Urbano Mar 08 '14 at 20:07
  • Yes, I think the lag is being introduced by the use of BigInteger to handle the huge numbers in play above. Not sure if there's a way round that. – Transmission Mar 08 '14 at 21:05
  • @Transmission yes, seems like it. 62 chars for passwords of length 900000 (btw, 900k length! wtf!) gives you a ridiculously large number that I bet the computer can't even represent. – Julián Urbano Mar 09 '14 at 03:10
  • You're right. I'm wondering if I should somehow break the computation into blocks, or if there's a possibility that I could generate the huge numbers required, then read them into an array of ints or similar, and do some kind of discrete logic FSA on that? – Transmission Mar 09 '14 at 18:42