- 论坛徽章:
- 0
|
我打算对登录用户进行验证,我的思路是这样的:
我更加输入的用户名和密码(明文)用getpwnam得到密文再取密文头两个字符作为salt,用户输入的口令明文做key,调用crypt函数将返回值和getpwnam得到的密文比较相同就认证通过;但是我的结果怎么始终不同?还有就是我用非root用户验证会报Segmentation fault;
代码如下:
#include <unistd.h>;
#include <stdio.h>;
#include <netinet/in.h>;
#include <pwd.h>;
#include <stdio.h>;
#include <crypt.h>;
#include <string.h>;
main()
{
struct passwd *pw;
char salt[3];
char key[255];
char *crypt_pw;
pw=getpwnam("root" ;
printf("%s password : %s\n",pw->;pw_name,pw->;pw_passwd);
printf("user id = %s\n",pw->;pw_uid);
printf("user gid = %d\n",pw->;pw_gid);
printf("pw_gecos = %s\n",pw->;pw_gecos);
printf("pw_dir = %s\n",pw->;pw_dir);
printf("pw_shell = %s\n",pw->;pw_shell);
strncpy(salt,pw->;pw_passwd,2);
printf("Salt : %s\n", salt);
crypt_pw = crypt("12345678",salt);
printf("crypt_pw : %s\n",crypt_pw);
if (strcmp(pw->;pw_passwd,crypt_pw)==0)
printf("login success!\n" ;
else
printf("login failed!\n" ;
}
root用户的执行结果:
root password : x
user id = (null)
user gid = 0
pw_gecos = root
pw_dir = /root
pw_shell = /bin/bash
Salt : x
crypt_pw : xxhDnUCapAZ6Y
login failed!
yvan用户的执行结果:
yvan password : x
Segmentation fault |
|