在C++中自动嵌套回路

发布于 2025-02-04 13:40:52 字数 3166 浏览 2 评论 0原文

我刚刚开始使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文