locked
how to mask the console password input? RRS feed

  • Question

  • Hi All,

    I like to mask the inputted passwords in my console application, so I tried to test the following code, from here: http://www.cplusplus.com/forum/unices/27735/ , to use it on my application, but it's not working correctly because when I press enter there is no return to line and the application receive the enter key press as an input character and displays it with a '*'.

    so how to correct that code, to make it working properly?

    thanks.


    • Edited by Yuri.B Thursday, October 17, 2013 8:30 PM
    Thursday, October 17, 2013 8:16 PM

Answers

  • This example is working, but , it doesn't support both of enter-key-press and backspace-key-press,

    so how to detect enter key and backspace key,


    By doing some programming?

    The first steps at least should be obvious:

    (1) Get the character into a variable. (Note that _getch() returns an int.)

    (2) Check the character to see if it needs any special handling.

    (3) If it's a backspace: erase the asterisk on screen from the last character,
    decrement the count of characters accepted, strip that last character from the
    password string, and iterate again.

    (4) If it's an Enter key: do whatever you want to handle that.

    (5) Extended keys such as F1. F2, etc. consist of two parts - the first being zero.
        So for those keys a second _getch() is needed.
        
    Then the enhanced example will look something like this:

    #include "stdafx.h"
    #include <conio.h>
    #include <string>
    #include <iostream>
    
    void MyExit() {system("pause");}
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      atexit(MyExit);
      std::string psswd;
      std::cout << "Enter a 6 digit password: ";
      for(int i=0; i<6; ++i)
        {
        int ch = _getch();
        if(ch == 0x08) // backspace
          {
          if(i > 0)
            {
            _putch('\b');
            _putch(' ');
            _putch('\b');
            psswd.erase(psswd.length()-1);
            i -= 2;
            }
          else --i;
          continue;
          }
        if(ch == 0x0d) // Enter
          {
          // do whatever ...
          std::cout << "\nToo few characters entered!\n";
          return -1;
          }
        if(ch == 0x00) // Extended key pressed (F1, F2, etc.)
          {
          // extended keys have two parts ...
          _getch(); // ignore it
          --i;
          continue;
          }
    
        psswd += ch;
        _putch('*');
        }
      std::cout << "\nYou entered: " << psswd << std::endl;
      return 0;
    }
    

    - Wayne


    • Edited by WayneAKing Friday, October 18, 2013 7:11 PM Accept backspace as first char
    • Marked as answer by Yuri.B Saturday, October 19, 2013 12:57 PM
    Friday, October 18, 2013 6:51 PM
  • Note: I have amended my last example to allow for a backspace as the first character entered.

    - Wayne

    • Marked as answer by Yuri.B Saturday, October 19, 2013 12:57 PM
    Friday, October 18, 2013 7:12 PM
  • Note that handling additional special keys such as F11, F12, PageDwn, PageUp, requires
    checking for a 0xE0 - these are two-part codes as well. For example:

    if(ch == 0x00 || ch == 0xe0) // Extended key pressed (F1, F2, etc. or F11, F12, etc.)
      {
      // extended keys have two parts ...
      _getch(); // ignore it
      --i;
      continue;
      }
    

    If you want to handle the Esc(ape) key specially:

    if(ch == 0x1b) // ESC
      {
      std::cout << "\nAborted!\n";
      return 0;
      }
    

    - Wayne

    • Marked as answer by Yuri.B Saturday, October 19, 2013 12:57 PM
    Friday, October 18, 2013 7:57 PM

