C 程序实现密码隐秘输入 linux系统可执行

读写用户输入,屏幕不回显

char *getpass( const char *prompt);

getpass用于从键盘读取用户输入,但屏幕不回显。

参数prompt为屏幕提示字符。

函数返回值为用户键盘输入的字符串。

屏幕不回显指的是,用户输入的内容,不会显示任何提示信息,就是在Linux中切换用户时,输入密码不现实一样。

程序如下:

 
 1     #include <stdio.h>  
 2     #include <unistd.h>  
 3       
 4     int main(int argc, char *args[])  
 5     {  
 6         // 调用getpass函数  
 7         // 函数的参数是提示信息  
 8         // 函数的返回值是用户输入的内容  
 9         char *password = getpass("Input your password : ");  
10         // 输出用户输入的信息  
11         printf("password = %s\n", password);  
12         return 0;  
13     }  
14 
15  

 

编译并执行程序

  1. [negivup@negivup mycode]$ gcc -o main main.c  
  2. [negivup@negivup mycode]$ ./main  
  3. Input your password :                           ------------这里输入内容不会回显  
  4. password = 123456 
posted @ 2017-11-08 22:58  xiaobingzzh  阅读(501)  评论(0编辑  收藏  举报