C++ 从 std::cin 读取密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1413445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading a password from std::cin
提问by Vargas
I need to read a password from standard input and wanted std::cin
not to echo the characters typed by the user...
我需要从标准输入中读取密码,并且不想std::cin
回显用户输入的字符...
How can I disable the echo from std::cin?
如何禁用 std::cin 的回声?
here is the code that I'm currently using:
这是我目前使用的代码:
string passwd;
cout << "Enter the password: ";
getline( cin, passwd );
I'm looking for a OS agnostic way to do this. Herethere are ways to do this in both Windows and *nix.
我正在寻找一种与操作系统无关的方式来做到这一点。 这里有一些方法可以在 Windows 和 *nix 中执行此操作。
回答by Vargas
@wrang-wrang answer was really good, but did not fulfill my needs, this is what my final code (which was based on this) look like:
@wrang-wrang 答案非常好,但没有满足我的需求,这就是我的最终代码(基于此)的样子:
#ifdef WIN32
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#endif
void SetStdinEcho(bool enable = true)
{
#ifdef WIN32
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
if( !enable )
mode &= ~ENABLE_ECHO_INPUT;
else
mode |= ENABLE_ECHO_INPUT;
SetConsoleMode(hStdin, mode );
#else
struct termios tty;
tcgetattr(STDIN_FILENO, &tty);
if( !enable )
tty.c_lflag &= ~ECHO;
else
tty.c_lflag |= ECHO;
(void) tcsetattr(STDIN_FILENO, TCSANOW, &tty);
#endif
}
Sample usage:
示例用法:
#include <iostream>
#include <string>
int main()
{
SetStdinEcho(false);
std::string password;
std::cin >> password;
SetStdinEcho(true);
std::cout << password << std::endl;
return 0;
}
回答by Jonathan Graehl
There's nothing in the standard for this.
标准中没有任何内容。
In unix, you could write some magic bytes depending on the terminal type.
在 unix 中,您可以根据终端类型编写一些魔术字节。
Use getpasswdif it's available.
如果可用,请使用 getpasswd。
You can system() /usr/bin/stty -echo
to disable echo, and /usr/bin/stty echo
to enable it (again, on unix).
您可以 system()/usr/bin/stty -echo
禁用回声,并/usr/bin/stty echo
启用它(再次,在 unix 上)。
This guy explainshow to do it without using "stty"; I didn't try it myself.
这家伙解释了如何在不使用“stty”的情况下做到这一点;我自己没试过。
回答by AraK
If you don't care about portability, you can use _getch()
in VC
.
如果你不关心便携性,你可以使用_getch()
in VC
。
#include <iostream>
#include <string>
#include <conio.h>
int main()
{
std::string password;
char ch;
const char ENTER = 13;
std::cout << "enter the password: ";
while((ch = _getch()) != ENTER)
{
password += ch;
std::cout << '*';
}
}
There is also getwch()
for wide characters
. My advice is that you use NCurse
which is available in *nix
systems also.
还有getwch()
对于wide characters
. 我的建议是,您NCurse
也可以使用*nix
系统中提供的内容。
回答by IProblemFactory
Only idea what i have, you could read password char by char, and after it just print backspace ("\b") and maybe '*'.
只知道我有什么,您可以逐个字符读取密码字符,然后只打印退格键(“\b”)和“*”。