将ASCII密码破解程序升级为多个字符

时间:2015-02-15 06:14:00

标签: c++ string ascii cstring

我最近开始学习C ++,我决定创建一个基本的密码破解控制台程序。在其中,用户输入一个字符,程序试图通过从第32个ASCII字符开始并上升到第126个来猜测用户输入的字符,检查ASCII字符是否与用户匹配。 ; s性格。如果匹配,则告诉用户。

现在,我想将程序从字符升级为字符串。我希望用户输入一个字符串,然后让程序猜测它。但是,我不知道该怎么做。我曾尝试在线搜索答案,但无济于事。

提前致谢!

#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;
}

1 个答案:

答案 0 :(得分:-1)

假设您的密码限制为10个字符。然后,您可以单独检查每个字符,如果找到匹配项,则将其添加到另一个字符数组(字符串)。

#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;
}

但是为什么你可以这样做xP:

char mypass[10];
gets(mypass);
cout<<"Password is --"<<mypass<<endl;