For a school project I am tasked to decrypt DES passwords. I have sample code provided below and I want to know if this is the best method. Also should I code in error handling for the dictionary file or does the while
condition take care of this?
#include <stdio.h>
#include <crypt.h>
#include <unistd.h>
#include <string.h>
#define _XOPEN_SOURCE
int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("Usage: ./crack <password>\n");
return 1;
}
int n = strlen(argv[1]);
char password[n];
for (int i = 0; i < n; i++)
{
password[i] = argv[1][i];
}
char salt[2];
salt[0] = password[0];
salt[1] = password[1];
FILE *fp;
fp = fopen("/usr/share/dict/words", "r");
char line[50];
while(fgets(line,50,fp)!= NULL)
{
line[strlen(line) - 1] = '\0';
if (!strcmp(argv[1], crypt(line, salt)))
{
printf("Password found!\n");
return 0;
}
}
printf("Not found\n");
fclose(fp);
return 0;
}