开发者

Java For Loop help

Hi I need to create a Java SHA1 brute force application and need help with the for开发者_如何学Python loops to create the words to guess. I need to guess passwords between 1 and 6 chars long. So I need a loop that starts with A...Z and then AA....AZ etc So adding letters as it goes along.

I am using a Char array to store the letters from a - z and numbers from 0 - 9. Thats what I am looping through.

This is my loop at the moment:

for (int a = 0; a<36; a++){

        guess[5] = letters[a];

         for (int b = 0; b<36; b++){

          guess[4] = letters[b];

             for (int c = 0; c<36; c++){

                    guess[3] = letters[c];

             for (int d = 0; d<36; d++){

                     guess[2] = letters[d];

                    for (int e = 0; e<36; e++){

                        guess[1] = letters[e];

                        for (int f = 0; f<36; f++){

                           guess[0] = letters[f];

thanks


You probably don't want to use arbitrarily nested loops for a brute force algorithm. Instead, look at it as counting in an arbitrary radix, represented by different symbols, such as [a-z, A-Z, 0-9, etc.] , but you could represent them as counting from say, 0-64 or whatever in a single digit. That way you may start with aaa, then eventually end up at aaA, and aa0 and eventually 999 if generating 3 digits.

With the way you're doing it, extending it to more digits would require modifying the code.


Your index into letters[i] looks wrong. Should that be the index for each loop level? In the first should that not be letters[a]?

It looks like that would loop through all of the combos, but you have a few incorrect index values and wow, I bet that would take a long time, LOL.


After finally understanding the question (thanks to @cHao), here's my proposal:

for (int len = 1; len <= 6; ++len) {
    // generate all words of length 'len'
    int [] index = new int[len + 1];
    while (index[len] == 0) {
        // generate the next guess
        for (int pos = 0; pos < len; ++pos) {
            guess[pos] = letters[index[pos]];
        }
        // increment the index
        for (int pos = 0; pos < len; ++pos) {
            if (index[pos] == letters.length - 1) {
                index[pos] = 0;
                // carry to the next position
            } else {
                ++index[pos];
                break;
            }
        }
        // use guess[0] through guess[len-1] as the next word at this point
    }
}


If your alphabet is 26 characters then your words correspond to numbers from 1 through 26^6 - 1. So one approach is:

int val = 1 + new java.util.Random().nextInt(308915774); // [0 to 26^6 - 1)
String word = Integer.toString(val, 26).toUpperCase(); // from [0-9,A-P]
word = word.replace('0', 'Q');
// etc for '1', through '9', where '9' -> Z

If you want to include 0-9 (36-character alphabet), then this trick isn't going to work because 36^6 - 1 > Integer.MAX_VALUE. I suggest then that you use Random.nextInt() to decide how many characters are going to be in your word and then generate exactly that many by using Random.nextInt() to generate random indices into your alphabet array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