先贴代码:
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PASS_MAX 8
int
main(int argc, char *argv[]) {
if(argc !=2)
return 1;
FILE *list = fopen("/usr/share/dict/words","r");
// FILE *list = fopen("~/cs50/2009fall/psets/2/words","r");
//Giving the salt based on the argv
char salt[3]={argv[1][0],argv[1][1]}, *crypt_pass;
char *temp=(char *)malloc(sizeof(char) * (PASS_MAX+2));
int n;
while( !feof(list)) {
fgets(temp,PASS_MAX+2,list);
n=strlen(temp);
char pass[PASS_MAX+2]={};
//remove NULL at end
for(int i=0;i<n-1;i++)
pass[i] = (char) temp[i];
//encrypt
crypt_pass=crypt(pass,salt);
if(!strcmp(crypt_pass,argv[1])) {
printf("pass %s\n",pass);
free(temp);
return 0;
}
}
free(temp);
return 2;
}
再在.../words里加入了123后运行,才能得到这个密码:
zhihua@ThinkPad (~/cs50/2009fall/psets/2): crack 50Tybs3rY0aeo
pass 123
但是对于其它的都不行,
julius:50zPJlUFIYY0o
skroob:50Bpa7n/23iug
wbrandes:5066.GpLaQCSk
baravelli:50VQc6uJtnGak
blaise:505YXx3Mz50bg
gcostanza:50vfotBdeBr.o
malan:HA8MvF/N223L6
不知道是哪里出错了,还是词库里真的没有上述代码的单词。
有知道的可以哄一声么?