How to mask password in c?

被刻印的时光 ゝ 提交于 2019-12-30 13:14:51

问题


In C, I want to display every single character that the user type as * (Ex, Please type in your password: *****)

I'm searching around but can't be able to find a solution for this. I'm working on Ubuntu. Does anybody know a good way?


回答1:


See my code. it works on my FC9 x86_64 system:

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

int main(int argc, char **argv)
{
        char passwd[16];
        char *in = passwd;
        struct termios  tty_orig;
        char c;
        tcgetattr( STDIN_FILENO, &tty_orig );
        struct termios  tty_work = tty_orig;

        puts("Please input password:");
        tty_work.c_lflag &= ~( ECHO | ICANON );  // | ISIG );
        tty_work.c_cc[ VMIN ]  = 1;
        tty_work.c_cc[ VTIME ] = 0;
        tcsetattr( STDIN_FILENO, TCSAFLUSH, &tty_work );

        while (1) {
                if (read(STDIN_FILENO, &c, sizeof c) > 0) {
                        if ('\n' == c) {
                                break;
                        }
                        *in++ = c;
                        write(STDOUT_FILENO, "*", 1);
                }
        }

        tcsetattr( STDIN_FILENO, TCSAFLUSH, &tty_orig );

        *in = '\0';
        fputc('\n', stdout);

        // if you want to see the result: 
        // printf("Got password: %s\n", passwd);

        return 0;
}



回答2:


Take a look at the ncurses library. It's a very liberally licensed library with a huge amount of functionality on a large variety of systems. I haven't used it very much, so I'm not sure exactly which functions you'd want to call, but if take a look at the documentation, I'm sure you'll find what you want.




回答3:


Do it manually; Read input one character at a time with for example getch() in conio, and print a * for each.




回答4:


use a program like this one ask me for more questions

this program is used to put * instead of the char and it deletes the input after using backspace ^^

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
    char a[100],c;
    int i;
    fflush(stdin);
    for ( i = 0 ; i<100 ; i++ )
    {

        fflush(stdin);
        c = getch();
        a[i] = c;
        if ( a[i] == '\b')
        {
            printf("\b \b");
            i-= 2;
            continue;
        }
        if ( a[i] == ' ' || a[i] == '\r' )
            printf(" ");
        else
            printf("*");
        if ( a[i]=='\r')
            break;
    }
    a[i]='\0';

    printf("\n%s" , a);
}



回答5:


This is simplest program using header file only stdio.h and conio.h for implement asterisk at the place of Character in C programming language.

#include<stdio.h>
#include<conio.h>
void main()
{
  char pwd[15];
  int i;
  printf("Enter Password : ");
  for(i=0;i<15;i++)
  {
    pwd[i]=getch();
    if(pwd[i]!='\r')
    {
        printf("*");
    }
    if(pwd[i]==13)
        break;
  }

  printf("\n     \nPassword is : ");
  for(i=0;i<15;i++)
    printf("%d ",pwd[i]);
}


来源:https://stackoverflow.com/questions/1754004/how-to-mask-password-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!