c - 多线程暴力破解算法

标签 c multithreading algorithm brute-force

我有一个多线程程序,它通过递增字符串、采用字符串分隔的字符来暴力破解密码。

我以为我可以在线程之间分割“字典”(字符集),但我显然错了。

我正在做的是将字典分成与线程一样多的部分,然后让它们处理它们的字符子集。

这是我的字典:

static const char tab[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789"
"!*~";

所以基本上我在做一些愚蠢的事情,因为如果我将它分成 4 个线程,那么就可以测试“adef”,但永远不能测试“a*1b”。

所以我想有一个更好的解决方案,但是我无法真正理解它。

这是我的递增算法(还有更多内容,但我只列出重要的内容):

void *func(void * bound){

 char Test[5]="0000";

 int u = bound + nb_pas;
 for (int h = bound; h <= u; ++h){
    Test[3] = tab[h];
    for (int k = bound; k <= u; ++k){
        Test[2] = tab[k];
        for (int j = bound; j <= u; ++j){
            Test[1] = tab[j];
            for (int i = bound; i <= u; ++i){
                Test[0] = tab[i];
                if(finished == 1){
                    for(int r = 1 ; r < nb_thread; r++ ){                            
                        pthread_cancel(tid[r]);                            
                    }
                    return EXIT_SUCCESS;
                }

                char *hash = crypt_r(Test,salt,cdata);

                if(strcmp(hash,ciphertext) == 0 ){


                    // Impression du résultat
                    printf("Password found: %s\n", Test);
                    printf("Hashed version of password is %s\n", hash);
                    printf("It took %f seconds to complete in %i steps \n", elapsed, compteur);
                    finished = 1;
                    return EXIT_SUCCESS;
                }

            }
        }
    }
}

if (finished == 0){
    printf(" NO MATCH\n");
}
 return 0;
}

其中 bound 是传递给与 (# of chars/# ofthreads) * 线程索引 对应的线程的 int >nb_pas(字符数/线程数)

我应该如何处理这个问题?我考虑过在将函数分配给线程之前模拟循环递增,但我不知道该怎么做......

感谢您的帮助

编辑

这是我生成线程的方法

for(int i = 0; i < nb_thread; i++){
        int b = nb_pas*i;
        if (pthread_create(&tid[i],NULL,func, b)!=0){ 
            printf("Une erreur s'est produite");
            return EXIT_FAILURE;
        }

    }

最佳答案

你可以尝试类似的方法

for (i=0; i<nbthread; i++) {
    pthread_create(......,func,i*strlen(tab)/nbthread);
}

然后:

func(void *bound) {
    int start = (int)bound;
    int end = start+strlen(tab)/nbthread;
    for (i=start; i<end];i++) { // just one slice of the alphabet
       Test[0] = tab[i];
       for(j=0; j<strlen(tab); j++) { // every letter
           Test[1] = tab[j];
              ....

}

关于c - 多线程暴力破解算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26363535/

相关文章:

c# - 如何处理 SystemEvents.OnUserPreferenceChanged 中的 C# 挂起

java - 想要并行运行非线程安全库 - 可以使用多个类加载器来完成吗?

.net - 如何分别渲染用户控件

python - 为一个范围组合系数?

c - 寻找回文

c - C中不同宏函数/内联方法的优缺点

javascript - 高级排序算法

Java颜色检测

c - 是否可以在 GCC/Clang 上强制进行尾调用优化?

c - 输入后程序转到空白终端(C 中的异或加密)