As noted in my comment:
The input from
fgets()
includes the newline; you don't eliminate it. Therefore, you're comparing the encryption of"crimson"
with"crimson\n"
and the answers must be different.
You mentioned in a comment:
…for some reason I had to replace the
strcmp
(it didn't compare like it's supposed to) in the while loop tostrncmp
and set the "int n" to 13.
With general encryption, since the encrypted data is a blob of binary data that might include embedded null bytes '\0'
, you would probably use memcmp()
rather than use either strcmp()
or strncmp()
.
However, since you're using crypt()
, the output has an encoding similar to Base-64. It produces a (fixed length) string, but you have to copy that string to preserve the value. It will be overwritten by the next call to crypt()
. So, you are probably busy stashing the same pointer into the array of pointers. (Aside: that <cs50.h>
header, and in particular its string
type, are annoying!)
Aside:
To be of any use, defines like #define _XOPEN_SOURCE
must appear before any system headers are included. Also, you should specify the version number you want; you should probably use 600 or 700, depending on what your platform supports. You shouldn't have to write the declaration of the crypt()
function in your code. The problem occurred because the #define _XOPEN_SOURCE
was misplaced and misvalued.
Also, the line salt = strcat(&argv[1][0], &argv[1][1]);
is weird and invokes undefined behaviour. It is weird because it tries to add the string starting at the second character of argv[1]
to the end of argv[1]
, and invokes undefined behaviour because (a) the strings overlap and (b) you're writing into space that isn't available.