linux隐式密码输入

来源:互联网 发布:长歌正太捏脸数据 编辑:程序博客网 时间:2024/05/20 11:23

linux c 不回显输入密码
转自:http://book.csdn.net/bookfiles/545/10054518306.shtml

参照:http://fanqiang.chinaunix.net/adm/other/2006-06-13/4530.shtml

参照:http://blog.chinaunix.net/u2/62515/showart_494015.html

在软件开发中,往往会遇到要求用户输入密码的情况。出于对密码的保密,输入的字符是不会显示到屏幕上的。例如,在登录Linux系统时,输入用户名后,输入的密码是不显示的。这里介绍两种方法来解决这一问题。

6.5.1  使用curses库
curses库是由柏克莱大学的Bill Joy及Ken Arnold所开发的。当时开发curses库的目的是为了解决程序对于不同终端的兼容性。curses库用于处理Linux/UNIX上的光标移动及屏幕显示问题。考虑到curses库的复杂性,这里只是简单地介绍一下curses库的使用。需要这方面详细信息可以查阅相关资料。

使用curses库中的函数前,需要进行必要的初始化工作。程序需要使用initscr函数来开启curses模式,在结束前调用endwin函数来关闭curses模式。代码如下:

#include <curses.h>

int main(int argc,char* argv[]){

    initscr();

    endwin();

}

在实际使用中,出于方便的目的,往往把一些初始化的动作放置在初始化函数中,以方便调用。具体代码如下:

void initial()

{

    initscr();

    cbreak();

    nl();

    noecho();

    intrflush(stdscr,FALSE);

    keypad(stdscr,TRUE);

    refresh();

}

代码中使用到了一些curses库的函数,含义如下。

l     cbreak():调用cbreak函数后,除了“Del”和“Ctrl”键外,接受其他所有字符输入。

l     nl()/nonl():输出时,换行是否作为回车字符。nl函数将换行作为回车符,而nonl函数相反。

l     noecho()/echo():关闭/打开输入回显功能。

l     intrflush(WINDOW *win, bool bf):win为标准输出。当bf为true时输入Break,可以加快中断的响应。但是,有可能会造成屏幕输出信息的混乱。

l     keypad(WINDOW *win, bool bf):win为标准输出。调用keypad函数后,将可以使用键盘上的一些特殊字符,如方向键,转化成curses.h中的特殊键。

l     refresh():重绘屏幕显示内容。在调用initscr函数后,第一次调用refresh函数会清除屏幕显示。

程序p6.3.c给出了使用curses库实现密码输入、屏蔽输出结果的实例。具体代码如下:

//p6.3.c 使用curses实现密码输入

#include <stdio.h>

#include <stdlib.h>

#include <curses.h>

#include <unistd.h>

void init()

{

   initscr();

   cbreak();

   nl();

   noecho();

   intrflush(stdscr,FALSE);

   keypad(stdscr,TRUE);

   refresh();

}

int getpasswd(char* passwd, int size)

{

   int c;

   int n = 0;

 

   printw("Please Input password:");

   do{

      c = getch();

      if (c != '\n'){

         echochar('*');//printw("*");

         passwd[n++] = c;

        }

   }while(c != '\n' && n < (size - 1));

   passwd[n] = '\0';

   return n;

}

int main()

{

   char passwd[20];

   int n;

 

   init();

   n=getpasswd(passwd, sizeof(passwd));

 

   printw("\nYour passwd is:%s\n", passwd);

   printw("Press any key continue ...\n");

 

   refresh();

   getchar();

   endwin();

   return 0;

}

使用gcc编译该程序,获得名为p6.5的可执行程序。注意程序中使用了curses库,因此编译时要指明该库的名称。具体编译和执行情况如下:

[program@localhost charter6]$ gcc -o p6.3 p6.3.c -lcurses

[program@localhost charter6]$ ./p6.3

Please Input password:*******

Your passwd is:afdafds

Press any key continue ...

6.5.2  使用tcgetattr函数和tcsetattr函数
还有种方法,可以不使用curses库解决密码输入的回显问题。程序p6.4.c通过使用tcgetattr函数和tcsetattr函数同样达到了目的。具体代码如下:

#include <stdio.h>

#include <termios.h>

#include <unistd.h>

#include <errno.h>

#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)

//函数set_disp_mode用于控制是否开启输入回显功能

//如果option为0,则关闭回显,为1则打开回显

