C语言不显示输入密码

原文地址:https://www.cnblogs.com/liqinglucky/p/hide_input.html

通过C语言实现隐藏密码输入。目标是做到读写用户输入,屏幕不回显。

程序

隐藏输入参考:C 程序实现密码隐秘输入 linux系统可执行 - xiaobingzzh - 博客园 (cnblogs.com)

文件passwd.c

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

int main(int argc, char **argv)
{
	//方案1:显示输入密码
	//char password[20] = {0};
	//scanf("%s",password) ;
	//方案2:不显示输入密码
    char *password = getpass("Input your password : ");
    
    if (strcmp (password, "123456") == 0){
        printf( "password right\n");
    }else
    {
        printf( "password worng\n");
    }

    return 0;
}

测试

# gcc passwd.c -o passwd
# ./passwd
Input your password :          <<< 输入:123456。并不显示在屏幕
password right

其他方案:

C语言模拟密码输入(显示星号) - 朴素贝叶斯 - 博客园 (cnblogs.com)

遇到问题:

问题1:linux 没有conio.h
# gcc main.c -o main
main.c:5:10: fatal error: conio.h: No such file or directory
    5 | #include <conio.h>
      |          ^~~~~~~~~
compilation terminated.

问题2:将 conio.h 替换成 <curses.h>  编译报错
#include <curses.h>

# gcc main.c -o main
/usr/bin/ld: /tmp/ccS9zJta.o: in function `getpwd':
main.c:(.text+0x96): undefined reference to `stdscr'
/usr/bin/ld: main.c:(.text+0x9e): undefined reference to `wgetch'
collect2: error: ld returned 1 exit status
posted @ 2023-02-15 16:24  liqinglucky  阅读(181)  评论(0编辑  收藏  举报