3

As an assignment, I have to find a password of a user starting from the hash of that password (which was made using 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. 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). 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

Where do these extra characters (= �g) come from?

Thank you

Maxim
  • 31
  • 1

1 Answers1

1

You're attempting to print salt as a string but you don't actually have a string. You have an array of two characters. A string is terminated by a null byte. 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.

You need to add space in the array for the null terminator:

char salt[3];
salt[2] = 0;
dbush
  • 205,898
  • 23
  • 218
  • 273