Password Masking [Help]

Hey I need help with password masking. I need some code to do this following code exactly but without using _getch()... i don't have <conio.h> so I cant use it.

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
#include <conio.h>
using namespace std;
int main(){
   string pass ="";
   char ch;
   cout << "Enter pass\n";
   ch = _getch();
   while(ch != 13){//character 13 is enter
      pass.push_back(ch);
      cout << '*';
      ch = _getch();
   }


I'm using g++ compiler, with Windows 7...
Last edited on
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;


string getpass(const char *prompt, bool show_asterisk=true)
{
  const char BACKSPACE='\b';
  const char RETURN='\r';

  string password;
  unsigned char ch=0;

  cout <<prompt<<endl;

  DWORD con_mode;
  DWORD dwRead;

  HANDLE hIn=GetStdHandle(STD_INPUT_HANDLE);

  if(hIn==INVALID_HANDLE_VALUE || hIn==NULL)
    {
        cout <<"GetStdHandle error. GetLastError() returned error code "<<GetLastError()<<endl;
        return "";
    }


  if(GetConsoleMode( hIn, &con_mode )==0)
    {
        cout <<"GetConsoleMode error. GetLastError() returned error code "<<GetLastError()<<endl;
        return "";
    }

  if(SetConsoleMode( hIn, con_mode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT) )==0)
    {
        cout <<"SetConsoleMode error. GetLastError() returned error code "<<GetLastError()<<endl;
        return "";
    }
  while(ReadConsoleA( hIn, &ch, 1, &dwRead, NULL) && ch !=RETURN)
    {
       if(ch==BACKSPACE)
         {
            if(password.length()!=0)
              {
                 if(show_asterisk)
                     cout <<"\b \b";
                 password.resize(password.length()-1);
              }
         }
       else
         {
             password+=ch;
             if(show_asterisk)
                 cout <<'*';
         }
    }
  cout <<endl;
  return password;
}


int main()
{
  const string correct_password="null";

  string password=getpass("Please enter the password: ",true);
  if(password==correct_password)
      cout <<"Correct password"<<endl;
  else
      cout <<"Incorrect password. Try again"<<endl;

  return 0;
}
Last edited on
It isn't letting me enter password using that code Null, thanks for the help though!

Here's an example of my output:
Please enter the password: 

Incorrect password. Try again
Hmm, that's weird, it works fine for me. What version of g++ are you using?
EDIT: added error checking routines to my code. Try running it again and see if it reports any errors
Last edited on
Hmm now i'm getting this:
Please enter the password: 
GetConsoleMode error. GetLastError() returned error code 6
Incorrect password. Try again


I'm not real sure what version of g++ i'm using, I downloaded it from cygwin less than 2 months ago.

6 The handle is invalid. ERROR_INVALID_HANDLE


http://help.netop.com/support/errorcodes/win32_error_codes.htm
Last edited on
I think there's a problem with cygwin g++. I've compiled it with cygwin and it didn't work for me too but it works when compiled with mingw-g++
Where can I download mingw-g++ for free?
I recommend you to download from here:
http://tdragon.net/recentgcc/
Everything works fine with the mingw compiler, thanks for the help!
Topic archived. No new replies allowed.