在C++中自动嵌套回路
我刚刚开始使用C ++,目前我正在尝试构建一种破解密码的算法。我遇到了嵌套循环的问题,以获取更长的密码。目前,它适用于4个字符密码,我必须等待一段时间,但是它可以使用。是否有一种方法可以摆脱硬编码的嵌套循环,使该算法比对于角色而不必一遍又一遍地复制和粘贴if语句的算法更重要?
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
const char alphanum[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&* ";
const string pw = "0000";
bool cracked = false;
int tries = 0;
const int pwLength = pw.length();
string crack();
string guess="0";
const int maxLen = 30;
int main()
{
clock_t begin, end;
double time;
begin = clock();
string crackedPassword = crack();
end = clock();
time = double(end - begin) / (double)CLOCKS_PER_SEC;
cout << "The password is: " << crackedPassword << " The program checked a total of " << tries << " possibilites and took " << time << " seconds to crack the password!";
return 0;
}
string crack() {
int index = 0;
int repeat = 0;
while (!cracked) {
for (int i = 0; i < 71; i++) {
tries++;
guess[index] = alphanum[i];
cout << "Trying: " << guess << endl;
if (repeat > 0) {
for (int i = 0; i < 71; i++) {
tries++;
guess.push_back(alphanum[i]);
cout << "Trying: " << guess << endl;
if (repeat > 1) {
for (int i = 0; i < 71; i++) {
tries++;
guess.push_back(alphanum[i]);
cout << "Trying: " << guess << endl;
if (repeat > 2) {
for (int i = 0; i < 71; i++) {
tries++;
guess.push_back(alphanum[i]);
cout << "Trying: " << guess << endl;
if (guess == pw) {
cracked = true;
break;
}
else {
guess.pop_back();
}
}
}
if (guess == pw) {
cracked = true;
break;
}
else {
guess.pop_back();
}
}
}
if (guess == pw) {
cracked = true;
break;
}
else {
guess.pop_back();
}
}
}
if (guess == pw) {
cracked = true;
break;
}
}
repeat++;
}
return guess;
}
I just got started with C++ and I'm currently trying to build an algorith that cracks passwords. I ran into a problem with nested loops for longer passwords. It currently works fine for 4 character passwords, I have to wait for a while, but it works. Is there a way to get rid of the hard coded nested loops, to make the algorithm work for more than for characters without having to copy and paste the if statements over and over again?
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
const char alphanum[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&* ";
const string pw = "0000";
bool cracked = false;
int tries = 0;
const int pwLength = pw.length();
string crack();
string guess="0";
const int maxLen = 30;
int main()
{
clock_t begin, end;
double time;
begin = clock();
string crackedPassword = crack();
end = clock();
time = double(end - begin) / (double)CLOCKS_PER_SEC;
cout << "The password is: " << crackedPassword << " The program checked a total of " << tries << " possibilites and took " << time << " seconds to crack the password!";
return 0;
}
string crack() {
int index = 0;
int repeat = 0;
while (!cracked) {
for (int i = 0; i < 71; i++) {
tries++;
guess[index] = alphanum[i];
cout << "Trying: " << guess << endl;
if (repeat > 0) {
for (int i = 0; i < 71; i++) {
tries++;
guess.push_back(alphanum[i]);
cout << "Trying: " << guess << endl;
if (repeat > 1) {
for (int i = 0; i < 71; i++) {
tries++;
guess.push_back(alphanum[i]);
cout << "Trying: " << guess << endl;
if (repeat > 2) {
for (int i = 0; i < 71; i++) {
tries++;
guess.push_back(alphanum[i]);
cout << "Trying: " << guess << endl;
if (guess == pw) {
cracked = true;
break;
}
else {
guess.pop_back();
}
}
}
if (guess == pw) {
cracked = true;
break;
}
else {
guess.pop_back();
}
}
}
if (guess == pw) {
cracked = true;
break;
}
else {
guess.pop_back();
}
}
}
if (guess == pw) {
cracked = true;
break;
}
}
repeat++;
}
return guess;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论