Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

password mask implemented in c program

Dear all,

I have a c program that accpts a sequence of chars from UNIX prompt for user
to enter password

char identifier [21];
fgets(identifier, 21, stdin);

However I want when the user type his/her password in the prompt

'*' will appear instead of echo the things user type.

Is there any code can do the above?

Thank you very much.

Regards,
Ginrai
0
ginrai
Asked:
ginrai
1 Solution
 
akshayxxCommented:
have a look at this .. this doesnt echo * .. but it disables echoing
it may not be a standard function these days
http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?getpasswd+S
0
 
grg99Commented:
Depends on the system you are doing this on.

On Unix, it might work to do a system( "stty -echo" ) to turn off character echoing.  

Oh, and if you want to echo a "*" in real-time, you'll also need system( "stty -raw" ) or somesuch so you get each character as typed.

This is a big nasty as you have to undo these when your program exits.  If your program bombs, you'd better have an exit procedure or try...except block to catch the error and reset things.

Regards,

grg99
0
 
ExceterCommented:
>> Is there any code can do the above?

Try this,

char pass[25];
input( pass, 25 );

...

void input( char* buffer, int length )
{
    int pos = 0;
    char c;
 
    buffer[0] = '\0';

    while( ( c = getch() ) != 13 )
    {
        if( c != 8 && pos < ( length - 1 ) )
        {
            printf("*");
            buffer[pos] = c;
            pos++;
        }
        else if( pos != 0 )
        {
            printf("%c %c",8,8);
            pos--;
        }
    }
     buffer[pos] = '\0';
}

Exceter
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ExceterCommented:
>> else if( pos != 0 )

Sorry, this line should be,

else if( pos != 0 && c == 8 )

Exceter
0
 
ginraiAuthor Commented:
Dear Exceter,

I've tried the code but it does not work, it still echo the character I typed and put some * after I press enter.

Regards,
Ginrai
0
 
ExceterCommented:
>> I've tried the code but it does not work, it still echo the character I typed and put some * after I press enter.

That is REALLY weird. The code works perfectly on my computer. What OS are you running? Which compiler do you have?

Try this demo program,

#include <stdio.h>
#include <conio.h>
#define STRLEN 25

void input( char*, int );

int main()
{
     char s[STRLEN];
     printf("Enter password: ");
     input(s,STRLEN);
        printf("\n\n%s\n",s);
     return 0;
}

void input( char* buffer, int length )
{
   int pos = 0;
   char c;

   buffer[0] = '\0';

   while( ( c = getch() ) != 13 )
   {
       if( c != 8 && pos < ( length - 1 ) )
       {
           printf("*");
           buffer[pos] = c;
           pos++;
       }
       else if( pos != 0 && c == 8 )
       {
           printf("%c %c",8,8);
           pos--;
       }
   }
    buffer[pos] = '\0';
}

Exceter
0
 
ginraiAuthor Commented:
Thank you, Exceter
0
 
ExceterCommented:
Why the B?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now