Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;暴力攻击函数不返回结果 我现在正在C++中研究一个蛮力攻击者项目。我已经设法让它工作了,但我面临的一个问题是,如果程序确实设法得到了正确的猜测,那么函数仍然会继续。我认为问题在于程序无法返回猜测。看看我的代码: (抱歉,混乱,顺便说一下,我不是C++中有经验的,我以前在Python/JS中编码) #包括 #包括 #包括 std::string chars=“abcdefghijklmnopqrstuvxyz”; std::string iterateStr(std::string s,std::string guess,int pos); 标准:管柱裂纹(标准:管柱s); std::string iterateChar(std::string s,std::string guess,int-pos); int main(){ 裂纹(“bb”); 返回退出成功; } //此函数遍历字母表中的字母 std::string iterateChar(std::string s,std::string guess,int pos){ 对于(int i=0;i_C++_For Loop_Recursion_Brute Force - Fatal编程技术网

C++;暴力攻击函数不返回结果 我现在正在C++中研究一个蛮力攻击者项目。我已经设法让它工作了,但我面临的一个问题是,如果程序确实设法得到了正确的猜测,那么函数仍然会继续。我认为问题在于程序无法返回猜测。看看我的代码: (抱歉,混乱,顺便说一下,我不是C++中有经验的,我以前在Python/JS中编码) #包括 #包括 #包括 std::string chars=“abcdefghijklmnopqrstuvxyz”; std::string iterateStr(std::string s,std::string guess,int pos); 标准:管柱裂纹(标准:管柱s); std::string iterateChar(std::string s,std::string guess,int-pos); int main(){ 裂纹(“bb”); 返回退出成功; } //此函数遍历字母表中的字母 std::string iterateChar(std::string s,std::string guess,int pos){ 对于(int i=0;i

C++;暴力攻击函数不返回结果 我现在正在C++中研究一个蛮力攻击者项目。我已经设法让它工作了,但我面临的一个问题是,如果程序确实设法得到了正确的猜测,那么函数仍然会继续。我认为问题在于程序无法返回猜测。看看我的代码: (抱歉,混乱,顺便说一下,我不是C++中有经验的,我以前在Python/JS中编码) #包括 #包括 #包括 std::string chars=“abcdefghijklmnopqrstuvxyz”; std::string iterateStr(std::string s,std::string guess,int pos); 标准:管柱裂纹(标准:管柱s); std::string iterateChar(std::string s,std::string guess,int-pos); int main(){ 裂纹(“bb”); 返回退出成功; } //此函数遍历字母表中的字母 std::string iterateChar(std::string s,std::string guess,int pos){ 对于(int i=0;i,c++,for-loop,recursion,brute-force,C++,For Loop,Recursion,Brute Force,我已经尝试了你的代码,甚至使用了你的iterateStr()函数的修改版本。我使用了诱拐这个词,因为它可以更快地搜索。当逐步通过调试器时,我注意到你的iterateChar()函数在找到匹配项时没有返回。我还注意到传入的字符串s的长度是6,但是在每次迭代中更新的猜测字符串的长度是7。您可能需要逐步检查代码并检查此项 例如,在特定的迭代中,s字符串包含:诱拐,但guess字符串包含aaaabjz,然后在下一次迭代中,guess字符串包含aaaaaabkz。这可能是您关心的问题,为什么即使您认为匹配

我已经尝试了你的代码,甚至使用了你的
iterateStr()
函数的修改版本。我使用了
诱拐
这个词,因为它可以更快地搜索。当逐步通过调试器时,我注意到你的
iterateChar()
函数在找到匹配项时没有返回。我还注意到传入的
字符串s
的长度是
6
,但是在每次迭代中更新的猜测字符串的长度是
7
。您可能需要逐步检查代码并检查此项

例如,在特定的迭代中,
s
字符串包含:
诱拐
,但
guess
字符串包含
aaaabjz
,然后在下一次迭代中,
guess
字符串包含
aaaaaabkz
。这可能是您关心的问题,为什么即使您认为匹配,循环或函数仍会继续找到了

这里的长度差异可能是你的罪魁祸首

此外,在单步执行修改后的代码时:

