how to display * for input password
| nicvyn (1) | |||
| how to display * when user is require to input the password? | |||
| Duoas (1596) | |||
| That is a system-dependant thing to do. What OS are you using? If you want something more platform-independant, check out NCurses (unix) and PDCurses (windows). | |||
| Mitsakos (343) | |||
I did it like that:
Isn't it right? | |||
| exception (321) | |||
It's a MS-Dos/Windows only solution, since _getch() is defined in conio.h which is MS specific. Plus, it isn't secure. Now the question is: should it be? Or should it just look neat? If it really is a password input, it certainly should be. In which case you need platform specific functions anyways (but not _getch()). Read the documentation of your OS-API for details. | |||
| gAaRa (40) | |||
Could you please explain me that part of your code as i am working on a similar program and thought why not ask in this thread rather than creating a new one. And one more thing. I work on Turbo C++. | |||
| Mitsakos (343) | |||
| If conio.h is only from MS i don't know if it will work with Turbo C++. But anyway here is how I thought about it. the
_getch() function wait's for an input from the keyboard but doesn't show the key to the console.
ch = _getch(); Reads a keystoke and saves it to the ch, but doesn't display it in the console.
while(ch != 13){ As long as the user doesn't press Enter (enter is ASCII code 13) continue reading keystrokes from the screen.
pass.push_back(ch); The pass is a string variable. The push_back() method puts the character specified as parameter at the end of the string.http://www.cplusplus.com/reference/string/string/push_back.html
cout << '*'; Since nothing has been displayed from the keystroke we display an asterisk ( '*' ) for every key.
ch = _getch(); You read another keystroke to continue reading keystrokes from the user.I don't know if you need anything else. Hope this helps. | |||
| exception (321) | |||
| conio.h should be included in the OS API, as far as I know, so work under any compiler in Windows. | |||
| Duoas (1596) | |||
| conio.h is an old Borland extention for Turbo C/C++ (which only works on 16-bit DOS and emulators), and is very much deprecated in modern compilers. If you know you will be sticking to Windows development, you can change the console input mode to do your bidding. Also, stay away from directly coding ASCII values. It probably won't hurt, but it could someday... so it is considered bad practice.
This is not secure. A keylogger can spy on what you type. (This is, sadly, fairly normal. I don't know of any specific Win32 API function that will securely read a password, but I know that the OS has one in there somewhere...) Hope this helps. [edit] Fixed a bug. | |||
| Incubbus (29) | |||
| as far as i know a keylogger will ALWAYS catch ur input... it may simulate the driver... | |||
| Duoas (1596) | |||
| If you've let a keylogger get that kind of access to your system, then yes, you are royally screwed. But for lesser softwares you can disable all hooks, etc. to prevent peeking. Failure to do so means even low-access software can hook and peek. Alas. | |||
| FindSyntax (7) | |||
| Hi, I am Rammohan from Bangalore and working as a Technical lead in big IT firm . Solution for your answer is follows: I believe you are a student and so I have written the following code to compile in TurboC. Since you people are using Turboc in your institutes. If you want the solution to work for other compiler or any other platform also please ask me and I will definitely send you in my way of code to work in all the platforms… #include<iostream.h> #include<conio.h> void main() { clrscr(); cout<<"Enter password: \n"; restart:int x[100],x1[100],i,j=0,k=0,l; for(i=0;i<=100;i++) { Rammohan:l=getch(); if(((l>=48)&&(l<=126))||(l==8)||(l==13)) x[i]=l; else goto Rammohan; if(x[i]==13) break; else if(x[i]==8) { gotoxy(1,2); clreol(); for(i=0;i<100;i++) x[i]='\0'; goto restart; } else { cout<<"*"; k++; } } cout<<"\nRe enter password: \n"; for(i=0;i<=k;i++) { x1[i]=getche(); if(x1[i]==13)break; } for(i=0;i<=k;i++) if(x[i]!=x1[i]) j++; if(j==0) cout<<"\nPasswords match!!\n"; else cout<<"\nPasswords do not match!!\n"; getch(); } ____________________ Regards, Rammohan Alampally, rammohan@india.com Technical Lead, Bangalore, India. www.FindSyntax.com -> Worlds first Web based Windows O/S is a Brain Chaild of Rammohan | |||
| Incubbus (29) | |||
| omg... may u use the [code]-brackets plz... mr-big-it ... nice english :P... | |||
| Duoas (1596) | |||
| *sigh* | |||
| pablo (1) | |||
| nice do someone know solution to hide entered character to console in linux? Rammohan, could you help me with this? ;) | |||
| Duoas (1596) | |||
| Whether in Windows or Linux, you still need to set the keyboard to no-echo, unbuffered input. Here's an example of how to do that in linux: http://www.cplusplus.com/forum/beginner/1988/page4.html#msg14522 Once you are in raw mode, use the same method as above to get the input and display *s. Good luck! | |||
| rajenipcv (8) | |||
| |||
| gAaRa (40) | |||
Well i wrote this code based on the codes posted above.
Well the bizarre problem is that if the starting character of the password is 'a' the the output statement "cout<<pass" shows a smiley face (maybe some ASCII value of DOS )instead of 'a' lol? For ex : Inputted Pass is : abc Displayed Pass is : smiley-facebc EDIT : I recently discoverd that if the entered pass is 3 letters only the above mentioned error comes. Can someone explain this to me why ? This sort of thing doesn't happen if 'a' is anywhere other than the starting character in thr array and the array hold only 3 letters. | |||
| Duoas (1596) | |||
| Failure to initialize creates a great big surprise. Include <cstring> and put the following line between lines 2 and 3 above:
memset( pass, 0, 5 );[edit] Oh, and keep your passwords four characters or less. | |||
This topic is archived - New replies not allowed.
