I'm new at C++, I am trying to make some brute force password cracking.

At the moment, it loops from aaaaa to zzzzz.

question : Is it possible to make it like from a to zzzzz?

I am working on this for like non-stop 6 hours and still could't find a solution.

Waiting for your help, thanks!

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string password;
    string generatedPass;
    int x;

    cin >> password;

    char digitArray[]={96,97,97,97,97,97,97,97,97,97};
    int lenght=0;
    int maxLenght=password.length();


    while( password.compare(generatedPass) != 0 ){
        digitArray[0]++;
        for(int x=0;x<password.length();x++){
           if (digitArray[x] == 123)
                {
                    digitArray[x] = 97;
                    digitArray[x + 1]++;
                }
        }

        // creating the string with those digits
        generatedPass=digitArray[password.length()-1]; // i set the first one here

        for(int      i=password.length()-2   ;  i>=0  ;  i-- )
            generatedPass+= digitArray[i]; // now i add other digits to next to first digit

            cout << generatedPass << endl; // i put it to test how it looks
    }

    cout << generatedPass << endl; // gives the result
    return 0;
}
有帮助吗?

解决方案

Looks like there is a pattern here:

    if(digitArray[0]==123){     // i designed it like stopwatch, if like when second is 60, you make it 0 and increment minute by 1
        digitArray[0]=97;       // 97 is the character 'a' in ASCII table, 122 is the 'z'
        digitArray[1]++;
    }

    // HELP HERE, PLEASE!
    //
    // my program loops itself after this, copy paste doesn't look cool. how can i improve this part? that 4 in the digitArray[4] is maximum passwordlenght
    // im working on this like nonstop 6 hours and still couldnt find a solution...

    if(digitArray[1]==123){
        digitArray[1]=97;
        digitArray[2]++;
    }

    if(digitArray[2]==123){
        digitArray[2]=97;
        digitArray[3]++;
    }

The generic form of the pattern is:

if (digitArray[x] == 123)
{
    digitArray[x] = 97;
    digitArray[x + 1]++;
}

Except for the last case. I'll let you figure out how to put in into a for loop.

Edit 1: creating passwords from 'a' to 'zzzzzz'
This is much like counting in base 26 or base 27 (depending on if you count spaces).

Let's consider adding. Assuming that the letters 'a' to 'z' are contiguous.

std::fill(&digitArray[0], &digitArray[sizeof(digitArray)], ' '); // Initialize
unsigned int last_digit_index = sizeof(digitArray) - 1U;
if (digitArray[last_digit_index] == ' ')
{
  digitArray[last_digit_index] = 'a';
else
{
  ++digitArray[last_digit_index];
}

There is a problem here and that is handling overflow, otherwise known as carry.

For overflow, the digit needs to be reset to ' ', and the next (or previous) digit needs to be incremented.

The above code takes care of incrementing the value, so only the index needs to be changed.

Changing of the index and looping (or recursing) is left as an exercise for the OP.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top