隐藏终端上的密码输入
我想在使用 *
写入密码时隐藏密码。 我使用 Linux GCC 来编写此代码。 我知道一种解决方案是使用像这样的 getch()
函数
#include <conio.h>
int main()
{
char c,password[10];
int i;
while( (c=getch())!= '\n');{
password[i] = c;
printf("*");
i++;
}
return 1;
}
,但问题是 GCC
不包含 conio.h
文件,因此,< code>getch() 对我来说没用。 有人有解决办法吗?
I want to mask my password while writing it with *
.
I use Linux GCC for this code.
I know one solution is to use getch()
function like this
#include <conio.h>
int main()
{
char c,password[10];
int i;
while( (c=getch())!= '\n');{
password[i] = c;
printf("*");
i++;
}
return 1;
}
but the problem is that GCC
does not include conio.h
file so, getch()
is useless for me.
Does anyone have a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
在Linux世界中,屏蔽通常不使用星号完成,通常回显只是关闭并且终端显示空白例如,如果您使用
su
或登录到虚拟终端等。有一个库函数为了处理获取密码,它不会用星号掩盖密码,但会禁用密码到终端的回显。这是我从一本 Linux 书中摘下来的。我相信它是 posix 标准的一部分
谷歌搜索 getpass() 提供了对 GNU 实现的引用(应该在大多数 Linux 发行版中)以及一些示例代码,用于在需要时实现您自己的代码
http://www.gnu.org/s/hello/manual/libc/getpass.html
他们自己推出的示例:
如果需要你可以用这个作为基础上修改它以显示星号。
In the Linux world, masking isn't usually done with asterisks, normally echoing is just turned off and the terminal displays blanks E.g. if you use
su
or log into a virtual terminal etc.There is a library function to handle getting passwords, it won't mask the password with asterisks but will disable echoing of the password to terminal. I pulled this out of a linux book I have. I believe its part of the posix standard
A google search for getpass() has a reference to the GNU implementation (should be in most linux distros) and some sample code for implementing your own if need be
http://www.gnu.org/s/hello/manual/libc/getpass.html
Their example for rolling your own:
If need be you could use this as the basis as modify it to display asterisks.
如果不依赖
getch
并避免过时的getpass
,建议的方法是通过使用termios
禁用终端 ECHO。经过几次搜索,找到了一个固定的灵活密码例程,令我惊讶的是,很少有人可以单独使用 C。而不是简单地使用 termiosc_lflag
选项重新编码getch
,稍微更通用的方法只需添加一些内容。除了替换getch
之外,任何例程都应该强制执行指定的最大长度以防止溢出,如果用户尝试输入超出最大值则进行截断,并在以某种方式发生截断时发出警告。下面的添加内容将允许从任何 FILE * 输入流读取,将长度限制为指定长度,在输入时提供最小编辑(退格)能力,允许指定或完全禁用字符掩码,最后返回输入的密码长度。当输入的密码被截断至最大或指定长度时添加警告。
希望它对其他寻求类似解决方案的问题有用:
显示用法的简单程序如下。如果使用静态字符数组来保存密码,只需确保将指针传递给该函数即可。
示例输出
Without
getch
to rely on and avoiding the obsoletegetpass
, the recommended approach is to disable terminal ECHO throughtermios
use. After a few searches to find a canned flexible password routine, I was surprised that very few for stand-alone use with C. Rather than simply recodinggetch
with termiosc_lflag
options, slightly more generalized approach takes just a few additions. Beyond replacinggetch
any routine should enforce a specified maximum length to prevent overflow, truncate if the user attempt to enter beyond the maximum, and warn if truncation occurs in some manner.Below, the additions will allow reading from any
FILE *
input stream, limiting the length to a specified length, provide minimal editing (backspace) ability when taking input, allow the character mask to be specified or disabled completely, and finally return the length of the password entered. A warning was added when the password entered was truncated to the maximum or specified length.Hopefully it will prove useful to others with this question looking for a similar solution:
A simple program showing the use would be as follows. If using a static array of character for holding the password, just insure a pointer is passed to the function.
Example Output
可以使用以下代码模拟
getch
的功能(这是一个非标准的 Windows 函数):请注意,您的方法并不完美 - 最好使用像 ncurses 这样的东西> 或另一个终端库来处理这些事情。
The functionality of
getch
(which is a non-standard, Windows function) can be emulated with this code:Note that your approach is not perfect - it's better to use something like ncurses or another terminal library to handle these things.
您可以通过这种方式在 Linux 上创建自己的
getch()
函数。演示代码:
You can create your own
getch()
function on Linux in this manner.Demo code:
感谢大家的帮助和帮助!支持解决我的问题。
我找到了最适合我的在 Linux 中隐藏密码的最佳方法。
使用 getpass() 函数。它只需要包含“unistd.h”文件。
getpass 函数的语法:
char * getpass (const char *prompt)
参数:
提示:询问密码时要打印的字符串指针
返回值:
密码的字符串指针
示例:
输出:
Enter 密码:
heet
Thanks all of you whose help & support to solve my problem.
I find a best way to hide my password in linux that fits me best.
To use getpass() function. It just need to include "unistd.h" file.
syntex of getpass function:
char * getpass (const char *prompt)
Parameters:
prompt: string pointer to print while asking for Password
Return Value:
string pointer of password
Example:
output:
Enter Password:
heet
您的方法是正确的,但是您需要在输入密码时关闭终端回显:
为什么不直接使用
getc()
而不是getch()
?Your method is correct, however you'll need to turn off terminal echo while the password is being entered:
Instead of
getch()
, why not just usegetc()
instead?如果不需要移植到 Windows 上,您可以使用 ncurses.h,但这里有某种更“可移植”的版本:
如果不需要移植,我会向您指出 ncurses 解决方案
portablegetch.h
而c文件
whatever.c
应该适合你的问题。
希望有帮助。
You might use ncurses.h if it is not necessary to be portable onto Windows for that, but here is some kind of a more "portable" version:
If it is not necessery to be portable ill point you to a ncurses solution
portablegetch.h
And the c-file
whatever.c
That should fit to your problem.
Hope that helps.
只需复制这些片段并使用它即可。希望有帮助
Just copy these snippet and use it. Hope it helped
只需传递您想要设置密码的 char* 及其大小,该函数就会完成其工作
这是一个示例,在你的编译器上运行它
Just pass for it the char* that you want to set password in and its size and the function will do its job
This is an example, run it on your compiler
不幸的是,在 C 标准库中没有这样的现成函数。也许在第三方库中。
一种选择是使用 ANSI 转义序列将控制台中的背景色设置为前景色以隐藏密码。尝试此链接。
Unfortunately in the C standard library there is no such function out of the box. Maybe in third party library.
One option is use ANSI escape sequences to set the background color to foreground color in the console to conceal the password. Try this link.
通过扫描字符,您可以将其放入缓冲区。如果按下退格键,您还需要编写代码,并适当更正插入的密码。
这是我曾经用诅咒编写的代码。使用
gcc file.c -o pass_prog -lcurses
进行编译With scanning the characters you can take it into a buffer. Also you need to write code if backspace is pressed, and appropriately correct the inserted password.
Here is a code which once i wrote with the curses. Compile with
gcc file.c -o pass_prog -lcurses
在 C 中,您可以使用
getpasswd()
函数,该函数与 shell 中的stty
执行类似的操作,例如:这是使用
stty
的等效 shell 脚本>(更改tty
的设置):来源:如何读取密码而不在 C 中回显密码?
In C you can use
getpasswd()
function which pretty much doing similar thing asstty
in shell, example:Here is equivalent shell script which use
stty
(which changes the settings of yourtty
):Source: How can I read a password without echoing it in C?
,如果您注意到,大多数当前的 Linux 发行版都不会使用星号屏蔽密码。这样做会泄露密码的长度,这没有任何好处。当输入密码时,简单地让光标不移动会更容易、更好。如果出于某种原因,您需要为输入的每个字符打印
*
,那么您将必须抓住每个按键在按下Enter
之前,这总是有问题的。If you notice, most current linux distro's do not mask a password with asterisks. Doing so divulges the length of the password which is no way beneficial. It is easier and better to simply make the cursor not move when a password is typed in. If for whatever reason you require a
*
to be printed for every character that's typed then you would have to grab every keypress beforeEnter
is hit and that's always been problematic.这是我的想法,改编自C++ 官方网站。
它将一次读取一个字符并将其添加到字符串中,并支持显示另一个字符。
Here is my idea, adapted from that of the C++ official site.
It will read one character at once and add it to the string and supports showing another character.
请注意,ICANON termios lflag 会关闭处理回车/换行,负 ECHO termios 设置会关闭 STDIN 的回显。
当使用它(无论是否打开回显)读取密码并打印输入字符的
'*'
时,这不仅仅是读取字符直到遇到换行符/回车符的问题,您还必须在“字符串构建例程”中处理退格键(否则退格键最终会出现在实际字符串中,并且不会导致字符从其中删除,就像各种基于字符串的输入函数的情况一样)。在 DOS 中的 C 语言中使用 getch 也会发生同样的情况。这也会很高兴地返回
0x08
表示退格键(或 127 或您的特定操作系统用作退格键的任何内容),跟踪“不删除字符串开头之前”,替换“字符串的新结尾” string' 为 0 并将当前位置计数器向后移动 1(除非您位于位置 0)取决于程序员使用这些函数中的任何一个(甚至是 dos C 上的 getch)。
getpass()
并没有执行用户最初要求的操作,他想要 * 的(这仍然向站在他身后看着他的屏幕的人透露密码的长度,以及在终端的滚动缓冲区中(如果他在使用后没有关闭它)。但在“非封闭环境”中,没有 * 可能是一个更好的主意。note that the ICANON termios lflag turns off the processing carriagereturn/linefeed, and the negative ECHO termios setting turns off echo for STDIN.
when using this (with or without the echo being on) to read a password and print
'*'
for entered characters, it's not just a matter of reading characters until a newline/carriage return is encountered, you also have to process backspace in your 'string building routine' (else the backspaces end up in the actual string, and do not cause characters to be removed from it such as would be the case with the various string based input functions).the same would happen in C in DOS with getch tho. that would also happily return
0x08
for backspace (or 127 or whatever your specific os uses as backspace)keeping track of 'not deleting -before- the start of the string', replacing the 'new end of the string' with 0 and moving the current position counter back by one (unless you are at position 0) is up to the programmer with any of these functions (even the getch on dos C).
getpass()
doesn't do what the user originally asked for btw, he wants *'s (which still disclose the length of the password to people standing behind him and looking at his screen, as well as in the scrollbuffer of the terminal if he doesn't close it after use). but without *'s is probably a better idea in 'non closed environments'.