for ( size_t i = 0; i < s.length(); i++ ) {
    guess = iterCh( s, guess, i );
    std::cout << "in the iterStr loop\n";
    if ( guess.compare( s ) == 0 ) {
        return guess;
    }
}
return guess;
for(size_t i=0;istd::cout发布的代码中的主要缺陷是递归函数返回一个字符串(猜测的密码),而没有明确指示调用方找到密码

按值传递所有字符串也是一个潜在的效率问题,但OP应该担心这样的代码片段:

guess[pos] = chars[i];  // 'chars' contains the alphabet

if(pos == s.length()) {
    if(guess.compare(s) == 0) {
        break;
    }
} 
其中,
guess
s
是长度相同的字符串。如果长度为2(OP的最后一个示例),
guess[2]
超出了范围,但是对
guess.compare(s)
的连续调用将只比较“内”两个字符

iterateStr
中的循环也没有任何用处,而且
pos
参数未使用

与其修复此尝试,不如从头重写它

#include <iostream>
#include <string>
#include <utility>

// Sets up the variable and start the brute force search
template <class Predicate>
auto crack(std::string const &src, size_t length, Predicate is_correct)
    -> std::pair<bool, std::string>;

// Implements the brute force search in a single recursive function. It uses a
// lambda to check the password, instead of passing it directly
template <class Predicate>
bool recursive_search(std::string const &src, std::string &guess, size_t pos,
                      Predicate is_correct);

// Helper function, for testing purpouse
void test_cracker(std::string const &alphabet, std::string const &password);

int main()
{
    test_cracker("abcdefghijklmnopqrstuvwxyz", "dance");
    test_cracker("abcdefghijklmnopqrstuvwxyz ", "go on");
    test_cracker("0123456789", "42");
    test_cracker("0123456789", "one");     // <- 'Password not found.'
}

void test_cracker(std::string const &alphabet, std::string const &password)
{
    auto [found, pwd] = crack(alphabet, password.length(),
        [&password] (std::string const &guess) { return guess == password; });

    std::cout << (found ? pwd : "Password not found.") << '\n';
}

// Brute force recursive search 
template <class Predicate>
bool recursive_search(std::string const &src, std::string &guess, size_t pos,
                      Predicate is_correct)
{
    if ( pos + 1 == guess.size() )
    {
        for (auto const ch : src)
        {
            guess[pos] = ch;

            if ( is_correct(guess) )
                return true;
        }     
    }
    else
    {
        for (auto const ch : src)
        {
            guess[pos] = ch;

            if ( recursive_search(src, guess, pos + 1, is_correct) )
                return true;
        }              
    }
    return false;
}

template <class Predicate>
auto crack(std::string const &src, size_t length, Predicate is_correct)
    -> std::pair<bool, std::string>
{
    if ( src.empty() )
        return { length == 0 && is_correct(src), src };

    std::string guess(length, src[0]);

    return { recursive_search(src, guess, 0, is_correct), guess };
}
#包括
#包括
#包括
//设置变量并启动蛮力搜索
模板
自动破解(std::string const&src,size\u t length,谓词正确)
->std::pair;
//在单个递归函数中实现暴力搜索
//lambda检查密码,而不是直接传递密码
模板
布尔递归搜索(std::string const&src,std::string&guess,size\t pos,
谓词是正确的);
//辅助函数,用于测试purpouse
无效测试破解程序(标准::字符串常量和字母表,标准::字符串常量和密码);
int main()
{
测试饼干(“舞蹈”);
测试饼干(“继续”);
测试饼干(“0123456789”、“42”);

测试饼干(“0123456789”,“一个”)//想一想:你怎么知道
iterateChar
返回是因为它找到了它要找的东西,还是因为它猜不透而失败了?一旦你弄明白了,然后用它来确定递归调用是否需要继续循环或停止。@1201programalm,嘿,谢谢你的回答-我已经做了一些测试和测试修正了一点代码-你能检查我下面的答案吗?我已经尝试了你的代码,我使用了
诱拐
这个词,因为它不会花那么长的时间,当输出在
诱拐
时,它会一直持续下去!当找到匹配项时,代码不会中断。我尝试了我的代码,我没有发现猜测字符串比它长的问题n string s。问题似乎是,iterateChar函数正在整个字符串中运行,即使我有一个return语句。return语句应该可以工作,因为我总是设法打印“Aha!”,但它不工作。
#include <iostream>
#include <string>
#include <utility>

// Sets up the variable and start the brute force search
template <class Predicate>
auto crack(std::string const &src, size_t length, Predicate is_correct)
    -> std::pair<bool, std::string>;

// Implements the brute force search in a single recursive function. It uses a
// lambda to check the password, instead of passing it directly
template <class Predicate>
bool recursive_search(std::string const &src, std::string &guess, size_t pos,
                      Predicate is_correct);

// Helper function, for testing purpouse
void test_cracker(std::string const &alphabet, std::string const &password);

int main()
{
    test_cracker("abcdefghijklmnopqrstuvwxyz", "dance");
    test_cracker("abcdefghijklmnopqrstuvwxyz ", "go on");
    test_cracker("0123456789", "42");
    test_cracker("0123456789", "one");     // <- 'Password not found.'
}

void test_cracker(std::string const &alphabet, std::string const &password)
{
    auto [found, pwd] = crack(alphabet, password.length(),
        [&password] (std::string const &guess) { return guess == password; });

    std::cout << (found ? pwd : "Password not found.") << '\n';
}

// Brute force recursive search 
template <class Predicate>
bool recursive_search(std::string const &src, std::string &guess, size_t pos,
                      Predicate is_correct)
{
    if ( pos + 1 == guess.size() )
    {
        for (auto const ch : src)
        {
            guess[pos] = ch;

            if ( is_correct(guess) )
                return true;
        }     
    }
    else
    {
        for (auto const ch : src)
        {
            guess[pos] = ch;

            if ( recursive_search(src, guess, pos + 1, is_correct) )
                return true;
        }              
    }
    return false;
}

template <class Predicate>
auto crack(std::string const &src, size_t length, Predicate is_correct)
    -> std::pair<bool, std::string>
{
    if ( src.empty() )
        return { length == 0 && is_correct(src), src };

    std::string guess(length, src[0]);

    return { recursive_search(src, guess, 0, is_correct), guess };
}