【发布时间】:2019-08-24 00:16:54
【问题描述】:
我有一个加密一些字符串的功能。 这太棒了,但是......这是错误:(
我可以使用该功能一次,但第二次,崩溃。
谢谢! :)
我使用 bash ubuntu (W10),编译我的项目时没有警告(和错误)。
char * encryptPassword(char * string){
printf("DEBUT\n");
FILE *fp=NULL;
char path[1035];
char password[32];
//char * password = NULL; //for the encrypt password
printf("MALLOC\n");
//password = (char *)malloc(33*sizeof(char));
char * result = NULL;
char chaine[128] = "echo ";
char end_chaine[128] = " | openssl md5 | cut -d ' ' -f2";
//Create the command
printf("STRCAT\n");
strcat(chaine,string);
strcat(chaine,end_chaine);
//Execute
printf("POPEN %s\n",chaine);
fp = popen(chaine, "r");
//Reclaim the encrypted password
printf("GETS\n");
fgets(path, sizeof(path)-1, fp);
pclose(fp);
//To remove the character '\n'
printf("SPRINTF\n");
sprintf(password,"%32s",path);
result = strtok(password,"\n");
printf("%s\n",result);
//OK IT'S FINISH !
return (result);
}
【问题讨论】:
-
查看
popen的返回值? -
sprintf(password,"%32s",path);应该说31,因为数组是char password[32];。奇怪的是你替换了password = (char *)malloc(33*sizeof(char));。我想你应该有char password[33]; -
不要对预定义的字符串使用任意长度的缓冲区。
char* chaine = "echo"。这将阻止您考虑连接到它们,因为您不应该这样做。而是使用专门用于连接的缓冲区。这段代码似乎充斥着关于各种字符串长度的疯狂假设,因此您可能在这里遇到大量缓冲区溢出问题。 -
除此之外,MD5 不适合散列密码。它太弱了,使用现代工具几乎可以立即破解较短的密码。这些天的绝对基线是一个特定于密码的哈希,其难度可变,例如Bcrypt。
-
我知道 fo md5 但谢谢 :)