0

I have created a series of for loops to iterate through every combination of a 4 letter long password (i j k l). I wish to enter each iteration into the crypt function but what I have been trying has not worked so far (code show below), the crypt function only acknowledges 1 of the 4 chars.

I have attempted concatenating the chars together but that does not seem to work. Any advice would be much appreciated!

Specific issue:

crypt((letters[i]letters[j]letters[k]letters[l]),"50")

Whole Code:

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

char* letters[] = {" ","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("missing command-line argument\n");
        return 1;
    }
    else
    {
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            { 
                for (int k = 0; k < 5; k++)
                {
                    for (int l = 0; l < 5; l++)
                    {
                        if (strcmp(argv[1],crypt((letters[i]letters[j]letters[k]letters[l]),"50")) == 0)
                        {
                            printf("Password: %s%s%s%s", letters[i],letters[j],letters[k],letters[l]);
                            break;
                        }
                    }
                }
            }
        }
        return 0;
    }
}

error code:

pset2/crack4.c:27:61: error: expected ')'             
if(strcmp(argv[1],crypt((letters[i]letters[j]letters[k]letters[l]),"50")) == 0)
                                                        ^
pset2/crack4.c:27:50: note: to match this '('              
if(strcmp(argv[1],crypt((letters[i]letters[j]letters[k]letters[l]),"50")) == 0)
                                             ^
1 error generated.
2
  • Does it indeed compile?? no mistakes here? crypt((letters[i]letters[j]letters[k]letters[l]),"50"); – obesman Jul 10 '17 at 16:33
  • Unfortunately not. Only one error code, I have added this to the bottom of the post, easier to read there :) – Hugo Hasted Jul 11 '17 at 11:35
0

Your code is too far from the target one. I would, if I may, recommend you to watch again the lecture (string part especially) and the walkthrough.

Some hints:

  1. Note that you try to iterate (to be fair, fail to iterate) over letters[0] to letter[5] only that is you try to check just few first lowercase letters. You probably need at least 2 arrays (or even more). One to hold alphabet and others to hold password guesses.

  2. If you have an array of 5, let it be password[5], you can crypt it by crypt(password, "50") (no brackets).

  3. char* letters[] = {" ","a" ...} is not an array of chars as you may think. This is the array of strings or the double dimension array of chars. Most probably, you made it unintentionally, just because your compiler advised you to add * to get rid of an error. That is because you initialize the array with "a", "b" ... that is strings not chars. You should change it to 'a', 'b' and set type to char not char*. You may initialize it with a string that is easier. Instead of char letters[] = {'a','b' ...} you can do the same by char letters[] = "ab ..."

  4. `And why do you add space as the first char??
3
  • Hey, thank you very much for your advice, I've responded to your parts below. I think I will rewatch and give this another go but want to understand why this doesn't work first. 1. I was a plonlker here, it should iterate between 1 and 53, I just changed it so I could run tests. 2. 3. Your completely right there, I have changed that. 4. I put the space it to account for 1, 2 and 3, letter password, bit crude but it works I believe. crypt(a ,50) == crypt( a,50) – Hugo Hasted Jul 11 '17 at 17:11
  • Part 2: I thought a method were instead of writing them all to memory but one where it checks as it goes along would be more efficient. Is there a way I can get crypt to accept 4 chars as the key? and if so, would the overall program would work surely? Thanks for your help again! :) – Hugo Hasted Jul 11 '17 at 17:17
  • 1. No, crypt("a","50") != crypt(" a", 50) 2. No, you cant get crypt accepted 4 chars. By definition, crypt accepts strings as input. String is a sequences of consecutive bytes in memory ended with '\0' byte. – obesman Jul 11 '17 at 18:03
0

You need to find a different way to represent the passwords you are trying to test. You might want to make an array char testword[5]; containing the five letters of the password. keep in mind that if a password has less than 5 characters, you cannot use spaces, the values need to be empty.

You can put the values into the array using testword[0] = letters[i]; testword[1] = letters[j]; etc, and then feed the array to crypt(), it will still works even if its not a string literal char *.

Good luck!

You must log in to answer this question.

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