密码校验实例

本文探讨了如何使用C语言中的getspnam函数获取用户信息,通过crypt函数加密密码,并展示了getpass函数在无回显输入密码的应用。重点介绍了MD5、Blowfish等加密算法在密码保护中的角色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.getspnam函数

#include <shadow.h>

struct spwd *getspnam(const char *name);
struct spwd {
    char *sp_namp;     /* Login name */
    char *sp_pwdp;     /* Encrypted password */
    long  sp_lstchg;   /* Date of last change
                          (measured in days since
                          1970-01-01 00:00:00 +0000 (UTC)) */
    long  sp_min;      /* Min # of days between changes */
    long  sp_max;      /* Max # of days between changes */
    long  sp_warn;     /* # of days before password expires
                          to warn user to change it */
    long  sp_inact;    /* # of days after password expires
                          until account is disabled */
    long  sp_expire;   /* Date when account expires
                          (measured in days since
                          1970-01-01 00:00:00 +0000 (UTC)) */
    unsigned long sp_flag;  /* Reserved */
};

2.crypt函数

原串 和 杂质串 返回一个加密串

 #define _XOPEN_SOURCE       /* See feature_test_macros(7) */
 #include <unistd.h>

 char *crypt(const char *key, const char *salt);
 //原串 和 杂质串 返回一个加密串
Link with -lcrypt

①cc -D _POSIX_SOURCE file.c
②#define _XOPEN_SOURCE
决不是简单的宏定义
它是使程序符合系统环境的不可缺少的部分

 If salt is a character string starting with the characters "$id$" 
 followed  by  a string terminated by "$":

$id$salt$encrypted

   then  instead  of using the DES machine, id identifies the 
   encryption method used and this then determines how the rest of 
   the password string is interpreted.  
   The following values of id are supported:

     ID  | Method
     ─────────────────────────────────────────────────────────
     1   | MD5
     2a  | Blowfish (not in mainline glibc; added in some
         | Linux distributions)
     5   | SHA-256 (since glibc 2.7)
     6   | SHA-512 (since glibc 2.7)

既指定ID又指定字符串,公用一个参数

3.getpass函数

NAME
       getpass - get a password

SYNOPSIS
       #include <unistd.h>

       char *getpass( const char *prompt);

关闭终端的回显功能,需要自己去实现这个prompt终端提示,返回你输入的密码

4.一个demo

#include <stdio.h>
#include <stdlib.h>
#include <shadow.h>
#include <string.h>
#include <unistd.h>


int main(int argc, char **argv)
{
        char *passwdInput;//保存 用户输入的字符串的地址
        struct spwd * UserInfo;//保存用户信息
        char *cryptedPassword;//保存经过加密的字符串

        if(argc < 2)
        {
                fprintf(stderr, "No Enugh argc\n");
                exit(0);
        }

		//得到用户输入的字符串 不回显 提示符
        passwdInput = getpass("Password: ");
        if(NULL == passwdInput)
        {
                perror("getpass :\n");
                exit(1);
        }

		//获取用户信息
        UserInfo = getspnam(argv[1]);
        if(NULL == UserInfo)
        {
                perror("getspnam :\n");
                exit(1);
        }
		
		//对获得的密码进行加密  sp_pwdp里面的格式 我们只会取到前两个$
        cryptedPassword = crypt(passwdInput, UserInfo->sp_pwdp);
        if(0 == strcmp(cryptedPassword, UserInfo->sp_pwdp))
        {
                printf("OK!\n");
        }
        else
        {
                printf("Failed\n");
        }

        return 0;
}

在这里插入图片描述
在这里插入图片描述
必须以root权限执行,root就是神一样的用户

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值