Program to crack a 4 character password

Hey, I'm attempting to use the function CrackP to try all possible combinations of "AAAA" through "ZZZZ" until CrackP's output equals P. I'd like a nudge in the right direction. Here's my current code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

string GenChar(int length)
{
    string res;
    for (int i = 0; i < length; i++)
    {
        res += 'A' + (rand() % 26);
    }
    return res;
}
void CrackP(string P) {
    string crackAttempt = GenChar(4);
    cout << "Target: " + P << endl;
    cout << "Attempting to crack...";

    while (crackAttempt != P) {
        crackAttempt = GenChar(4);

    }

    if (crackAttempt == P) {
        cout << "Cracked " + crackAttempt;
    }
    

}
int main()
{
    string P;
    srand((unsigned)time(nullptr));
    P = GenChar(4);
    CrackP(P);
}



What should I do from here?
Last edited on
This will give you all combination from "AA" to "ZZ".
Maybe you can adapt it to your needs.
1
2
3
4
5
6
7
8
9
10
  string s("  ");

  for (char c1 = 'A'; c1 <= 'Z'; c1++)
    for (char c2 = 'A'; c2 <= 'Z'; c2++)
    {
      s[0] = c1;
      s[1] = c2;
      cout << s << "\n";
    }
Thomas, that's exactly what I needed. I've adapted it to the following:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        for (char c1 = 'A'; c1 <= 'Z'; c1++)
            for (char c2 = 'A'; c2 <= 'Z'; c2++)
                for (char c3 = 'A'; c3 <= 'Z'; c3++)
                    for (char c4 = 'A'; c4 <= 'Z'; c4++) {
                        s[0] = c1;
                        s[1] = c2;
                        s[2] = c3;
                        s[3] = c4;

                        cout << s << endl;


                    }


    }


My question is this: how can I run the for loops only until s = P?

For example, if P is AAAZ, I want to stop the execution of the for loops. I also want to print the number of iterations or attempts it took to "crack" the password.

I tried enclosing the for loops in

while ( s != P ), but that doesn't work because s is an empty string until later on in the loop.

Any suggestions?
Simply return from the function when you find a match.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void CrackP (const string & P) 
{   string s; 
    int attempts = 0;
    
    for (char c1 = 'A'; c1 <= 'Z'; c1++)
        for (char c2 = 'A'; c2 <= 'Z'; c2++)
            for (char c3 = 'A'; c3 <= 'Z'; c3++)
                for (char c4 = 'A'; c4 <= 'Z'; c4++) 
                {   s[0] = c1;
                    s[1] = c2;
                    s[2] = c3;
                    s[3] = c4;
                    attempts++;
                    if (s == P) 
                    {   cout << "Cracked " + crackAttempt;
                        cout << " in " << attempts << " attempts " << endl;
                        return;
                    }
                }
}


BTW, there is a much more efficient way to do this by checking one character at a time. i.e. Don't enter the second loop until you've found a match on the first loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void CrackP (const string & p) 
{   for (char c1 = 'A'; c1 <= 'Z'; c1++)
        if (p[0] == c1)
            for (char c2 = 'A'; c2 <= 'Z'; c2++)
                if (p[1] == c2)
                    for (char c3 = 'A'; c3 <= 'Z'; c3++)
                        if (p[2] == c3)
                            for (char c4 = 'A'; c4 <= 'Z'; c4++) 
                                if (p[3] == c4)
                                {   cout << "Cracked " << p << endl;                                    
                                    return;
                                }
                
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;


const string ALPHABET = "ABC";                             // For testing
// const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";   // For real
const int CHOICES = ALPHABET.size();


//======================================================================


void nextWord( vector<int> &Iword )              // The next word (as defined by character indices)
{
   for ( int i = 0; i < Iword.size(); i++ )
   {
      if ( Iword[i] < CHOICES - 1 )              // If can increment this position, do so and return
      {
         Iword[i]++;
         return;
      }
      else                                       // Otherwise cycle back and continue to next position
      {
         Iword[i] = 0;
      }
   }
}


//======================================================================


string word( vector<int> &Iword )
{
   string result = "";
   for ( int i : Iword ) result += ALPHABET[i];
   return result;
}


//======================================================================


int main()
{
   int N = 4;                                    // Intended length of password
   vector<int> Iword( N, 0 );
   int MAXWORDS = pow( CHOICES, N );

   cout << word( Iword ) << endl;

   for ( int w = 2; w <= MAXWORDS; w++ )
   {
      nextWord( Iword );
      cout << word( Iword ) << endl;
   }
}


//======================================================================
AAAA
BAAA
CAAA
ABAA
BBAA
CBAA
ACAA
BCAA
CCAA
AABA
BABA
CABA
ABBA
BBBA
CBBA
ACBA
BCBA
CCBA
AACA
BACA
CACA
ABCA
BBCA
CBCA
ACCA
BCCA
CCCA
AAAB
BAAB
CAAB
ABAB
BBAB
CBAB
ACAB
BCAB
CCAB
AABB
BABB
CABB
ABBB
BBBB
CBBB
ACBB
BCBB
CCBB
AACB
BACB
CACB
ABCB
BBCB
CBCB
ACCB
BCCB
CCCB
AAAC
BAAC
CAAC
ABAC
BBAC
CBAC
ACAC
BCAC
CCAC
AABC
BABC
CABC
ABBC
BBBC
CBBC
ACBC
BCBC
CCBC
AACC
BACC
CACC
ABCC
BBCC
CBCC
ACCC
BCCC
CCCC
Last edited on
Topic archived. No new replies allowed.