基于 crypt.c 示例,我有以下代码,但是当我运行它时,password1 在第二次调用 crypt 期间被损坏。谁能发现问题?
对两个请求输入相同的密码应该会导致最后所有三个字符串的值相同。
=================================================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <crypt.h>
#define _XOPEN_SOURCE
#include <unistd.h>
/*
cc calc1.c -ocalc1 -lcrypt
*/
int main(void) {
unsigned long seed[2];
char salt[] = "$1$zhodFRlE$";
char *password2,*password1,*temp1;
int i,ok;
printf("%s is Salt\n",salt);
password1 = crypt(getpass("Password1 :"), salt);
printf("Crypt Password1: %s\n",password1);
temp1 = strdup(password1);
printf("Crypt temp1: %s\n",temp1);
password2 = crypt(getpass("Password2 :"),temp1);
printf("Crypt Password1: %s\n",password1);
printf("Crypt temp1: %s\n",temp1);
printf("Crypt Password2: %s\n",password2);
return 0;
}
==================================================== ===============