954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Masking user input (password)

i have ceated a file in C. i have to give the login security. i already gave the user name & password. but when i type the password the letters are visible how to hide the letters

kavaas
Newbie Poster
2 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

What operating system are you on?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
What operating system are you on?

windows xp

kavaas
Newbie Poster
2 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

There is no standard way to do that

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

When you type characters into the console, its going to STDIN.
you're wanting to modify the character value coming from the keyboard.
There are Windows API functions available for this. off the top of my head i can't remember what they are. MSDN would be your best bet for this.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

This is unportable... but you might be able to use...

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1045691686&id=1043284392

For other OS etc the following might also interest you...

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1042856625&id=1043284385

From that the following seems to work in linux...

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <ctype.h>

int mygetch ( void ) 
{
  int ch;
  struct termios oldt, newt;
  
  tcgetattr ( STDIN_FILENO, &oldt );
  newt = oldt;
  newt.c_lflag &= ~( ICANON | ECHO );
  tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
  ch = getchar();
  tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
  
  return ch;
} 

int main()
{
  
  int ch;
  char pword[BUFSIZ];
  int i = 0;
  
  puts ("Enter your password");
  fflush(stdout);
  
  while ((ch = mygetch()) != EOF 
          && ch != '\n' 
          && ch != '\r' 
          && i < sizeof(pword) - 1)
  {
    if (ch == '\b' && i > 0) 
    {
      printf("\b \b");
      fflush(stdout);
      i--;
      pword[i] = '\0';
    }
    else if (isalnum(ch))
    {
      putchar('*');
      pword[i++] = (char)ch;
    }
  }

  pword[i] = '\0';
  
  printf ("\nYou entered >%s<", pword);
  
  return 0;
}

Although the backspace don't work, might be a way round that.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

hmmm it would appear

if (ch == 0x7f && i > 0) 
    {
      printf("\b \b");
      fflush(stdout);
      i--;
      pword[i] = '\0';
    }

Allows the backspace to be recognised but I'm not sure how reliable it is?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You