How to create a password mask in C

1

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

Follow the 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]='
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]='%pre%';    
scanf("%s", senha); } 
'; scanf("%s", senha); }
    
asked by anonymous 07.06.2017 / 15:12

2 answers

0

Below your modified example: Include in your project the library of strings string.h for the use of memset. It is a good practice to zero the 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);
    
07.06.2017 / 16:17
0

I managed to do a 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 is 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 tcgetattr function retrieves the terminal information. Already, the tcsetattr function changes, and you can pass some options. The TCSAFLUSH option says that if there are some bytes to read, they will be discarded.

References:

  • response in the SO;
  • man command from tcsetattr.
  • PS: I do not know if it runs under Windows.

        
    07.06.2017 / 16:04