linux 下password加密程序(能够用于替换shadow文件里的用户password)

源代码例如以下:

#include <stdio.h>
#include <unistd.h>


int main(int argc, char *argv[]){
    if(argc != 3){
        printf("%s <salt> <crypt>
",argv[0]);
        return -1;
    }

    char *passwd = crypt(argv[1],argv[2]);

    printf("passwd : %s
",passwd);
    return 0;
}

总结:

char *crypt(const char *key, const char *salt);
crypt是个password加密函数,它是基于Data Encryption Standard(DES)演算法。crypt仅仅适用于password的使用,不适合用于资料加密。

crypt()将參数key所指的字符串加以加密。key字符串长度仅取前8个字符,超过此长度的字符没有意义。參数salt为两个字符组成的字符串,由a-z、A-Z、0-9。“.”和“/”所组成,用来决定使用4096 (a-z、A-Z、0-9。“.”和“/”共64个字符。64的平方为4096)种不同内建表格的哪一个。函数运行成功后会返回指向编码过的字符串指针,參数key 所指的字符串不会有所更动。编码过的字符串长度为13 个字符。前两个字符为參数salt代表的字符串。 返回值: 返回一个指向以NULL结尾的password字符串。 注意编译时要在末尾加入 -lcrypt 选项。

原文地址:https://www.cnblogs.com/mthoutai/p/7081057.html