Hi,
can anybody Write a program for Login that is while enter the password it should be print as * to corresponding character?
Thanks in advance,
srinivas k
Printable View
Hi,
can anybody Write a program for Login that is while enter the password it should be print as * to corresponding character?
Thanks in advance,
srinivas k
Yes programmers can generally do that.Quote:
can anybody Write a program for Login
If you're writing for Windows....:
Code:char *getPass(char *szBuffer, size_t len)
{
size_t i;
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwOld, dwNumRead, dwNumWritten;
if((hIn != INVALID_HANDLE_VALUE) && (hOut != INVALID_HANDLE_VALUE))
{
if(GetConsoleMode(hIn,&dwOld))
{
if(SetConsoleMode(hIn,ENABLE_PROCESSED_INPUT))
{
for(i=0;i<len-1;i++)
{
if(ReadConsole(hIn,&szBuffer[i],1,&dwNumRead,NULL))
{
if(szBuffer[i] == '\r')
{
break;
}
else WriteConsole(hOut,"*",1,&dwNumWritten,NULL);
}
else break;
}
WriteConsole(hOut,"\n",1,&dwNumWritten,NULL);
szBuffer[i] = '\0';
SetConsoleMode(hIn,dwOld);
return szBuffer;
}
}
}
return NULL;
}
I've worked on this for days. The only way to get it to work was use the function above
Some said that if you're using visual studio you can just use getch() but I've never tried that.
I have tried using conio.h and getch() using GCC but they didn't work.
Do you really need to make it that portable? are you thinking of compiling it for anything other than windows?
No, this type of code will by it's nature use unportable functions. You may of course be able to use some sort of portable library to "hide" the unportable bits, e.g. ncurses or some such. But there are no standard C library functions to do this - there never has been, and there probably never will be, as there are certainly the possibility that some architectures don't even support this mode of operation.
--
Mats
Yes, that's not going to work - you WILL need some system specific code in there - there's no other way. This is because the C library doesn't have functions to turn off the echo (that is, there's no way to read characters from the keyboard without them also being printed to the screen), nor is there a way to "get one character at a time" in the standard library - it's all being processed by the OS into a line before it's being made available to the application. You need a way to bypass both the echo and the "hold it until a line is complee" to implement this, and there's just no way to do that without using system specific code. You can use third party libraries such as ncurses, but the standard C library will not do it. There's just no way.
--
Mats
Let's see if he gets it this time that it's not going to work.
To the OP: Your options are to either use a portable library that has separate implementations for most systems, to write your own, or to simply just use the one on the platform you need right now.
I've always wondered why there isn't a standard function to do this (like getch())? It certainly sounds like a fairly common thing that a lot of programs would need to do.
Probably because it's not guaranteed to be supported - and because once you get that far, you probably want some other control(s) over the input too, and this quickly gets system specific. There are libraries that support it this any much more, such as ncurses. It's just not part of the C standard.
--
Mats
No. Getchar reads a single character from the input stream - the printing is done by the underlaying OS's line input processing. Consider for example a teletype - it has no way to NOT print the character you entered [the way to "hide" passwords on a teletype is to read one char, then output "\10%\10*\10X\10\\\10$" or some such, so that the input gets thoroughly covered in ink]. Yes, I know, there aren't many teletypes around these days. There are, however, still some types of terminals [or emulators of such running on PC's] that do local line processing, and those would fall into the same criteria of not being able to do this.
--
Mats
That's not entirely true. In general, this is more how it actually works:
getchar() simply reads the char from the buffer of the FILE *. If you don't have anything in the buffer to read, the OS-specific read function is called. The printing of the char to the screen is NOT done by getchar(). That is done by your shell/OS read function/etc. etc..
The default behavior of your system is to echo chars from beyond stdin into the screen. Think about it for a second. The chars are being printed to the screen BEFORE it's even accepted by stdin (because stdin actually wraps the OS version of stdin).
Functions like getch() actually change the terminal settings so echoing is NOT done in this manner. On Windows this is done with SetConsoleMode() I believe. For *nix systems you have to similarly play with the settings and have it so the output is not echoed to stdout.
In fact, I think both Windows and *nix systems allow you to preserve the terminal settings. You might be able to write a program which changes your terminal mode, quits, and then you could start another program which uses the new terminal mode where getchar() would behave like getch(). ;)
Heres the program to accept a password.While typing the characters are masked using '*' character.It also accepts backspace.Displays the entered pw when user hits ENTER key
heres the program:
check out:
http://www.lern2hack.blogspot.com/20...d-mask-it.html
Oh look, sriki is back pushing his hacker wannabe site with dubious code full of mistakes.
Tell me, what happens when I type in 30000 characters into your "program" ?
More like a "how to write code which makes it easy to be hacked" site IMO.
Not to mention it's so poorly written. if (ch == 13) <--- OK, how the heck am I supposed what ASCII value 13 is?
sriki: It's fine to teach other how to do things, but please, avoid poor programming concepts and things. If you're going to teach someone, do it right! There's enough of bad programming examples out there - we don't need more of them!
That is horrible code.
What I posted should work on Windows. I believe someone might have posted something similar for *nix systems somewhere else here (in the C++ section perhaps).
Here's my version (based on MacGyver's getch() function):
Thanks to all the people who helped me out with it (from this forum)Code:/* prototypes */
char mygetch(void);
void getpassword(char password[], int maxlength);
/* input password from user */
void getpassword(char password[], int maxlength)
{
int i=0, done=0;
char c;
printf("Please input password (max. %d characters): ", maxlength);
fflush(stdout);
while (!done)
{
c = mygetch();
switch(c)
{
case '\r':
case '\n':
done = 1;
break;
case '\b':
if (i > 0) /* perform backspace only if characters have already been input */
{
printf("\b \b");
i--;
}
break;
default:
if (i<maxlength && isprint(c)) /* save character in password as long as it's not a none-printing character */
{
password[i++] = c;
putchar('*');
}
break;
}
fflush(stdout);
}
password[i] = '\0';
}
/* like getchar() except without echo */
char mygetch(void)
{
static HANDLE hIn;
static DWORD NumRead;
static INPUT_RECORD InRec;
static char c;
hIn = GetStdHandle(STD_INPUT_HANDLE);
if(hIn == INVALID_HANDLE_VALUE)
{
printf("GetStdHandle poop");
}
else
{
do
{
ReadConsoleInput(hIn, &InRec, 5, &NumRead);
if (InRec.EventType == KEY_EVENT)
{
if (InRec.Event.KeyEvent.bKeyDown == 1)
{
c = InRec.Event.KeyEvent.uChar.AsciiChar;
/* testing print
printf(" KDown Repeat KeyCode ScanCode Unicode Ascii Ctrl-State \n");
printf(" %3x %3x %3x %3x %3x %3x %3lx \n",
InRec.Event.KeyEvent.bKeyDown,
InRec.Event.KeyEvent.wRepeatCount,
InRec.Event.KeyEvent.wVirtualKeyCode,
InRec.Event.KeyEvent.wVirtualScanCode,
InRec.Event.KeyEvent.uChar.UnicodeChar,
InRec.Event.KeyEvent.uChar.AsciiChar,
InRec.Event.KeyEvent.dwControlKeyState );
*/
/* printf("|if %c %02x ", c, c); testing*/
break;
}
}
}
while( c != '\r' );
}
return c;
}
You probably meant to put maxlength there.Code:Error 1 error C2065: 'KEYLENGTH' : undeclared identifier
sorry Elysia, but the function itself is the harder part. I assumed you can just make the prototype from the function.
About the KEYLENGTH error, I'm sorry it was supposed to be maxlength.
I edited the code anyway
Sure, I suppose you can ignore the prototypes, I guess.
And I did mention that it depends on MacGyver's getch() function, which is already known to be Windows specific and to require windows.h