How to censor password?

Pages: 12
As an extra requirement from this assignment, I need to censor the password of this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  bool login()
{
	const int NumUser=3;
	string User[NumUser], Password[NumUser], UserEntry, PasswordEntry;
	User[0]="admin"; 
	Password[0]="admin1"; 
	User[1]="student2"; 
	Password[1]="stud2";
	User[2]="student3";
	Password[2]="stud2";

	for (int x=0; x<3; x++)
		{
			cout<<"Please enter your username: ";
			cin>>UserEntry;
			cout<<"Please enter your password: ";
			cin>>PasswordEntry;
			for (int y=0; y<NumUser; y++)
			{
			if (User[y].compare (UserEntry)==0 && Password[y].compare (PasswordEntry)==0)
				{
					return true;
				}
			}
			cout<<"Invalid username or password. \n\n";
		}
	cout<<"You have exceed the number of tries allowed.\n\n";
	system ("pause");
	return 0;
}


Please do give your guidance, thanks :D
I don't think it's possible just using the standard libraries.
You might be able to find something in here that will work for you, though:
http://www.cplusplus.com/articles/E6vU7k9E/
You could go out of the way and use ncurses/pdcurses.

It has a command called noecho(); whichs stops you text displaying on the terminal like on UNIX systems when entering the password.

This is a bit overkill for one thing however so it may not be the practicle situation.
Given that you're using system("pause");, it seems that you're using Windows. In that case, you can "hide" the password by doing
cout << string(300, '\n'); // Print 300 newlines
which should make the password scroll off of the console screen, unless you've changed the screen buffer size (and if that's the case, then change 300 to a suitably large number, say 10000).

Of course, that still doesn't make the password disappear as you're typing it.
Last edited on
How can I implement the passcode censoring into the code?
The only way is by turning off echo in the terminal, which requires OS-specific code.

It seems like an odd requirement for an assignment. What system or systems do you think your professor will use this code on? (Windows? Linux?)

Are you using a modern compiler or are you using the old Borland Turbo C++ compiler (meaning, does it work when you #include <conio.h>)?

[edit] I'm asking so I can give you the best answer.
Last edited on
Well it's an advanced requirement. Just fulfilling for extra marks XD I'm doing this on windows. I'm not using that compiler, just the usual iostream, iomanip, string and cmath and I don't think I will?
On Windows, toggle character display with:

1
2
#define NOMINMAX
#include <windows.h> 
1
2
3
4
5
6
7
8
9
void echo( bool on )
  {
  HANDLE hStdIn = GetStdHandle( STD_INPUT_HANDLE );
  DWORD mode;
  GetConsoleMode( hStdIn, &mode );
  mode &= ~ENABLE_ECHO_INPUT;
  if (on) mode |= ENABLE_ECHO_INPUT;
  SetConsoleMode( hStdIn, mode );
  }

This only works on Windows, of course, so if your professor tries to compile this under Unix it won't work -- or compile.
So where do i put them? i put the

1
2
#define NOMINMAX
#include <windows.h> 


at the top, but where do i put the rest?
I emplemented a teqnique for this. Basically, you get each character using getch(), which get's a single key press, but doesn't display it. You can use that to censor your display if you feel up to it.
eh well....
i need to know where i need to put them as well? >.<
One way to do it is to put

1
2
#define NOMINMAX
#include <windows.h> 

at the top where the rest of your #include s are, and then put

void echo(bool on);

somewhere above main() (doesn't really matter where), and then put the definition for that function where all of your other functions are (below the main function, presumably).
Stick the function anywhere in your code you want, just like any other function.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
using namespace std;

#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

void echo( bool on )
  {
  HANDLE hStdIn = GetStdHandle( STD_INPUT_HANDLE );
  DWORD mode;
  GetConsoleMode( hStdIn, &mode );
  mode &= ~ENABLE_ECHO_INPUT;
  if (on) mode |= ENABLE_ECHO_INPUT;
  SetConsoleMode( hStdIn, mode );
  }

int main()
  {
  string fake_password;
  cout << "Enter a (fake) password: ";

  echo( false );
  getline( cin, fake_password );
  cout << "\n";
  echo( true );

  cout << "Good job! You entered \"" << fake_password << "\".\n";
  return 0;
  }


@IWishIKnew
I already asked OP if she could use <conio.h> and the answer was (implicitly) no, but she was using Windows.

Hope this helps.
what is this <conio.h> suppose to do anyway? a classmate who figured out how to do this mentioned that, and something about using a while loop so every time the computer sense a character, they will automatically replace it with a * unless the entry is "enter" or sth like that?
The <conio.h> is a remnant of an ancient Microsoft header (which everyone mis-attributes to Borland) that provided functionality for playing with the console, just like you want. Unless you are using an old Borland compiler, though, it is unlikely to have very much functionality, if it exists at all.

One of the functions in that library allows you to get a single character of input at a time, just as the user presses the key. (This is called unbuffered or "raw" input.)

Stars/asterisks are pretty GUI stuff someone invented without realizing it was a bad idea. You are better off not using them.

If you must, though, google "msdn setconsolemode" to learn more about playing with stuff like unbuffered input (vs the default line-buffered input).

Your friends think they are being cooler, but they are not. Passwords should not show the person over your shoulder how long they are.
Gahhhh i swear the advance requirements are just weird. there are simpler ones like changing the colours, and ridiculously difficult ones like this one
You are seriously ahead of your class if you realize that.

All terminal I/O is ridiculously difficult. Heck, all normal I/O is itself much more difficult than people are led to believe. Playing with I/O == more grief than I really want to deal with.

It's the people who take the time to do it right that are smart. (Good job, then!)

Here's a simple '*'ing password reader I wrote using Windows API some time ago:
http://www.cplusplus.com/forum/general/3570/#msg15410

Most of that stuff is just dealing with Windows. The important part is the loop and checking for '\r' (ENTER) and '\b' (BACKSPACE) characters.

Hope this helps.

O.o im lost...
i'll try to digest it first haha >.<
Why not just do it correctly, without stars, and explain the reason to your professor. Wouldn't that be worth more points?
im suppose to just submit the cpp file onto an online site for the teacher to grade it >.<
Pages: 12