Brute-Force-Passwort-Cracker

84924
Jamal

Ich programmiere nur ein klassisches Brute-Force-Programm zum Knacken von Passwörtern, nur um mich zu verbessern.

Ich habe erklärt, wie mein Programm am Anfang des Codes funktioniert. Schauen Sie sich einige dieser Screenshots an, um sie besser zu verstehen.

Mein Programm funktioniert wirklich gut, aber es ist ein bisschen schmutzig und es kann schneller sein, wenn ich diese beiden Probleme löse:

  1. Der Hauptcode ist nicht so lang. Es sieht so lang und schmutzig aus, weil ich einen Codeblock 8 mal in einer switch case-Anweisung kopiert habe. Beispiel: Schleifen von Fall 1 mit Kennwörtern mit einer Zeichenlänge. case 2= zwei Zeichen, case 8= 8 Zeichen langes Passwort. Der einzige Unterschied zwischen diesen Fällen ist die "for-Schleife" -Zählung. case 1hat 1 forSchleife, case 8hat 8 verschachtelte Schleifen. Ich möchte meinen Code schöner machen, also wie kann ich diesen kopierten / eingefügten Code loswerden und ihn auf 1/8 der aktuellen Größe bringen? STRG + MAUSRAD NACH UNTEN, verkleinern und die eingefügten Teile sehen.

  2. Es wird zuerst eine Ziffer, dann zwei Ziffern, dann drei Ziffern usw. versucht. Es sollte also warten, bis 1, 2, 3 die vierstelligen Werte erhalten. Und das Programm verliert so viel Zeit bei höheren Ziffern. Meine CPU i7 3770k hat 6 Kerne und das Programm läuft nur mit einem. Ich denke, es liegt daran, dass es 13% CPU-Auslastung gibt. Ich möchte es höher machen, wie 6 Kerne für dieselbe Aufgabe, oder jeder Kern kümmert sich um einen Teil. Der erste Kern beginnt, nur die 8 Zeichen zu schleifen, und der zweite Kern macht das Gleiche mit den 7 Zeichen ... und wenn einer von ihnen die Antwort findet, endet das Programm. Können wir das wirklich tun?

Hier ist der Code

#include <iostream>
#include <ctime>
using namespace std;
string crackPassword(string pass);
long long int attempt;
clock_t start_t, end_t;

int main(){
    string password;

    cout << "Enter the password to crack : ";
    cin >> password;

    cout << endl << endl << endl << ">\n>> CRACKED THE PASSWORD! >>\n>" << endl <<  endl <<"The password : " << crackPassword(password) << endl;
    cout << "The number of attempts : " << attempt << endl;
    cout << "The time duration  passed : " << (double)(end_t - start_t)/1000 << " seconds" << endl << endl;
    return 0;
}

