0

Help, I think I'm going nuts. For crack program, I made a nested loop that iterates through my dictionary words array (I realize that's not strictly necessary for the assignment, but I added it anyway so I can feel like a fancy hacker...). It iterates through all the words, hashes each possible capitalization pattern of the word, and checks it. But when I put a printf() in the inner loop (just to test it) it prints each word twice, and I suspect it's to do with my capsPattern() function. Here is the code (it's a bit long, but the most pertinent part is the "Dictionary attack" loop):

#include <stdio.h>
#include <cs50.h>
#include <crypt.h>
#include <string.h>
#include <ctype.h>

string capsPattern (char word[], char pattern[]);
unsigned dec_to_bin (unsigned integer);

    int main(int argc, string argv[])
    {
        string input = argv[1];

        //some words for dictionary attack
        char words[][5] = {"john", "lisa", "mary", "bob", "tom", "dog", "lol"};

        //get the # of words in word array
        int dic_size = sizeof(words)/sizeof(words[0]);

        //"Dictionary attack" loop. 
        char pattern[32];
        for (int i = 0; i < dic_size; i++)
        {
            string unhashed = words[i];
            //inner loop iterates through all possible capitalization patterns             
            for (int j = 0; j < 16; j++)
            {
                //converts binary int returned by dec_to_bin into a string acceptable by capsPattern function
                sprintf(pattern, "%04d", dec_to_bin(j));

                unhashed = capsPattern(unhashed, pattern);
                string hashed = crypt(unhashed, "50");
                int match = strcmp(hashed, input);
                if (match == 0)
                {
                    printf("%s\n", unhashed);
                    return 0;
                    break;
                }
                printf("%s\n", unhashed); // This line is here just to test the program, but it prints everything twice!! Something wrong!
            }    
        }

        printf ("Dictionary attack failed. Moving on to brute force approach.\n");        
    }
}

//This function capitalizes a word in whatever pattern you pass to it (as a    binary numeric string).
//So passing "1000" will capitalize 1st letter and make next 3 lower-case (if they aren't already).

string capsPattern (char word[], char pattern[])
{
    string capWord = word;
    for (int i = 0; i < 5; i++)
    {
        capWord[i] = word[i];
    }
    //capitalizes appropriate letters
    for (int i = 0; (pattern[i] != '\0') && (capWord[i] != '\0'); i++)
    {
        if (pattern[i] == '0') 
        {
            if ((capWord[i] >= 'A') && (capWord[i] <= 'Z'))
            {
                capWord[i] += 32;
            }
        }
        else if (pattern[i] == '1') 
        {
            if ((capWord[i] >= 'a') && (capWord[i] <= 'z'))
            {
                capWord[i] -= 32;
            }
        }
    }
    return capWord;
}

//Converts decimal integer to binary. 
unsigned dec_to_bin (unsigned integer)
{
    if (integer == 0) return 0;
    return (integer % 2) + 10 * dec_to_bin(integer / 2);
}
3
  • There's something odd going on with your main function. I've counted five { and six }. Have I missed one?
    – Blauelf
    Feb 24 '17 at 12:49
  • Oh, are you seeing duplicates only for the three-character words? Would not be too surprising, as there are only 8 ways to capitalize those. 2 to the nth power for n characters.
    – Blauelf
    Feb 24 '17 at 12:56
  • You're right!!! I get it now. I didn't notice that in my actual program because the array there is a lot bigger than what you see here, so I only saw 3-letter words printed. Thanks! Feb 24 '17 at 20:25

You must log in to answer this question.

Browse other questions tagged .