Masking Text while scanning - C -
i have prompt user password, when entering password want characters masked, have created code characters masked until enter key pressed. character array pwd not reading characters guess!
char pwd[10]; while(ch!=13) { pwd[i] = ch; ch = '*' ; printf("%c",ch); ch=getch(); i++; } pwd[i]='\0'; printf("%s",pwd);
nothing printed when try print pwd.
as @olaf , @eugenesh. mentioned, terrible way passwords. said, here's help.
assuming being initialized somewhere, move "pwd[i] = ch" end of loop before "i++" whole thing should (note still wrong):
char pwd[10]; while(ch!=13) { ch = '*' ; printf("%c",ch); ch=getch(); pwd[i] = ch; i++; } pwd[i]='\0'; printf("%s",pwd);
i print "*" after user enters character. let's fix other problems while we're @ it.
int = 0; /* since didn't specify if initialized */ char pwd[15]; /* setting 10 have caused overrun buffer. not crashy, it's huge source of security holes. let's not that. */ /* unless you're waiting user enter in character happens ascii 13, you'll never out. let's change since i'm guessing want 13 character password. */ while(i != 13) { char ch=getch(); pwd[i++] = ch; /* can post increment right here don't have @ end of loop might forget */ printf("*"); fflush(stdout); /* since using printf stdout (not stderr, need manually flush stdout or things won't show until newline printed. */ } printf("\n"); /* print newline don't end printing password on same line */ pwd[14]='\0'; printf("password entered our crappy security system: %s",pwd);
Comments
Post a Comment