Home > Backend Development > C++ > How Can I Disable Echo in C 's `std::cin` for Secure Password Input?

How Can I Disable Echo in C 's `std::cin` for Secure Password Input?

Barbara Streisand
Release: 2024-12-21 09:06:11
Original
404 people have browsed it

How Can I Disable Echo in C  's `std::cin` for Secure Password Input?

Disabling Echo in std::cin for Password Input

In scenarios where sensitive input like passwords needs to be protected from echoing, it becomes crucial to suppress character visibility during user input. This article delves into OS-agnostic methods for disabling echo in std::cin, a fundamental standard input stream in C .

Consider the following code snippet:

string passwd;
cout << "Enter the password: ";
getline(cin, passwd);
Copy after login

This code prompts the user to enter a password but visibly echoes the typed characters, rendering the input vulnerable to potential eavesdropping. To address this issue, the below solutions offer secure ways to conceal password characters during input.

Windows:

For Windows systems, the SetConsoleMode function provides control over standard input echoing. Passing ENABLE_ECHO_INPUT as a flag enables echo, while its negation disables it, ensuring that passwords remain hidden during input.

HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
mode &= ~ENABLE_ECHO_INPUT;
SetConsoleMode(hStdin, mode);
Copy after login

POSIX:

On POSIX-compliant systems, the tcgetattr and tcsetattr functions are employed to manage the echo setting for stdin. Unsetting the ECHO flag suppresses echoing, allowing for secure password input.

struct termios tty;
tcgetattr(STDIN_FILENO, &tty);
tty.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &tty);
Copy after login

Example Usage:

An example implementation incorporating both methods:

#ifdef WIN32
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#endif

void SetStdinEcho(bool enable = true)
{
#ifdef WIN32
    // Windows implementation
#else
    // POSIX implementation
#endif
}

int main()
{
    SetStdinEcho(false);
    string password;
    cin >> password;
    SetStdinEcho(true);
    cout << password << endl;
    return 0;
}
Copy after login

By utilizing these techniques, developers can effectively enhance password security by preventing echoing, ensuring a more secure user input experience for sensitive information retrieval.

The above is the detailed content of How Can I Disable Echo in C 's `std::cin` for Secure Password Input?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template