C ++:我怎么做没有复制和粘贴?它循环自身 [英] C++ : How can I do it without copy&paste? It loops itself

查看:132
本文介绍了C ++:我怎么做没有复制和粘贴?它循环自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,我试图做一些暴力破解密码。

I'm new at C++, I am trying to make some brute force password cracking.

此时,它会从aaaaa循环到zzzzz。

At the moment, it loops from aaaaa to zzzzz.

问题:从a到zzzzz?

question : Is it possible to make it like from a to zzzzz?

我正在努力工作6个小时,但仍然找不到解决方案。

I am working on this for like non-stop 6 hours and still could't find a solution.

等待您的帮助,谢谢!

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string password;
    string generatedPass;
    int x;

    cin >> password;

    char digitArray[]={96,97,97,97,97,97,97,97,97,97};
    int lenght=0;
    int maxLenght=password.length();


    while( password.compare(generatedPass) != 0 ){
        digitArray[0]++;
        for(int x=0;x<password.length();x++){
           if (digitArray[x] == 123)
                {
                    digitArray[x] = 97;
                    digitArray[x + 1]++;
                }
        }

        // creating the string with those digits
        generatedPass=digitArray[password.length()-1]; // i set the first one here

        for(int      i=password.length()-2   ;  i>=0  ;  i-- )
            generatedPass+= digitArray[i]; // now i add other digits to next to first digit

            cout << generatedPass << endl; // i put it to test how it looks
    }

    cout << generatedPass << endl; // gives the result
    return 0;
}


推荐答案

这里:

    if(digitArray[0]==123){     // i designed it like stopwatch, if like when second is 60, you make it 0 and increment minute by 1
        digitArray[0]=97;       // 97 is the character 'a' in ASCII table, 122 is the 'z'
        digitArray[1]++;
    }

    // HELP HERE, PLEASE!
    //
    // my program loops itself after this, copy paste doesn't look cool. how can i improve this part? that 4 in the digitArray[4] is maximum passwordlenght
    // im working on this like nonstop 6 hours and still couldnt find a solution...

    if(digitArray[1]==123){
        digitArray[1]=97;
        digitArray[2]++;
    }

    if(digitArray[2]==123){
        digitArray[2]=97;
        digitArray[3]++;
    }

模式的通用形式是:

if (digitArray[x] == 123)
{
    digitArray[x] = 97;
    digitArray[x + 1]++;
}

除了最后一种情况。我会让你知道如何加入 for 循环。

Except for the last case. I'll let you figure out how to put in into a for loop.

编辑1:创建从a到zzzzzz的密码

这很像是在26号或27号计数(取决于是否计算空格)。

Edit 1: creating passwords from 'a' to 'zzzzzz'
This is much like counting in base 26 or base 27 (depending on if you count spaces).

让我们考虑添加。假设字母'a'到'z'是连续的。

Let's consider adding. Assuming that the letters 'a' to 'z' are contiguous.

std::fill(&digitArray[0], &digitArray[sizeof(digitArray)], ' '); // Initialize
unsigned int last_digit_index = sizeof(digitArray) - 1U;
if (digitArray[last_digit_index] == ' ')
{
  digitArray[last_digit_index] = 'a';
else
{
  ++digitArray[last_digit_index];
}

这里有一个问题,那就是处理溢出, > carry 。

There is a problem here and that is handling overflow, otherwise known as carry.

对于溢出,数字需要重置为'',下一个数字需要递增。

For overflow, the digit needs to be reset to ' ', and the next (or previous) digit needs to be incremented.

上述代码负责增加值,因此只需要更改索引。

The above code takes care of incrementing the value, so only the index needs to be changed.

更改索引和循环(或递归)将留作OP的练习。

Changing of the index and looping (or recursing) is left as an exercise for the OP.

这篇关于C ++:我怎么做没有复制和粘贴?它循环自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