0

I am running this program with with ./crack 50yoN9fp966dU
50yoN9fp966dU
is crimson encrypted. which is on the word list. My program is as follow:

#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    if(argc > 2)  
    {
        printf("Invalid Argument \n");
        return 1;
    } 
    else 
    {
        FILE *fp1;
        fp1 = fopen("/usr/share/dict/words", "r");
        char line[9];
        while (fgets(line, 9, fp1) != NULL)
        {
            char *EncryptLine1;
            char *EncryptLine2;
            printf("%s", line);
            EncryptLine1 = crypt(line, "50"); 
            if(argv[1] == EncryptLine1)
            {
                printf("%s \n", line);
            }
            EncryptLine2 = crypt(line, "HA");
            if(argv[1] == EncryptLine2)
            {
                printf("%s \n", EncryptLine2);
            }
        }  
    }
}

If I add a printf("%s", EncryptLine1), I see the argv[1], i.e 50yoN9fp966dU, but the loop continue and does not print the answer.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 1
    Try using strcmp to compare strings. I belive using double equation signs doesn't work in c with strings. – Luke Aug 02 '14 at 18:36
  • 3
    You are comparing memory location (addresses). To compare strings use `strcmp()` and family. – alvits Aug 02 '14 at 18:36

3 Answers3

2

You are doing pointer comparison instead of contents (pointed data) comparison.

Change this:

if (argv[1] == EncryptLine1)

To this:

if (strcmp(argv[1],EncryptLine1) == 0)

And this:

if (argv[1] == EncryptLine2)

To this:

if (strcmp(argv[1],EncryptLine2) == 0)
barak manos
  • 29,648
  • 10
  • 62
  • 114
1

argv[1], EncryptLine1 and EncryptLine2 are all char*s. operator== on two char*s simply checks to see if they are pointing to the same memory location. What you want is to compare the contents of the strings they represent. So, the ifs should look like this:

if(!strcmp(argv[1], EncryptLine1))
Pradhan
  • 16,391
  • 3
  • 44
  • 59
1

You have some problems in your code:

  1. You blithely assume argc will never be smaller than 2. Check for unequal 2 instead of bigger two in your first condition.
    Anyway, if you return out of an if-block, using else and doing deeper nesting is really superfluous.
  2. Strings cannot be compared with ==, use strcmp.:

    if( ! strcmp(argv[1], EncryptLine1))
    
  3. You need to add a break to break out of the loop or a return to leave the function in your conditional block after printing success, if you want to end the loop there.

    if( ! strcmp(argv[1], EncryptLine1)) {
        printf("%s \n", line);
        break;
    }
    

BTW: Why don't you reuse EncryptLine1 (not that you need any temporary at all there)?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118