Comp Sci What is the Best Way to Create a Secure Password Prompt in C++?

  • Thread starter Thread starter Deathfish
  • Start date Start date
  • Tags Tags
    C++ Echo
AI Thread Summary
To create a secure password prompt in C++, the implementation should echo each character as an asterisk while allowing backspace functionality for corrections. The current approach using `getch()` captures key presses, but it fails to handle backspace and delete properly, leading to poor output formatting. Input filtering is recommended to accept only valid characters based on the ASCII table. Additionally, prompting the user to re-enter the password can enhance security by ensuring consistency. Properly addressing these issues will improve the overall functionality of the password prompt.
Deathfish
Messages
80
Reaction score
0
Homework Statement

Currently using stdio.h, conio.h, string.h, stdlib.h
not using iostream, namespace std etc.

I am asked to make a password prompt where each character echoes an asterisk (*)

The user must be able to backspace and delete to make corrections.

The password is then stored in an array which can accommodate up to 100 passwords.

The attempt at a solution

count=0;
while(c != eof())
{
`c=getch();
if (c != eof())
{
printf("*");
c=pw[userid][count];
count++;
}
}
pw[userid][count] = '\0';

sadly, the output indentation looks horrible and it is uneditable using backspace and delete.
 
Physics news on Phys.org
As I recall, getch returns any key presses. This includes control characters, such as, say, the backspace character (ASCII 0x08):
http://en.wikipedia.org/wiki/ASCII

You might also want to do some input filtering to ensure that only characters you want accepted actually are (again, look at the ASCII table). Lastly, rather than go crazy with allowing the user to edit and change the password, you might want to consider using a "Please type password again", just to make sure the user is entering both the same.

Good luck!
 

Similar threads

Replies
5
Views
2K
Replies
3
Views
2K
Replies
3
Views
3K
Replies
7
Views
8K
Replies
1
Views
2K
Replies
2
Views
6K
Replies
13
Views
7K
Back
Top