I've been working on the pset2 hacker problem and decided to decompose a problem by creating subtasks. Anyway, the first part was to simply compare two crypted strings (at the moment, just one letter strings crypted using crypt) using strcmp, but it does not seem to work for some reason. Anyway, the key is 'a' and I made a loop around printable characters, but for some reason the strcmp works oddly with the codes (gives that crypted strings are equal when they are not). I've also been thinking about how to approach the "immutability of strings" and the overall changing the length of the string problem and I have a weird idea, that might work, but I am certain that some have created better solutions, so I'd be grateful if you could share.
Any other tips on this segment would also be appreciated! Thanks a lot!
string key = "a";
string salt = "50";
char test[2];
string coded = crypt(key, salt);
printf("%s\n", coded);
int result;
for (char i = 'B'; i <= 'z'; i++)
{
test[0] = (char)i;
test[1] = '\0';
result = strcmp(coded, crypt(test,salt));
printf("%i\n", strcmp(coded, crypt(test,salt)));
if (result == 0)
{
printf("%s\n", crypt(test,salt));
for (int j = 0, n = strlen(test); j < n; j++)
{
printf("%c", test[j]);
}
printf("\n");
break;
}
}
// results
50OqznXGVcOJU
0
506AM4LYuoGsQ
B