-3

I have recently started learning C++, and I have decided to create a basic password-cracking console program. In it, the user inputs a character, and the program tries to guess what character the user entered by starting at the 32nd ASCII character and going up to the 126th, checking along the way to see if the ASCII character matches with the user's character. If it matches, it tells the user.

Now, I want to upgrade my program from a character to a string. I want the user to input a string, and then for the program to guess it. However, I have no idea how to do this. I have tried searching for an answer online, but to no avail.

Thanks in advance!

#include <iostream>


int main()
{
    using namespace std;
    char Password;
    char C1;
    cout << "Input a character to crack." << endl;
    cin >> Password;
    for (C1 = 32; C1 < 127; (int)C1++) {
        printf("Trying %d\n", C1);
        if (C1 == Password) {
            cout << "The password is " << C1 << "." << endl;
            break;
        }       
    }
    system("PAUSE");
    return 0;
}
Eugene K
  • 3,381
  • 2
  • 23
  • 36
  • 1
    It looks like you want to loop over all possible strings. Is there a length limitation? Regardless, your question has nothing to do with passwords. – Martin G Feb 15 '15 at 06:22

1 Answers1

-1

Lets say your password is limited to 10 characters. Then you can check each character individually and add it to another array of characters(string) if a match is found.

#include <iostream>
#include <stdio>
int checkCH(char a , char b)//functions checks equality among characters
{
    int k = 0;
   if (a==b)
    k=1;
   return k;
}
int main()
{
    char pass[10] , chkr[10] , thepass[10];
   int x;
   cout<<"Enter Password:";gets(pass);//in case password has a space
   for (x = 0 ; x<10 ; x++)
    {
    cout<<endl;
    cout<<"Checking possible match for character number"<<x<<endl;
      cout<<endl;
        for (int i = 65 ; i<127 ; i++)
      {
        chkr[x]=char(i);
         cout<<"      Searching--"<<chkr[x]<<endl;//shows user what the program is searching for
         if (checkCH(pass[x],chkr[x]))//check if randomly generate character matches a character in the password
            thepass[x]=chkr[x];
      }
   }
   thepass[x--]='\0';//terminate the array
   cout<<"Password is - "<<thepass ;
   return 0;
}

But why do that when you can just do this xP:

char mypass[10];
gets(mypass);
cout<<"Password is --"<<mypass<<endl;
m0bi5
  • 8,900
  • 7
  • 33
  • 44