How to create a password mask in C

Asked

Viewed 2,506 times

1

I created this code in C to generate mask in the password, but it does not let delete the characters later nor save the variable correctly. What can it be ?

Follows current code:

void login(){   


 int i,tam;
 char usuario[80], senha[80];
 printf("Digite o usuario. -> ");
 scanf("%s", usuario);
 printf("Digite a senha. -> ");
 fflush(stdin);
  for (i=0; i<10; i++) 
{
    senha[i] = getch();
    putchar('*');

}
printf("\n");
senha[i]='\0';    
scanf("%s", senha); } 

2 answers

0


Below your modified example: include in your project the string library. h for memset usage. It is a good practice to reset variables at startup.

 int tam = 0;
 char usuario[80], senha[80], senha2[80];
 memset(usuario, 0x00, sizeof(usuario));
 memset(senha, 0x00, sizeof(senha));
 memset(senha2, 0x00, sizeof(senha2));

 printf("Digite o usuario. -> ");
 scanf("%s", &usuario);
 printf("Digite a senha. -> ");
 fflush(stdin);

    do
    {   
         senha[tam] = getch();          
         if(senha[tam] == 0x08 && tam > 0)  //Backspace
         {   
            printf("\b \b"); 
            senha[tam] = 0x00;
            tam--;

         } 
         else if (senha[tam] == 13) // Enter
         {  
            senha[tam] = 0x00;
            break;
        }
        else if (senha[tam] != 0x08)
        {
            putchar('*');
            tam++;              
         }
    }while(tam < 10) ;

printf("\nDigite a senha novamente ->");

scanf("%s", &senha2); 

if (strcmp(senha, senha2) == 0)
{
    printf("%s, Senha ok\n", usuario);
}
else 
    printf("%s, Senha invalida\n", usuario);
  • I’m trying to adapt the code but it’s asking twice for the password and the second time without mask. I need him to ask only once the password and store in the variable "password" following to a "for" that I use to validate a txt with login and password of what I have already registered.

  • Remove the scanf("%s", &senha2) and compare condition below if(strcmp... What was done was only a validator, but you can remove without problems that the password will be stored in the variable "password'. I suggest you create a separate main function to be used when necessary the user type the password, so you can reuse the code.

  • It worked out ! Thank you #

0

I managed doing a little program for Linux.

#include <stdio.h>
#include <termios.h>

int main()
{
  int i,tam;
  char usuario[80], senha[80];

  struct termios new, old;

  printf("Digite o usuario. -> ");
  scanf("%s", usuario);
  printf("Digite a senha. -> ");

  // salvando atual terminal
  tcgetattr (fileno (stdin), &old);
  new = old;
  new.c_lflag &= ~ECHO; // desligando o ECHO

  // setando novo terminal
  tcsetattr (fileno (stdin), TCSAFLUSH, &new);

  scanf("%s", senha);

  // setando para o antigo terminal
  (void) tcsetattr (fileno (stdin), TCSAFLUSH, &old);
}

Here follows the definition of struct termios:

struct termios
{
  tcflag_t c_iflag;      /* input modes */
  tcflag_t c_oflag;      /* output modes */
  tcflag_t c_cflag;      /* control modes */
  tcflag_t c_lflag;      /* local modes */
  cc_t     c_cc[NCCS];   /* special characters */
}

The function tcgetattr retrieves the terminal information. Already, the function tcsetattr arrow changes, you can pass some options. The option TCSAFLUSH says if there are any bytes to read, it will be discarded.

References:

  1. reply in the OS;
  2. man of the tcsetattr command.

PS: I don’t know if it runs on Windows.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.