Thread: Password field with: **********

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    Question Password field with: **********

    hi

    I want to create a login for a simple C aplication, and I'm wondering if it's possible to hide the password caracters by replacing them with ******* ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's compiler and environment specific. Search the forum for the phrase "password" and you should get lots of results.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    humm...
    OK!
    tnx quzah, that's what i'm going to do :-)

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The old Borland & Turbo C versions had a header file named "conio.h". In that file they had a function named "getch()" which was perfect for this purpose. It did not echo the key that was pressed, onto the screen at all.

    Although out of date, they old Borland C versions are available from for download from their archive site. Just google and go.

    Without that conio.h file, something quite similar can be done. Just have something like this:

    Edit: Sorry this was used in another language, with unbuffered input, not C -

    Code:
    while((c = getchar()) != '\n')  {
      putchar('*');
      /* add code here to append the char to the password string */
       
    }
    The char is printed to the screen, but it's overwritten so fast, you can't see it. (If it's for anything serious, note that it might be visible on a frame by frame view from a video camera.)

    Adak
    Last edited by Adak; 04-23-2007 at 10:06 PM.

  5. #5
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hey Adak!
    tnx for the reply...

    i'm going to try that. i'll keep you posted

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I have my reservations about that suggestion. The value of the key pressed should be echoed to stdout before the call to getchar returns in the first place.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by citizen View Post
    I have my reservations about that suggestion. The value of the key pressed should be echoed to stdout before the call to getchar returns in the first place.
    My apologies - you're quite right, Citizen. I forgot I programmed that "replacement of char with a star" program, in another language.

    The C version program I coded was using Borland/Turbo C, and used conio.h.

    Thanks for the post, Citizen.

    Adak

  8. #8
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I doubt this would work, but what if you temporarily redirected stdout to some void while you read the character? Would that prevent the character from being echoed back onto stdout, or is it still engrained on a more lower level somewhere?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What you're thinking of is probably not safe and too difficult when working compiler/OS specific alternatives exist. I'd sooner secretly write black characters on black background before considering that.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It won't work anyway. You can test it by simply calling fclose on the output stream and you'll see that it still echo your input.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    yep!
    doesn't work

    Code:
    while((c = getchar()) != '\n')  {
      putchar('*');
      /* add code here to append the char to the password string */
       
    }
    i was talking about Adak's example code...
    Last edited by IndioDoido; 04-24-2007 at 05:31 AM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe you could post some code - since you haven't done so yet.

    All this "it doesn't work" is no use to figuring out all the possible ways you could have screwed up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yeah, sorry to have lead you astray with that earlier post, Indio.

    Code:
    /* Password.c by Adak */
    
    #include <stdio.h>
    #include <conio.h>  	/* for getch() */ 
    
    #define MaxChars 20
    
    char c;
    char Usrname[MaxChars] = { '\0' };
    char Passwrd[MaxChars] = { '\0' };
    
    int main()  {
       int i = 0;
       printf("\n\n Enter Username: ");
       fgets(Usrname, MaxChars, stdin);
    
       printf("\n Enter Password:");
       putchar(' ');
       while((c = getch()) != 13) {   /* '\n' doesn't work here */
          putchar('*');
          if (i < MaxChars - 1)
            Passwrd[i++] = c;
       }  
       Passwrd[i] = '\0';
    
       /* display it */
       printf("\n\n       Username: %s", Usrname);
       printf("\n       Password: %s", Passwrd);
       printf("\n\n\t\t     Program Complete, Please Press Enter "); 
       fseek(stdin, 0, SEEK_END);
       c = getchar();
       return(0);
    }
    The fseek() code is just to move the pointer in the keyboard buffer down to the end. Otherwise I have to do silly stuff like:

    Code:
    getchar();
    getchar();
    
    or
    getchar();
    while((c = getchar()) != '\n');
    And if the password was shorter than the allocated memory, it worked one way, and when the password that was keyboarded in was longer than the number of char's in the password array, it worked another (not due to a buffer overrun, just more keys stored in the keyboard buffer).

    So I got tired of that crap.

    The fseek() above handles both instances, as I want them - just the same, and just one getchar() needed.

    Above program runs OK in a console window of Windows2000. Haven't tried it on an XP box.

    Adak

  14. #14
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hey Adak, no problem.

    your most recent code works in windows vista x64

    but there's a little problem...if i want to use the backspace to erase the password, it puts more caracters to the field.

    i don't know if this is a vista x64 issue or just a minor code issue. Can you try using the backspace to see if it's from my OS or the code?

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You'll have to hardcode any kind of rubout (backspace, delete) that you want, because the input isn't line buffered and the terminal can't take care of it for you. In general this means that you have to find the key pressed, print it to stdout to move the cursor, print spaces, and move the cursor back.
    Code:
    if( c == '\b' ) {
       printf( "\b\b \b\b" );
       /* manage your buffers: you wouldn't want to keep old data there */
       pwd[ --pwdcount ] = '\0';
    }
    Something like that... depends on what you really have in terms of storage or code.
    Google the ASCII character set, implement what you need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 03:42 AM
  2. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  3. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  4. Password Field
    By Explicit in forum C Programming
    Replies: 10
    Last Post: 05-22-2004, 08:22 PM
  5. password field
    By trekker in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-27-2003, 01:56 AM