string crackPassword(string pass){
    int digit[7],alphabetSet=1,passwordLength=1;
    start_t = clock();

    string test,alphabet = "1337 also daktari is pro";
    while(1){


            switch(passwordLength){
                case 1:
                    while(alphabetSet<4){
                    switch(alphabetSet){
                        case 1 : alphabet = "-0123456789";
                                 cout << endl << endl <<"Testing only digits(0123456789) - 10 Characters, please wait";  break;
                        case 2 : alphabet = "-abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only lowercase characters(abcdefghijklmnopqrstuvwxyz) - 26 Characters, please wait";  break;
                        case 3 : alphabet = "-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only uppercase characters(ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 26 Characters, please wait";  break;
                        }

                    for(digit[0]=1;digit[0]<alphabet.length();digit[0]++){
                                                    attempt++;
                                                    if(attempt%2500000==0) cout << ".";
                                                    test=alphabet[digit[0]];
                                                    for(int i=1;i<passwordLength;i++)
                                                        if(alphabet[digit[i]]!='-')test+=alphabet[digit[i]];
                                                    if(pass.compare(test)==0){end_t = clock(); return test;}
                                                    }
                                                    alphabetSet++;
                    }
                    break;
                case 2:
                    alphabetSet=1;
                    while(alphabetSet<6){
                    switch(alphabetSet){
                        case 1 : alphabet = "-0123456789";
                                 cout << endl << endl <<"Testing only digits(0123456789) - 10 Characters, please wait";  break;
                        case 2 : alphabet = "-abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only lowercase characters(abcdefghijklmnopqrstuvwxyz) - 26 Characters, please wait";  break;
                        case 3 : alphabet = "-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only uppercase characters(ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 26 Characters, please wait";  break;
                        case 4 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyz) - 36 Characters, please wait";  break;
                        case 5 : alphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                        }


                         for(digit[1]=0;digit[1]<alphabet.length();digit[1]++)
                             for(digit[0]=1;digit[0]<alphabet.length();digit[0]++){
                                                    attempt++;
                                                    if(attempt%2500000==0) cout << ".";
                                                    test=alphabet[digit[0]];
                                                    for(int i=1;i<passwordLength;i++)
                                                        if(alphabet[digit[i]]!='-')test+=alphabet[digit[i]];
                                                    if(pass.compare(test)==0){end_t = clock(); return test;}
                                                    }
                                                    alphabetSet++;
                    }
                    break;
                case 3:
                    alphabetSet=1;
                    while(alphabetSet<8){
                    switch(alphabetSet){
                        case 1 : alphabet = "-0123456789";
                                 cout << endl << endl <<"Testing only digits(0123456789) - 10 Characters, please wait";  break;
                        case 2 : alphabet = "-abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only lowercase characters(abcdefghijklmnopqrstuvwxyz) - 26 Characters, please wait";  break;
                        case 3 : alphabet = "-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only uppercase characters(ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 26 Characters, please wait";  break;
                        case 4 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyz) - 36 Characters, please wait";  break;
                        case 5 : alphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing uppercase characters and numbers(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 36 Characters, please wait";  break;
                        case 6 : alphabet = "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 52 Characters, please wait";  break;
                        case 7 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 62 Characters, please wait";  break;
                    }
                                        for(digit[2]=0;digit[2]<alphabet.length();digit[2]++)
                                            for(digit[1]=0;digit[1]<alphabet.length();digit[1]++)
                                                for(digit[0]=1;digit[0]<alphabet.length();digit[0]++){
                                                    attempt++;
                                                    if(attempt%2500000==0) cout << ".";
                                                    test=alphabet[digit[0]];
                                                    for(int i=1;i<passwordLength;i++)
                                                        if(alphabet[digit[i]]!='-')test+=alphabet[digit[i]];
                                                    if(pass.compare(test)==0){end_t = clock(); return test;}
                                                    }
                                                    alphabetSet++;
                    }
                    break;
                case 4:
                    alphabetSet=1;
                    while(alphabetSet<8){
                    switch(alphabetSet){
                        case 1 : alphabet = "-0123456789";
                                 cout << endl << endl <<"Testing only digits(0123456789) - 10 Characters, please wait";  break;
                        case 2 : alphabet = "-abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only lowercase characters(abcdefghijklmnopqrstuvwxyz) - 26 Characters, please wait";  break;
                        case 3 : alphabet = "-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only uppercase characters(ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 26 Characters, please wait";  break;
                        case 4 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyz) - 36 Characters, please wait";  break;
                        case 5 : alphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing uppercase characters and numbers(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 36 Characters, please wait";  break;
                        case 6 : alphabet = "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 52 Characters, please wait";  break;
                        case 7 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 62 Characters, please wait";  break;
                    }

                                    for(digit[3]=0;digit[3]<alphabet.length();digit[3]++)
                                        for(digit[2]=0;digit[2]<alphabet.length();digit[2]++)
                                            for(digit[1]=0;digit[1]<alphabet.length();digit[1]++)
                                                for(digit[0]=1;digit[0]<alphabet.length();digit[0]++){
                                                    attempt++;
                                                    if(attempt%2500000==0) cout << ".";
                                                    test=alphabet[digit[0]];
                                                    for(int i=1;i<passwordLength;i++)
                                                        if(alphabet[digit[i]]!='-')test+=alphabet[digit[i]];
                                                    if(pass.compare(test)==0){end_t = clock(); return test;}
                                                    }
                                                    alphabetSet++;
                    }
                    break;
                case 5:
                    alphabetSet=1;
                    while(alphabetSet<8){
                    switch(alphabetSet){
                        case 1 : alphabet = "-0123456789";
                                 cout << endl << endl <<"Testing only digits(0123456789) - 10 Characters, please wait";  break;
                        case 2 : alphabet = "-abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only lowercase characters(abcdefghijklmnopqrstuvwxyz) - 26 Characters, please wait";  break;
                        case 3 : alphabet = "-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only uppercase characters(ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 26 Characters, please wait";  break;
                        case 4 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyz) - 36 Characters, please wait";  break;
                        case 5 : alphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing uppercase characters and numbers(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 36 Characters, please wait";  break;
                        case 6 : alphabet = "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 52 Characters, please wait";  break;
                        case 7 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 62 Characters, please wait";  break;
                    }
                                for(digit[4]=0;digit[4]<alphabet.length();digit[4]++)
                                    for(digit[3]=0;digit[3]<alphabet.length();digit[3]++)
                                        for(digit[2]=0;digit[2]<alphabet.length();digit[2]++)
                                            for(digit[1]=0;digit[1]<alphabet.length();digit[1]++)
                                                for(digit[0]=1;digit[0]<alphabet.length();digit[0]++){
                                                    attempt++;
                                                    if(attempt%2500000==0) cout << ".";
                                                    test=alphabet[digit[0]];
                                                    for(int i=1;i<passwordLength;i++)
                                                        if(alphabet[digit[i]]!='-')test+=alphabet[digit[i]];
                                                    if(pass.compare(test)==0){end_t = clock(); return test;}
                                                    }
                                                    alphabetSet++;
                    }
                    break;
                case 6:
                    alphabetSet=1;
                    while(alphabetSet<8){
                    switch(alphabetSet){
                        case 1 : alphabet = "-0123456789";
                                 cout << endl << endl <<"Testing only digits(0123456789) - 10 Characters, please wait";  break;
                        case 2 : alphabet = "-abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only lowercase characters(abcdefghijklmnopqrstuvwxyz) - 26 Characters, please wait";  break;
                        case 3 : alphabet = "-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only uppercase characters(ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 26 Characters, please wait";  break;
                        case 4 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyz) - 36 Characters, please wait";  break;
                        case 5 : alphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing uppercase characters and numbers(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 36 Characters, please wait";  break;
                        case 6 : alphabet = "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 52 Characters, please wait";  break;
                        case 7 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 62 Characters, please wait";  break;
                    }
                            for(digit[5]=0;digit[5]<alphabet.length();digit[5]++)
                                for(digit[4]=0;digit[4]<alphabet.length();digit[4]++)
                                    for(digit[3]=0;digit[3]<alphabet.length();digit[3]++)
                                        for(digit[2]=0;digit[2]<alphabet.length();digit[2]++)
                                            for(digit[1]=0;digit[1]<alphabet.length();digit[1]++)
                                                for(digit[0]=1;digit[0]<alphabet.length();digit[0]++){
                                                    attempt++;
                                                    if(attempt%2500000==0) cout << ".";
                                                    test=alphabet[digit[0]];
                                                    for(int i=1;i<passwordLength;i++)
                                                        if(alphabet[digit[i]]!='-')test+=alphabet[digit[i]];
                                                    if(pass.compare(test)==0){end_t = clock(); return test;}
                                                    }
                                                    alphabetSet++;
                    }
                    break;
                case 7:
                    alphabetSet=1;
                    while(alphabetSet<8){
                    switch(alphabetSet){
                        case 1 : alphabet = "-0123456789";
                                 cout << endl << endl <<"Testing only digits(0123456789) - 10 Characters, please wait";  break;
                        case 2 : alphabet = "-abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only lowercase characters(abcdefghijklmnopqrstuvwxyz) - 26 Characters, please wait";  break;
                        case 3 : alphabet = "-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only uppercase characters(ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 26 Characters, please wait";  break;
                        case 4 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyz) - 36 Characters, please wait";  break;
                        case 5 : alphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing uppercase characters and numbers(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 36 Characters, please wait";  break;
                        case 6 : alphabet = "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 52 Characters, please wait";  break;
                        case 7 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 62 Characters, please wait";  break;
                    }
                        for(digit[6]=0;digit[6]<alphabet.length();digit[6]++)
                            for(digit[5]=0;digit[5]<alphabet.length();digit[5]++)
                                for(digit[4]=0;digit[4]<alphabet.length();digit[4]++)
                                    for(digit[3]=0;digit[3]<alphabet.length();digit[3]++)
                                        for(digit[2]=0;digit[2]<alphabet.length();digit[2]++)
                                            for(digit[1]=0;digit[1]<alphabet.length();digit[1]++)
                                                for(digit[0]=1;digit[0]<alphabet.length();digit[0]++){
                                                    attempt++;
                                                    if(attempt%2500000==0) cout << ".";
                                                    test=alphabet[digit[0]];
                                                    for(int i=1;i<passwordLength;i++)
                                                        if(alphabet[digit[i]]!='-')test+=alphabet[digit[i]];
                                                    if(pass.compare(test)==0){end_t = clock(); return test;}
                                                    }
                                                    alphabetSet++;
                    }
                    break;
                case 8:
                    alphabetSet=1;
                    while(alphabetSet<8){
                    switch(alphabetSet){
                        case 1 : alphabet = "-0123456789";
                                 cout << endl << endl <<"Testing only digits(0123456789) - 10 Characters, please wait";  break;
                        case 2 : alphabet = "-abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only lowercase characters(abcdefghijklmnopqrstuvwxyz) - 26 Characters, please wait";  break;
                        case 3 : alphabet = "-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing only uppercase characters(ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 26 Characters, please wait";  break;
                        case 4 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyz";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyz) - 36 Characters, please wait";  break;
                        case 5 : alphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing uppercase characters and numbers(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) - 36 Characters, please wait";  break;
                        case 6 : alphabet = "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 52 Characters, please wait";  break;
                        case 7 : alphabet = "-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                 cout << endl << endl << "Couldn't find the password, increasing the searching level."<< endl << endl << "Testing lowercase, uppercase characters and numbers(0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) - 62 Characters, please wait";  break;
                    }
                    for(digit[7]=0;digit[7]<alphabet.length();digit[7]++)
                        for(digit[6]=0;digit[6]<alphabet.length();digit[6]++)
                            for(digit[5]=0;digit[5]<alphabet.length();digit[5]++)
                                for(digit[4]=0;digit[4]<alphabet.length();digit[4]++)
                                    for(digit[3]=0;digit[3]<alphabet.length();digit[3]++)
                                        for(digit[2]=0;digit[2]<alphabet.length();digit[2]++)
                                            for(digit[1]=0;digit[1]<alphabet.length();digit[1]++)
                                                for(digit[0]=1;digit[0]<alphabet.length();digit[0]++){
                                                    attempt++;
                                                    if(attempt%2500000==0) cout << ".";
                                                    test=alphabet[digit[0]];
                                                    for(int i=1;i<passwordLength;i++)
                                                        if(alphabet[digit[i]]!='-')test+=alphabet[digit[i]];
                                                    if(pass.compare(test)==0){end_t = clock(); return test;}
                                                    }
                                                    alphabetSet++;
                    }
                    break;
            }
            cout  << endl << endl << endl << endl << "*" << endl;
            cout  << "*** Password length is not " << passwordLength <<  ". Increasing password length! ***";
            cout  << endl << "*" << endl << endl;
            passwordLength++;
    }
 }
