0

Variable hash1 becames equal hash2 after operator hash2 = crypt(key,"50");.

#define _XOPEN_SOURCE
#include <unistd.h>
#define alphalen 52
#define halfalphalen 26
#define plen 5
#include <stdio.h>
#include <cs50.h>
#include <string.h>

int main (int argc, string argv[])
{
    int i,j,k,l,m;
   // printf("%s\n",argv[1]);
    string hash1 = crypt(argv[1],"50");
    char* key;
    key = calloc(plen+1,sizeof(char));
    char* hash2;
    hash2 = malloc(sizeof(char));
    for (i = 0; i < alphalen; i++)
    {
        if (i < halfalphalen)
            key[0] = i + 'A';
        else
            key[0] = i - halfalphalen + 'a';
        for (j = 0; j < alphalen; j++)
        {
            if (j < halfalphalen)
                key[1] = j + 'A';
            else
                key[1] = j - halfalphalen + 'a';
            for (k = 0; k < alphalen; k++)
            {
                if (k < halfalphalen)
                    key[2] = k + 'A';
                else
                    key[2] = k - halfalphalen + 'a';
                for (l = 0; l < alphalen; l++)
                {
                    if (l < halfalphalen)
                        key[3] = l + 'A';
                    else
                        key[3] = l - halfalphalen + 'a';
                    for (m = 0; m < alphalen; m++)
                    {
                        if (m < halfalphalen)
                            key[4] = m + 'A';
                        else
                            key[4] = m - halfalphalen + 'a';
                        hash2 = crypt(key,"50");
                        if (strcmp(hash1, hash2) == 0)
                        {
                            printf("%s\n",key);
                            free(key);
                            //free(hash2);
                            return 0;
                        }    
                    }
                }
            }    
        }   
    }
}
0

I looked at the code and there was a lot that I didn't understand it's purpose, but I had some ideas. I'm also not addressing the question of why "hash1 becames equal hash2" because I think the underlying use of hash1 makes it a moot point.

First question: What purpose does hash1 serve? Look at this code:

string hash1 = crypt(argv[1],"50");

argv[1] contains the encrypted string that needs to be DEcrypted. It is then passed to crypt(), which returns the encrypted value of that string, and stores it in hash1. So, simply put, hash1 contains an encryption of an encrypted string. What use is this???

Moving on, let's look at the cascade of nested for loops. While the nesting looks ok, what does each stage do? Let's look at the first one:

    if (i < halfalphalen)
        key[0] = i + 'A';
    else
        key[0] = i - halfalphalen + 'a';

The control variable i always starts at 0, so the if condition is always true, and key[0] will always be set to 0 + 'A' or 'A' every time. Same logic applies in the other loops. Ultimately, every execution will produce key = "AAAAA" regardless of input.

Maybe the test condition should be if (argv[0][i] < halfalphalen) ???

There may be more issues, but these need to be fixed first.

As an educational exercise, you could go back and figure out why hash1 and hash2 end up with the same value. Bonus question: They're actually both char* pointers underneath. Are they two different addresses with the same values stored in two places, or are they two pointers with the same addresses???? ;-)

If this answers your question, well, if it gives you enough info to work with, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • On first question. I used string hash1 in my educational purpose to check if my code with nested for loops will print right key respective to hash2. On second question. I think each stage works other way: key[0] is first letter in 5 letter password and passes A to Z while i < halfalphalen and a to z if condition false. Letters change because of for (i = 0; i < alphalen; i++). On bonus question. These pointers are different adresses with different value that become same after the operator. Dec 7 '18 at 14:32

You must log in to answer this question.

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