0

I've been scratching my head as to how to use the crypt function, even after having watched the walkthrough video a dozen times and trying to decipher the man crypt pages.

I've written this code to do the comparison so far:

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

int main(int argc, string argv[])
{
    // Check password to be cracked has been provided as second argument
    if (argc != 2)
    {
        if(argc == 1)
        {
            printf("Error: No password to be deciphered has been provided\n");
            return 1;
        }
        else
        {
            printf("Error: More than two arguments have been provided\n");
            return 1;    
        }
    }

    // Get encyrpted password to be deciphered
    string k = argv[1];

    printf("Encrypted password to match: %s\n", k);

    // Starting password
    // string password = "rofl";
    string password = "rofl\0";

    // Assuming salt, first 2 chars of hashed password

    // Case 1 - salt[3]
    char salt1[3] = {k[0],k[1],'\0'};

    string guess1 = crypt(password,salt1);

    if (guess1 == k)
    {
        printf("salt 1 - PASS - %s\n", salt1);
        return 0;
    }
    else
    {
        printf("salt 1 - %c%c%c -> %s did not match with %s\n", salt1[0], salt1[1], salt1[2], guess1, k);
    }

    // Case 2 - salt[2]
    char salt2[2] = {k[0],k[1]};

    string guess2 = crypt(password,salt2);

    if (guess2 == k)
    {
        printf("salt 2 - PASS - %s\n", salt2);
        return 0;
    }
    else
    {
        printf("salt 2 - %c%c -> %s did not match with %s\n", salt2[0], salt2[1], guess2, k);
    }

    // Case 3 - salt string
    string salt3 = "50\0";

    string guess3 = crypt(password,salt3);

    if (guess3 == k)
    {
        printf("salt 3 - PASS - %s\n", salt3);
        return 0;
    }
    else
    {
        printf("salt 3 - %s -> %s did not match with %s\n", salt3, guess3, k);
    }

    // Case 4 - salt string
    string salt4 = "50";

    string guess4 = crypt(password,salt4);

    if (guess4 == k)
    {
        printf("salt 4 - PASS - %s\n", salt4);
        return 0;
    }
    else
    {
        printf("salt 4 - %s -> %s did not match with %s\n", salt4, guess4, k);
    }

    // Case 5 - salt[]
    char salt5[] = "50";

    string guess5 = crypt(password,salt5);

    if (guess5 == k)
    {
        printf("salt 5 - PASS - %s\n", salt5);
        return 0;
    }
    else
    {
        printf("salt 5 - %c%c -> %s did not match with %s\n", salt5[0], salt5[1], guess5, k);
}

    // Print failure statement + return error
    printf("Password could not be decrypted\n");
    return 2;
}

This has been compiled with:

~/workspace/pset2/crack/ $ clang -ggdb3 -O0 -std=c11 -Wall -\Werror -Wshadow cryptfn.c -lcrypt -lcs50 -lm -o cryptfn

It produces output:

~/workspace/pset2/crack/ $ ./cryptfn 50fkUxYHbnXGw
Encrypted password to match: 50fkUxYHbnXGw
salt 1 - 50 -> 50fkUxYHbnXGw did not match with 50fkUxYHbnXGw
salt 2 - 50 -> 50fkUxYHbnXGw did not match with 50fkUxYHbnXGw
salt 3 - 50 -> 50fkUxYHbnXGw did not match with 50fkUxYHbnXGw
salt 4 - 50 -> 50fkUxYHbnXGw did not match with 50fkUxYHbnXGw
salt 5 - 50 -> 50fkUxYHbnXGw did not match with 50fkUxYHbnXGw
Password could not be decrypted

So the encrypted strings look the same, but they're not.

I've run debug50, and it is here I can see, the values aren't the same.

My encrypted guesses have value 0x7ffff7dd9200 <_ufc_foobar+131200> "50fkUxYHbnXGw", but k/argv[1] has value 0x7fffffffe34d "50fkUxYHbnXGw"

I googled "_ufc_foobar" and much to my surprise, this came up - Pset2 Hacker Edition- Crack.c - but it makes no sense to me. I don't think it's the same error.

Can anyone point me in the right direction? Many thanks.

2

Strings in C are pointers to char, pointing to the first character in the string, so the value you compared is a memory address. To compare strings by content rather than by address, use the strcmp function. And there's no need for specifying an explicit null terminator in a string literal, as it contains an implicit one, but using a list of char (salt2), it's not added automatically.

1
  • Thanks Blauelf. strcmp is what I needed, works like a charm :) Mar 16 '17 at 4:53

You must log in to answer this question.

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