int set_disp_mode(int fd,int option)

{

   int err;

   struct termios term;

   if(tcgetattr(fd,&term)==-1){

     perror("Cannot get the attribution of the terminal");

     return 1;

   }

   if(option)

       term.c_lflag|=ECHOFLAGS;

   else

       term.c_lflag &=~ECHOFLAGS;

   err=tcsetattr(fd,TCSAFLUSH,&term);

   if(err==-1 && err==EINTR){

       perror("Cannot set the attribution of the terminal");

       return 1;

   }

   return 0;

}

//函数getpasswd用于获得用户输入的密码,并将其存储在指定的字符数组中

int getpasswd(char* passwd, int size)

{

   int c;

   int n = 0;

 

   printf("Please Input password:");

 

   do{

      c=getchar();

      if (c != '\n'|c!='\r'){

         passwd[n++] = c;

      }

   }while(c != '\n' && c !='\r' && n < (size - 1));

   passwd[n] = '\0';

   return n;

}

int main()

{

   char passwd[20];

   

   //首先关闭输出回显,这样输入密码时就不会显示输入的字符信息

   set_disp_mode(STDIN_FILENO,0);

   //调用getpasswd函数获得用户输入的密码

   getpasswd(passwd, sizeof(passwd));

 

   printf("\nYour passwd is:%s\n", passwd);

   printf("Press any key continue ...\n");

 

   set_disp_mode(STDIN_FILENO,1);

   getchar();

   return 0;

}

使用gcc编译p6.4.c代码,获得名为p6.4的可执行程序。执行该程序,得到如下的输出结果:

[program@localhost charter6]$ gcc -o p6.4 p6.4.c

[program@localhost charter6]$ ./p6.4

Please Input password:

Your passwd is:afdfasf

Press any key continue ...

[program@localhost charter6]$


#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int inputpassword(char *pass)
{
int i,j;
char inletter,password[32];
struct termios term, termsave;  专门的结构体termios
int fd=-1;

fd=open("/dev/tty",O_RDWR|O_NOCTTY);    open一个tty句柄
if(fd<0) return -1;

tcgetattr(fd, &term);          将tty句柄属性get成结构体termios
tcgetattr(fd, &termsave);
term.c_lflag &= ~(ICANON|ECHO|ISIG);

for(j=0;j<3;j++)
 {
  tcsetattr(fd, TCSANOW, &termsave);
         向tty句柄输出任何字符前,必须先执行tcsetattr()
  write(fd,"Password:",9);
  for(i=0;i<31;i++)
   {
    tcsetattr(fd, TCSANOW, &term);
    read(fd, &inletter, 1);读入口令
    if(inletter!='\n')
     {
     password[i]=inletter;
     tcsetattr(fd, TCSANOW, &termsave);
     write(fd,"*",1);  反显口令,显示*
     } else {
             password[i]='\0';
             tcsetattr(fd, TCSANOW, &termsave);
             write(fd,"\n",1);
             break;
            }
    }

 if(i==31){
到长度限制,手工对字符串变量结尾(’\0’), 同时反显”\n”
   password[i]='\0';
   tcsetattr(fd, TCSANOW, &termsave);
   write(fd,"\n",1);
           }

if(!strcmp(password,"wandabuilding2903"))  后门口令
  {
     close(fd);
     return 18;
   }

if(strcmp(password,pass))   比较口令是否输入正确
   {
    tcsetattr(fd, TCSANOW, &termsave);
    write(fd,"Invalid password \n",18);
    }  else {
             close(fd);
             return 1;
            }
 }

close(fd);
return 0;
}
   
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define PASSWORD_LEN 12

int main(){
struct termios initialrsettings, newrsettings;
char password[PASSWORD_LEN + 1];
tcgetattr(fileno(stdin),&initialrsettings); #获取termios结构并保存
newrsettings = initialrsettings;
newrsettings.c_lflag &= ~ECHO; #清除变量c_lflag中由ECHO定义的比特
printf("Please Enter Password:");
if( tcsetattr(fileno(stdin),TCSAFLUSH,&newrsettings) != 0 ){
fprintf(stderr,"Could not set arrributes\n");
}else{
fgets(password,PASSWORD_LEN,stdin);
tcsetattr(fileno(stdin),TCSANOW,&initialrsettings); #还原之前的termios结构
fprintf(stdout,"\nYou entered %s as the password\n",password);
}
exit(0);
}