All replies

  • Consider an alternative:

    #include "stdafx.h"
    #include <conio.h>
    #include <string>
    #include <iostream>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      std::string psswd;
      std::cout << "Enter a 6 digit password: ";
      for(int i=0; i<6; ++i)
        {
        psswd += _getch();
        _putch('*');
        }
      std::cout << "\nYou entered: " << psswd << std::endl;
      system("pause");
      return 0;
    }
    

    - Wayne

    • Proposed as answer by Bordon Friday, October 18, 2013 4:29 AM
    • Marked as answer by Yuri.B Friday, October 18, 2013 3:41 PM
    • Unmarked as answer by Yuri.B Friday, October 18, 2013 4:41 PM
    Thursday, October 17, 2013 10:36 PM
  • Consider an alternative:

    #include "stdafx.h"
    #include <conio.h>
    #include <string>
    #include <iostream>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      std::string psswd;
      std::cout << "Enter a 6 digit password: ";
      for(int i=0; i<6; ++i)
        {
        psswd += _getch();
        _putch('*');
        }
      std::cout << "\nYou entered: " << psswd << std::endl;
      system("pause");
      return 0;
    }

    - Wayne

    This example is working, but , it doesn't support both of enter-key-press and backspace-key-press,

    so how to detect enter key and backspace key,

    thanks.

    Friday, October 18, 2013 4:45 PM
  • This example is working, but , it doesn't support both of enter-key-press and backspace-key-press,

    so how to detect enter key and backspace key,


    By doing some programming?

    The first steps at least should be obvious:

    (1) Get the character into a variable. (Note that _getch() returns an int.)

    (2) Check the character to see if it needs any special handling.

    (3) If it's a backspace: erase the asterisk on screen from the last character,
    decrement the count of characters accepted, strip that last character from the
    password string, and iterate again.

    (4) If it's an Enter key: do whatever you want to handle that.

    (5) Extended keys such as F1. F2, etc. consist of two parts - the first being zero.
        So for those keys a second _getch() is needed.
        
    Then the enhanced example will look something like this:

    #include "stdafx.h"
    #include <conio.h>
    #include <string>
    #include <iostream>
    
    void MyExit() {system("pause");}
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      atexit(MyExit);
      std::string psswd;
      std::cout << "Enter a 6 digit password: ";
      for(int i=0; i<6; ++i)
        {
        int ch = _getch();
        if(ch == 0x08) // backspace
          {
          if(i > 0)
            {
            _putch('\b');
            _putch(' ');
            _putch('\b');
            psswd.erase(psswd.length()-1);
            i -= 2;
            }
          else --i;
          continue;
          }
        if(ch == 0x0d) // Enter
          {
          // do whatever ...
          std::cout << "\nToo few characters entered!\n";
          return -1;
          }
        if(ch == 0x00) // Extended key pressed (F1, F2, etc.)
          {
          // extended keys have two parts ...
          _getch(); // ignore it
          --i;
          continue;
          }
    
        psswd += ch;
        _putch('*');
        }
      std::cout << "\nYou entered: " << psswd << std::endl;
      return 0;
    }
    

    - Wayne


    • Edited by WayneAKing Friday, October 18, 2013 7:11 PM Accept backspace as first char
    • Marked as answer by Yuri.B Saturday, October 19, 2013 12:57 PM
    Friday, October 18, 2013 6:51 PM
  • Note: I have amended my last example to allow for a backspace as the first character entered.

    - Wayne

    • Marked as answer by Yuri.B Saturday, October 19, 2013 12:57 PM
    Friday, October 18, 2013 7:12 PM
  • Note that handling additional special keys such as F11, F12, PageDwn, PageUp, requires
    checking for a 0xE0 - these are two-part codes as well. For example:

    if(ch == 0x00 || ch == 0xe0) // Extended key pressed (F1, F2, etc. or F11, F12, etc.)
      {
      // extended keys have two parts ...
      _getch(); // ignore it
      --i;
      continue;
      }
    

    If you want to handle the Esc(ape) key specially:

    if(ch == 0x1b) // ESC
      {
      std::cout << "\nAborted!\n";
      return 0;
      }
    

    - Wayne

    • Marked as answer by Yuri.B Saturday, October 19, 2013 12:57 PM
    Friday, October 18, 2013 7:57 PM
  • thank you very much!
    Saturday, October 19, 2013 12:57 PM