Antworten
24
"Ich möchte es höher machen als 6 Kerne ... Können wir es wirklich tun?" Ja das geht. Um dies zu tun, müssen Sie "Multithreading" verwenden. Unterstützung für Multithreading ist in C ++ 11 integriert ([siehe zum Beispiel hier] (http://www.cplusplus.com/reference/thread/thread/)). ChrisW vor 5 Jahren 0

3 Antworten auf die Frage

15
Jamal
  • Prefer not to get into the habit of using using namespace std.

  • Make sure to include <string>.

  • For the <ctime> library, you should use std::clock_t instead of clock_t.

  • Prefer to avoid global variables:

    long long int attempt;
    clock_t start_t, end_t;
    

    As these variables can be modified anywhere in the program, you could introduce bugs, which will also hurt maintainability and testability.

    You should have attempt initialized to 0 (it's being incremented) in main() and pass it to crackPassword() by reference. In this way, you'll know that only these two functions can recognize attempt (if you ever add additional functions).

    start_t and end_t just need to be in main(). I'd also recommend renaming them (especially remove the _t), otherwise it may look like they're part of the library.

  • There's no need to use std::endl so many times, which also flushes the buffer, needlessly slowing down the code. Just use "\n", which only gives a newline. It'll also make the code a bit shorter, especially in places where it can be put into an existing hard-coded output line.

    This, for instance:

    cout << endl << endl << endl << ">\n>> CRACKED THE PASSWORD! >>\n>" << endl <<  endl <<"The password : " << crackPassword(password) << endl;
    

    would turn into this:

    cout << "\n\n\n>\n>> CRACKED THE PASSWORD! >>\n>\n\n The password : " << crackPassword(password) << "\n";
    

    You could also split this into separate lines for clarity, and to keep the lines shorter:

    cout << "\n\n\n>\n>> CRACKED THE PASSWORD! >>\n>\n\n;
    cout << "The password : " << crackPassword(password) << "\n";
    
  • You don't specifically need a C-style cast here:

    (double)(end_t - start_t)/1000
    

    Cast the C++ way, with static_cast<>:

    static_cast<double>(end_t - start_t)/1000
    

    Also, in case you'll need to use this in other places, consider having it as a variable. You should also use the CLOCKS_PER_SEC macro, which is part of the library.

    double timeDuration = static_cast<double>(end_t - start_t) / CLOCKS_PER_SEC;
    
  • In crackPassword(), pass should be passed by const& instead of by value as it's not being modified inside the function. This will also save an unnecessary copy.

    string crackPassword(string const& pass){
    
  • Prefer to have one variable declaration/initialization per line:

    int digit[7];
    int alphabetSet=1;
    int passwordLength=1;
    

    This will allow each variable to be more visible. It will also be possible to add a comment for separate variables if needed.

  • A line like this:

    for(digit[0]=1;digit[0]<alphabet.length();digit[0]++){
    

    should use appropriate whitespace for readability:

    for (digit[0] = 1; digit[0] < alphabet.length(); digit[0]++) {
    

    Generally, keep whitespace between operators, and in the case of for loop statements, separate each part as well.

    If this is being avoided because the line is too long, then it should be effectively shortened in some other way. All of that will help with readability.

Können Sie mir erklären, warum die Verwendung von Namespace std schlecht ist? Danke übrigens, diese Informationen sind für mich nützlich! vor 5 Jahren 0
@daktari: Im Allgemeinen wird davon abgeraten, dass der STL-Code (der im `std`-Namespace enthalten ist) für den globalen Namespace verfügbar gemacht wird. Wenn Sie beispielsweise eine eigene `string'-Klasse erstellen und einige der gleichen Namen verwenden, kommt es bei` using namespace std` zu Namenskollisionen. Dies führt zu Mehrdeutigkeiten und kann die Kompilierung verhindern. Für kleine Anwendungen oder wenn Sie dies in einen lokalen Bereich (z. B. eine Funktion) legen, ist dies keine große Sache. Wenn es jedoch global ist, ist es dem gesamten Programm ausgesetzt. Jamal vor 5 Jahren 2
9
Jerry Coffin

@ChrisW raises a good point, but doesn't take the concept as far as I would.

He's absolutely correct that what you're doing it basically just counting. What he doesn't point out is that it can (and probably should) be implemented as actual counting.

For example, to test all passwords up to 8 characters long, using only digits for the alphabet, we end up simply counting from 0 to 99999999. We convert each of those from a number to a string, then test the resulting string.

I would, however, advise against the strategy you suggested of testing 1 digit numbers in one thread, 2 digit numbers in a second thread, and so on. The problem is fairly simple: each digit you add multiplies the number of combinations by 10. Your first thread, testing 1-digit passwords, only has 10 possibilities. By the time you get to the last thread (8-bit passwords) it has 108 times as many possibilities as the first. Clearly the first will finish much more quickly than the last; most of the time will still be consumed by only one thread (running on only one core).

Instead, you want to split your overall range into a set of equal-sized sub-ranges. Given 100000000 combinations, you want to test approximately 100000000/8 = 12500000 possibilities on each core. To do that, you have one thread test possibilities from 0 to 12500000, the next from 12500000 to 25000000, and so on until you reach 99999999. This way, each thread does approximately equal work, so all the cores share the work about equally.

There are a couple of different ways to do that. One would be to explicitly create threads, one for each range of numbers. Another possibility would be to leave most of the code as a fairly simple loop, and use OpenMP to split that loop up into threads:

#pragma omp parallel for
for (long long i = 0; i<9999999ULL; i++) {
    std::string candidate = std::to_string(i);
    if (test_password(candidate)) 
        std::cout<<"We found it!\n"<<candidate<<"\n";
}   

Doing a quick test on my laptop, this reduced the search time for a password from about 3 seconds to less than 1 second. Of course, the absolute speed will depend on how long it takes to test a password, but as long as you can test passwords concurrently from multiple threads (or testing a password is a lot faster than generating one) you can gain considerable speed from multithreading this way.

If you want to do roughly the same thing, but with (for example) a mixture of letters and digits, you can still use the same basic idea. The only difference is the base in which you convert the numbers to a string. For example, to use only letters (not digits) you could do something like this:

std::string to_string(long long val) { 
    std::string ret;
    while (val) { 
        ret += ('a' + val % 26);
        val /= 26;
    }
}

To create a mixture of digits and letters, it's generally easiest to start with an array of the characters you want to use, then index into that array:

char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";

std::string to_string(long long val) { 
    std::string ret;
    static const size_t size = sizeof(digits) -1;
    while (val) { 
        ret += digits[val % size];
        val /= size;
    }
}

At some point, this won't work correctly any more, simply because a long long (or even an unsigned long long) is large enough to produce all the numbers in a given range. If you need/want to search that large of a range, you'll probably need to do other things to improve your search though. An unsigned long long has a range of at least 64 bits, and generating all 64-bit numbers will take longer than you're probably willing to wait (even if we ignore conversion and testing the results, just counting from 0 to 0xffffffffffffffff will take longer than most people are willing to wait.

Hier ist der Fix, wenn es einige Leute gibt, die das gleiche Problem mit to_string und haben : [to_string Fix] (http://tehsausage.com/mingw-to-string) vor 5 Jahren 0
Ich habe nur beide Buchstaben und die "Mischung aus Ziffern und Buchstaben" ausprobiert, beide laufen von 1 bis 999999, ich sehe keinen Buchstaben. `std :: string to_string (long long val); int main () {std :: string password; std :: cin >> password; für (lang lang i = 0; i <9999999ULL; i ++) {std :: string Kandidat = std :: to_string (i); if (password.compare (kandidat)) std :: cout << "\ n" < vor 5 Jahren 0
@daktari Dies ist ein Buchstabe aus dem Alphabet aus Kleinbuchstaben: `('a' + val% 26)`; weil "val% 26" eine Zahl ist; Wenn "val% 26" gleich "0" ist, lautet der Buchstabe "a" + 0 ", was" a "ist. Wenn "val% 26" gleich "1" ist, lautet der Buchstabe "a" + 1 "" b ""; usw. Alternativ ist dies ein Buchstabe aus dem Wörterbuch, der als "Ziffern" bezeichnet wird: "Ziffern [Wert% Größe]" ChrisW vor 5 Jahren 0
Ja, aber wie gesagt, es schleift nur Zahlen, wenn ich sie drucke, auch wenn es a, b, c macht, fügt es sie zusammen: zz dann aaa dann aab dann aac usw. vor 5 Jahren 0
6
ChrisW

The one and only difference between those cases is the "for loop" count, case 1 got 1 for loop, case 8 got 8 nested for loops. I want to make my code prettier, so how can I get rid of this copy&paste code and make it 1/8 size of current size.

Iterating through passwords is like counting numbers:

  • Start with the first digit "0"
  • Increment until you get to the last digit "9"
  • Next increment, increment the next digit and reset this digit "10"
  • Increment the first digit again: "11", "12", etc.
  • Increment the next digit when you have to: "20"
  • If you can't increment the next digit then increment the one after that: "99" "100"

So something like (untested code ahead):

// try up to 8 digit password
char password[9];
// password is null terminated
password[8] = 0;
// password is initially zero-filled
memset(password,0,8);
// start at last character and make it bigger by building to the left
char* pass = &password[7];

for (;;)
{
    for(int i = 0; i < alphabet.length; ++i)
    {
        password[7] = alphabet[i];
        // test for password match here
        if (test.compare(pass))
            return string(pass); // found!
    }
    // increment one or more chars to the left
    // and maybe decrement pass to make it bigger
    // before we run the above for loop again on the right-most char
    for (int j = 6; j >= 0; --j)
    {
        if (password[j] == 0)
        {
            // first time we've overflowed this high
            --pass;
            password[j] = alphabet[0];
            break;
        }
        // increment the existing character
        string::size_t found = alphabet.find(password[j]);
        ++found;
        if (found < alphabet.length)
        {
            password[j] = alphabet[found];
            break;
        }
        // else need to overflow to the next higher
        password[j] = alphabet[0];
        if (j == 0)
        {
            // can't go higher, return failure
            return string("");
        }
        continue; // i.e. try again with --j    
    }
}
Ich teste es, es sieht wirklich gut aus, aber was ist mit "Rekursion" für "verschachtelt für Schleifen"? vor 5 Jahren 0
Rekursion scheint mir eine andere, gültige Möglichkeit zu sein, sie umzusetzen. ChrisW vor 5 Jahren 0
Ich konnte meine verschachtelte Schleife nicht in eine Rekursionsfunktion konvertieren, sie ist zu kompliziert. vor 5 Jahren 0