0

I am having trouble with all the different types: strings, char, char * while appending, adding, concatenating variables that end up not compatible since they are different types.

I am trying to run all possible 3 character combinations for ascii characters 48 through 127. However, i get an annoying "+C" term at the end of my 3 character combinations. How do I get rid of that. Thank you so much in advance~~

(also just a heads up I wanted to see all possible combos so i have a print statement inside the nested for loop that shows me all the possible combos that run to ensure it got all of them)

int main(int argc, char * argv[])
{
    char * hash =  argv[1];
    char guess_3[3];

// too many too little arguements
if (argc != 2)
{
    printf("Usage: %s hash\n", argv[0]);
    return 1;
}

char salt[2];
//get salt
for (int i = 0; i < 2; i++)
{
    salt[i] = hash[i];
}

//check for three characters
for (int i = 48; i < 127; i++)
{
    char ASCII_i = i;

    for (int j = 48; j < 127; j++)
    {
        char ASCII_j = j;

        for (int k = 48; k < 127; k++)
        {
            char ASCII_k = k;
            guess_3[0] = ASCII_i;
            guess_3[1] = ASCII_j;
            guess_3[2] = ASCII_k;
            printf("%s\n", guess_3);
            char * check = crypt(guess_3, salt);
            if (check == hash)
            {
                printf("%s\n", guess_3);
                return 0;
            }
        }
    }
}
}

1 Answer 1

0

Strings (i.e., char arrays) must end with an end of string(EOS) marker, \0. Many functions, starting with printf() depend on the existence of an EOS marker to function correctly. The way printf() works is to start printing at the start of the string and continues until an EOS marker is encountered, regardless of the size of the array. If the string isn't properly terminated with an EOS marker, printf() will keep reading from memory and trying to print whatever it encounters until it either sees an EOF (a binary 0 byte) or goes beyond the allocated memory for the program (which will likely generate a segmentation fault).

Now, lets look at the code above. char guess_3[3]; allocates space for 3 chars. That space has to leave room for the EOS marker. Unfortunately, the code is using the 3 spaces for active chars in the string. There's no EOS marker, so when printf() is called, it's going to keep going beyond the end of guess_3 and try to print whatever data is there as chars. When it finally finds a binary 0 byte, it stops. This is why you're getting extra data printed along with the string.

There may be other issues, but this has to be fixed first.

As a side note, you should become familiar with functions like isalpha, isupper, islower, isnum, and all their cousins! Time to do a little research.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .