c - 获取密码的隐窝是添加一些奇怪的 foobar 东西,所以它不等于

标签 c cs50

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <crypt.h>

int main(int argc, string argv[])
{
    if(argc > 2){ printf("too many arguments\n"); return 51; }
    if(argc < 2){ printf("too few arguments\n"); return 50; }
    //if(strlen(argv[1]) > 4){ printf("Password is greater than 4 characters\n"); return 52; }

    if(argc == 2) //make sure there are enough args
    {
        char hash_guess[] = "rofk";
        //long long counter = 0;

        //while(guess != argv[1]) //crypt(hash_guess, "50") != argv[1]) //while answer not correct
        //{
            for(int a = 65; a < 91; a++)
            {
                for(int b = 65; b < 91; b++)
                {
                    for(int c = 65; c < 91; c++)
                    {
                        for(int d = 65; d < 91; d++)
                        {
                            for(int A = 0; A < 9; A = A + 5) //control if first is caps or not
                            {
                                for(int B = 1 ; B < 9 ; B = B + 5)//control if second is caps or not
                                {
                                    for(int C = 2; C < 9; C = C + 5) //control if third is caps or not
                                    {
                                        for(int D = 3; D < 9; D = D + 5) //control if fourth is caps or not
                                        {
                                            hash_guess[0] = a;
                                            hash_guess[1] = b;
                                            hash_guess[2] = c;
                                            hash_guess[3] = d;
                                            hash_guess[A] = tolower(hash_guess[A]);
                                            hash_guess[B] = tolower(hash_guess[B]);
                                            hash_guess[C] = tolower(hash_guess[C]);
                                            hash_guess[D] = tolower(hash_guess[D]);
                                            printf("%s\n", hash_guess);

                                            string cryptoguess = (crypt(hash_guess, "50"));
                                            string input = argv[1];

                                            if( cryptoguess == input ) { return 0; }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            //}
        //}
        //string guess = crypt(hash_guess, "50");
        //printf("%lli", counter);
    }
    }
}

我正在尝试制作一个程序,它通过每 4 个字母的单词,从 aaaa 开始到 ZZZZ。我完成了那部分。

部分作业是对其进行加密,如果加密与加密密码相匹配,那么您就知道您“破解”了他们的密码。当我比较我手动输入的加密密码和使用 crypt 函数出现的密码时,它们是相同的,但是在调试器中我看到它被计算机加密时是这样的:

"0x7ffff7dd9200 <_ufc_foobar+131200> "50k72iioeOiJU""

和我输入的正常显示

"0x7fffffffe34f "50k72iioeOiJU""

没有 _ufc_foobar 也是一样。有谁知道为什么会出现这种情况以及我该如何摆脱它?

最佳答案

您看到的奇怪垃圾是代码中可以忽略的内存地址偏移量的可视化。

当 GNU 指定 char *crypt 函数的结果时,您在代码中使用 string

因此,您不能使用== 比较指向char 数组的指针,而是需要使用strcmp C comparing pointers (with chars)

对于地穴,请参阅:http://www.gnu.org/software/libc/manual/html_node/crypt.html

关于c - 获取密码的隐窝是添加一些奇怪的 foobar 东西,所以它不等于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44939677/

相关文章:

c - 多次包含头文件

c - 如何在 C 中使用 GUI?

c - 函数执行后结构体数组中的值丢失

c - 使用 char* 的段错误(核心转储)

c - 为什么 x[2] 也重新分配 y 值?我该如何解决这个问题

c++ - 在 C/C++ 中,反转字节中位顺序的最简单方法是什么?

c - 如何导入 C 库?

c - 指针还是地址?

c - 每当我在此代码中输入 4.2 时,nm 的值为 19,其中预期为 20

c - 嵌套多个 while 循环 - CS50greedy.c less