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 ******* ?
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 ******* ?
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.
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 -
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.)Code:while((c = getchar()) != '\n') { putchar('*'); /* add code here to append the char to the password string */ }
Adak
Last edited by Adak; 04-23-2007 at 10:06 PM.
hey Adak!
tnx for the reply...
i'm going to try that. i'll keep you posted
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.
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?
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.
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.
yep!
doesn't work
i was talking about Adak's example code...Code:while((c = getchar()) != '\n') { putchar('*'); /* add code here to append the char to the password string */ }
Last edited by IndioDoido; 04-24-2007 at 05:31 AM.
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.
Yeah, sorry to have lead you astray with that earlier post, Indio.
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:/* 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); }
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).Code:getchar(); getchar(); or getchar(); while((c = getchar()) != '\n');
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
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?
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.
Something like that... depends on what you really have in terms of storage or code.Code:if( c == '\b' ) { printf( "\b\b \b\b" ); /* manage your buffers: you wouldn't want to keep old data there */ pwd[ --pwdcount ] = '\0'; }
Google the ASCII character set, implement what you need.