PSET2 / Crack - Char Variable [4]输出50+个字符

时间:2017-11-10 14:55:51

标签: c cs50

int main(int argc, char *argv[])
{
char alphabet[52], check[4];

//checks to see there is exactly 2 command-line inputs

if (argc != 2 )
{
    printf("Please enter exactly two command-line inputs");
    return 1;
}

string pw = argv[1];

printf("%s\n", pw);
printf("%s\n", crypt(pw,"50"));



//sets alphabet array to letters of the alphabet
alphabet[0] = ' ';
for (int alpha = 1; alpha <= 26; alpha++)
{
    alphabet[alpha] = (alpha + 64);
}

for (int alpha = 27; alpha <= 52; alpha++)
{
    alphabet[alpha] = alpha + 70;
}


// variable to determine the number of alphabet starting with A the code will try decode
int count = 2;

for (int i = 1; i <= count; i++)
{
    //resets every other character to ' '
    for (int x = 1; x <=4; x++)
    {
           check[x] = ' ';
    }

    //changes the 1st letter of check to the i'th letter of the alphabet
    check[0] = alphabet[i];
    printf("%s\n", check);
    printf("Strcmp is %i\n", strncmp(pw,check,strlen(pw)));

    if (strncmp(pw,check,strlen(pw)) == 0)
    {
        printf("Password is %s\n", check);
        return 0;
    }

    for (int j = 0; j <= count; j++)
    {
        check[1] = alphabet[j];

        printf("%s\n", check);
        printf("Strcmp is %i\n", strncmp(pw,check,2));
    }
}

因此代码当前创建了一个名为alphabet的53个字符数组,它创建了一个空白'',然后是A - z。

然后我尝试做的是创建嵌套循环来创建一个4位代码(变量:check [4]),代码的每个字母都是从字母数组中的字符中提取的。

我已经打印检查以查看存储在变量中的内容 - 在debug50中它似乎存储了正确的字符但是当我运行代码时,./crack BA的输出变为

A    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 1
A    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 1
AA   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 1
AB   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 1
B    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 33
B    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 33
BA   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 0
BB   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 

我不明白为什么当我尝试输出检查它有正确的输出(“A”,“AB”......等)后跟整个pw变量(A-z)

提前感谢您的帮助

亚历

0 个答案:

没有答案