简体   繁体   English

如何在 C 的地穴 function 中使用可变盐?

[英]How can I make a variable salt to be used in the crypt function in C?

As an assignment, I have to find a password of a user starting from the hash of that password (which was made using crypt).作为一项任务,我必须找到从该密码的 hash 开始的用户密码(使用 crypt 制作)。 So, I'm trying to create a variable salt (2-letter string) which I then tend to use in the crypt function until the result matches the hash.因此,我正在尝试创建一个变量 salt(2 字母字符串),然后我倾向于在 crypt function 中使用它,直到结果与 hash 匹配。 However, when I try to make this variable salt, instead of two characters, I get 5/6 (even if I define the salt as an array of size 2).但是,当我尝试制作这个变量 salt,而不是两个字符时,我得到 5/6(即使我将 salt 定义为大小为 2 的数组)。 Does anyone know how this can be fixed?有谁知道如何解决这个问题? Difficult explanation, I know, but see code (and result) below.我知道很难解释,但请参阅下面的代码(和结果)。

char salt[2];
for (int i = 65; i < 91; i++)
{
    salt[0] = i;
    for (int j = 65; j < 91; j++)
    {
        salt[1] = j;
        printf("%s\n", salt);
    }
}

Outcome: AA �g AB �g AC �g AD �g AE �g AF �g... ... ... ZW �g ZX �g ZY �g ZZ �g结果:AA →g AB →g AC →g AD →g AE →g AF →g... ... ... ZW →g ZX →g ZY →g ZZ →g

Where do these extra characters (= �g) come from?这些额外的字符 (= �g) 是从哪里来的?

Thank you谢谢

You're attempting to print salt as a string but you don't actually have a string.您正在尝试将salt打印为字符串,但实际上并没有字符串。 You have an array of two characters.您有一个包含两个字符的数组。 A string is terminated by a null byte.字符串由 null 字节终止。 By passing this array to printf , it attempts to read past the end of the array looking for a byte with value 0. Reading past the end of an array invokes undefined behavior which in this case manifests as printing extra characters.通过将此数组传递给printf ,它会尝试读取数组末尾以查找值为 0 的字节。读取数组末尾会调用未定义的行为,在这种情况下表现为打印额外字符。

You need to add space in the array for the null terminator:您需要在数组中为 null 终结器添加空间:

char salt[3];
salt[2] = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM