在c中使用等于运算符

时间:2017-10-12 13:23:59

标签: c string equality

我的代码中有两个变量,我想互相检查,但每当我这样做时,if语句被认为是错误的,其中一个是char输入[],从argv [1]中提取并输入到函数和另一个是crypt(pass,salt)的结果

permute_axes()

当前输出(假设50k72iioeOiJU是输入)是

static const char alphabet1[] =
        "a";


void mycrack(char input[])
{
    char pass[5];
    pass[4] = '\0';
    char salt[3];
    salt[0] = input[0];
    salt[1] = input[1];
    salt [2] = '\0';

    for (int a = 0, size = sizeof(alphabet1); a < size - 1; ++a) {
        for (int b = 0, size = sizeof(alphabet1); b < size - 1; ++b) {
            for (int c = 0, size = sizeof(alphabet1); c < size - 1; ++c) {
                for (int d = 0, size = sizeof(alphabet1); d < size - 1; ++d) {

                    pass[0] = alphabet1[a];
                    pass[1] = alphabet1[b];
                    pass[2] = alphabet1[c];
                    pass[3] = alphabet1[d];
                    char* hash = crypt(pass,salt);
                    printf("pass:%s        salt:%s         hash:%s         input:%s\n",pass,salt,hash,input);
                    if(hash == input){
                        printf("you got it %s",pass);
                        exit(0);
                    }    
                }
            }
        }
    }
}
int main(int argc, string argv[1])
{
    mycrack(argv[1]);

    return 0;

同时,由于哈希和输入相等,预期输出将是

pass:aaaa        salt:50         hash:50k72iioeOiJU         input:50k72iioeOiJU

2 个答案:

答案 0 :(得分:1)

hashinputchar* s - 当您在它们之间使用==运算符时,您实际上检查两个指针​​指向相同的内存地址,他们显然不会。相反,您应该比较他们指向的字符串的内容。假设这些是标准的,以null结尾的字符串,您可以使用strcmp

if (strcmp(hash, input) == 0) {
    printf("you got it %s", pass);
    exit(0);
}

答案 1 :(得分:0)

使用strcmp比较两个字符串。

if(strcmp(hash,input)==0)

https://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm