如何在C中屏蔽密码? [英] How to mask password in c?
本文介绍了如何在C中屏蔽密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C语言中,我想显示用户键入的每个单个字符* (例如,请输入您的密码:*****)
In C, I want to display every single character that the user type as * (Ex, Please type in your password: *****)
我正在四处搜寻,但无法为此找到解决方案. 我正在使用Ubuntu.有人知道一个好方法吗?
I'm searching around but can't be able to find a solution for this. I'm working on Ubuntu. Does anybody know a good way?
推荐答案
请参阅我的代码.它可以在我的FC9 x86_64系统上运行:
See my code. it works on my FC9 x86_64 system:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
int main(int argc, char **argv)
{
char passwd[16];
char *in = passwd;
struct termios tty_orig;
char c;
tcgetattr( STDIN_FILENO, &tty_orig );
struct termios tty_work = tty_orig;
puts("Please input password:");
tty_work.c_lflag &= ~( ECHO | ICANON ); // | ISIG );
tty_work.c_cc[ VMIN ] = 1;
tty_work.c_cc[ VTIME ] = 0;
tcsetattr( STDIN_FILENO, TCSAFLUSH, &tty_work );
while (1) {
if (read(STDIN_FILENO, &c, sizeof c) > 0) {
if ('\n' == c) {
break;
}
*in++ = c;
write(STDOUT_FILENO, "*", 1);
}
}
tcsetattr( STDIN_FILENO, TCSAFLUSH, &tty_orig );
*in = '\0';
fputc('\n', stdout);
// if you want to see the result:
// printf("Got password: %s\n", passwd);
return 0;
}
这篇关于如何在C中屏蔽密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文