C语言的隐藏输入

这篇博客介绍了如何在CentOS系统中利用libncurses库来实现终端密码输入的隐藏,通过编译和运行示例代码test.c,用户可以输入密码并由星号(*)掩盖,确保密码不被明文显示。文章详细阐述了安装libncurses库的步骤、编写C语言代码以及创建Makefile的过程,并展示了编译和运行的结果。
摘要由CSDN通过智能技术生成

使用libncurses.so实现隐藏输入,即用 * 掩盖终端输入密码明文

centos上安装libncurses如下

# yum install ncurses-devel ncurses

示例源代码 test.c

#include <stdio.h>
#include <curses.h>

void main()
{
    char pasword[10], ch;
    int i;

    WINDOW * win=  initscr();
    cbreak();
    noecho();

    printf("Enter the password <any 8 characters>: ");

    for(i=0; i<8; i++)
    {
        ch = getchar();
        pasword[i] = ch;
        printf("*");
    }
    pasword[i] = '\0';

    endwin();


    /*If you want to know what you have entered as password, you can print it*/
    printf("\nYour password is :");

    for(i=0;i<8;i++)
    {
        printf("%c",pasword[i]);
    }
    printf("\n");
}

Makefie:

test:
        gcc test.c -o test -lncurses
clean:
        rm -rf test

编译并运行:

# make
gcc test.c -o test -lncurses
# ./test
*nter the password <any 8 characters>: *******
Your password is :12345678

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值