0

So I am trying to solve the password crack. I managed to create all possible 4 letter words with character from A to z. However the encryption using crypt does not always seem to work. I encrypted halo using salt 50 to make 50Ka7rarwEQ1. and when I try to decrypt this, my program does print 'halo'. However, when using the examples provided by the assignment, the program runs for a while and then prints nothing. Does anyone know what I did wrong here?

Thanks!

#define _XOPEN_SOURCE
#include <unistd.h>

#include <stdio.h>
#include <cs50.h>

#include <string.h>
#include <ctype.h>


int main(int argc, string argv[])
{
char array[4];
string salt = "50";
   if (argc != 2)
   {
       printf("Error, no hashed password was given.");
       return 1;
   }
   else
   {
   for (int i = 64; i < 123; i++)
        {
            char letteri = i;
            if (isalpha(letteri))
            {
                array[0] = i;
            }
            for (int j = 64; j < 123; j++)
            {
                char letterj = j;
                if (isalpha(letterj))
                {
                array[1] = j;
                }
                for (int k = 64; k < 123; k++)
                {
                    char letterk = k;
                    if (isalpha(letterk))
                    {
                    array[2] = letterk;
                    }
                    for (int l = 64; l < 123; l++)
                    {
                        char letterl = l;
                        if (isalpha(letterl))
                        {
                        array[3] = letterl;
                        string result = crypt(array,salt);
                        if (strcmp(result, argv[1]) == 0)
                        {
                            printf("%s\n", array);
                        }
                        }
                    }
                }
            }
        }
        return 0;
   }
}
0

First of all: In C, strings are null-terminated. That means that C will interpret all characters as part of the string until it hits 0. Your array has four elements, and there's no null for termination. Increase its size by 1, and set that last element to 0.

Also, you're far less efficient than you could be. You assign only alphabetic characters, but then still run the for-loop with the value you already used. The for-loop would have to be in the same if block as the assignment.

And of course that kind of nested loops is very inflexible, in this way it restricts you to exactly four characters.

BTW, letterj and j differ only in type, you really don't need both. char is nothing more than an eight bit integer on all systems I know (whether signed or unsigned depends on implementation)

9
  • Thank you for your advice, it sure helped me to get a better understanding of the way C works. Also after your suggestions, the program works for a few hashed passwords. However, it does not work for all. Is there perhaps something else I am overlooking? – Danielle Bianchi Feb 2 '17 at 15:14
  • Are the failing ones also alphabetic and exactly four characters long? – Blauelf Feb 2 '17 at 15:42
  • For the failing ones nothing gets printed out – Danielle Bianchi Feb 2 '17 at 18:40
  • Repeating: Are the failing ones also alphabetic and exactly four characters long? – Blauelf Feb 2 '17 at 18:42
  • Sorry I misunderstood, I used the hashed password provided in the exercise, so I assume these are also alphabetic and four characters long. Is this what you meant? – Danielle Bianchi Feb 3 '17 at 13:53

You must log in to answer this question.

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