我最近开始学习C++,并决定创建一个基本的密码破解控制台程序。在它中,用户输入一个字符,程序试图猜测用户输入的字符,从第32个ASCII字符开始,一直到第126个,沿途检查ASCII字符是否与用户的字符匹配。如果匹配,它会告诉用户。
现在,我想把我的程序从一个字符升级为一个字符串。我想让用户输入一个字符串,然后让程序猜测。然而,我不知道如何做到这一点。我试着在网上搜索答案,但没有成功。
提前感谢!
#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 %dn", C1);
if (C1 == Password) {
cout << "The password is " << C1 << "." << endl;
break;
}
}
system("PAUSE");
return 0;
}
假设您的密码限制为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--]=' ';//terminate the array
cout<<"Password is - "<<thepass ;
return 0;
}
但是,既然你可以做到xP:,为什么还要这样做呢
char mypass[10];
gets(mypass);
cout<<"Password is --"<<mypass<<endl;