0% found this document useful (0 votes)
247 views2 pages

C++ Login System with Hidden Passwords

The document contains code for a C++ login program with 3 examples: 1) A basic program that gets username and password input and checks it against hardcoded values. 2) A version that hides the password input for Windows by disabling echoing to the console. 3) A version that hides the password input for Unix systems by manipulating terminal attributes to disable echoing instead of using Windows APIs.

Uploaded by

otikokakhidze58
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
247 views2 pages

C++ Login System with Hidden Passwords

The document contains code for a C++ login program with 3 examples: 1) A basic program that gets username and password input and checks it against hardcoded values. 2) A version that hides the password input for Windows by disabling echoing to the console. 3) A version that hides the password input for Unix systems by manipulating terminal attributes to disable echoing instead of using Windows APIs.

Uploaded by

otikokakhidze58
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1 one:

#include <iostream>
#include <string>

using namespace std;

int main() {
// Hardcoded username and password (replace these with your actual
authentication mechanism)
const string correctUsername = "user";
const string correctPassword = "pass";

string enteredUsername;
string enteredPassword;

// Get user input for username


cout << "Enter username: ";
cin >> enteredUsername;

// Get user input for password


cout << "Enter password: ";
cin >> enteredPassword;

// Check if the entered credentials are correct


if (enteredUsername == correctUsername && enteredPassword == correctPassword) {
cout << "Login successful. Welcome, " << enteredUsername << "!" << endl;
} else {
cout << "Login failed. Incorrect username or password." << endl;
}

return 0;
}

for hidden password:

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main() {
const string correctUsername = "user";
const string correctPassword = "pass";

string enteredUsername;
string enteredPassword;

cout << "Enter username: ";


cin >> enteredUsername;

cout << "Enter password: ";


HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
cin >> enteredPassword;

// Restore console mode


SetConsoleMode(hStdin, mode);

if (enteredUsername == correctUsername && enteredPassword == correctPassword) {


cout << "Login successful. Welcome, " << enteredUsername << "!" << endl;
} else {
cout << "Login failed. Incorrect username or password." << endl;
}

return 0;
}

for unix

<ScrollWheelDown>#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>

using namespace std;

int main() {
const string correctUsername = "user";
const string correctPassword = "pass";

string enteredUsername;
string enteredPassword;

cout << "Enter username: ";


cin >> enteredUsername;

cout << "Enter password: ";

termios oldt;
tcgetattr(STDIN_FILENO, &oldt);
termios newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);

cin >> enteredPassword;

// Restore terminal settings


tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

if (enteredUsername == correctUsername && enteredPassword == correctPassword) {


cout << "Login successful. Welcome, " << enteredUsername << "!" << endl;
} else {
cout << "Login failed. Incorrect username or password." << endl;
}

return 0;
}

You might also like