2009-11-23 141 views
12

我想讓用戶使用命令行界面輸入密碼。但我不想在屏幕上顯示此密碼(或顯示「****」)。C命令行密碼輸入

如何在C? 謝謝。

更新:

我只工作在Linux上。所以我實際上並不關心Win或其他系統。我試過盧卡斯的解決方案,它運行良好。不過,我還有另一個問題:

  1. 如果這是一個過程&單線程應用,改變的termios的設置會影響不同的終端?

  2. 1進程怎麼樣 - 多線程,多進程 - 多線程?

非常感謝。

+7

http://www.linux.com/learn/docs/man/3177-getpass3 – sambowry 2009-11-23 22:37:09

+0

你在哪個控制檯和操作系統上?在大多數情況下,用戶輸入將在命令行上回顯。這可以改變,但會依賴於平臺。 – Lucas 2009-11-23 22:37:21

+2

重複:http://stackoverflow.com/questions/1413445/read-a-password-from-stdcin – 2009-11-23 22:37:29

回答

2

對於C /命令行/ Linux,請參閱:

man getch 
man noecho 

看到getchnoecho的COMENT。我從來沒有嘗試過這個。

在bash如果你使用read -s它不會在屏幕上回應:

> read -s x 
<type something><enter> 
> echo $x 
<whatever you typed> 
+0

Upvoted爲RTFM理念和跨平臺提示。 – conradkdotcom 2014-07-31 20:09:52

16

如果您的系統提供了它,getpass是一個選項:

#include <unistd.h> 
/* ... */ 
char *password = getpass("Password: "); 

這將不顯示任何的字符打字。

2

要以便攜的方式執行此操作,您需要使用標準化或事實上的標準庫。

請參閱man 3 termiosman 3 ncurses

這裏是一個程序,將在Linux和其他Unix系統工作...

#include <stdio.h> 

int main(void) { 
    int f = open("/dev/tty", 0); 
    char s[100]; 

    system("stty -echo > /dev/tty"); 
    f >= 0 && read(f, s, sizeof s) > 0 && printf("password was %s", s); 
    system("stty echo > /dev/tty"); 
    return 0; 
} 

許多改進是可能的。使用termios可能會更好,並且會避免system()的fork和可能的安全問題。使用標準I/O讀取密碼可能會更好。 (正如所寫的那樣,必須刪除類型化的換行符。)Linux中有一個getpass()函數,但有些函數被標記爲「過時,不要使用它」。處理SIGTSTP可能是個好主意。

總體來說,我可能會尋找一個現有的解決方案,與所有這些小問題,交易...

1

如果您有機會獲得詛咒庫,你可以使用noecho。如果您使用的是Microsoft的C編譯器,則可以使用_getch。但afaik,這兩者都將你與控制檯聯繫在一起。如果您需要閱讀stdin,無論它來自控制檯還是文件或管道命令,則需要告訴操作系統它應該如何應用handle the console

9

如果您使用的是UNIX環境,可以關閉命令行的ECHO。

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

#define SIZE 100 

void getPassword(char password[]) 
{ 
    static struct termios oldt, newt; 
    int i = 0; 
    int c; 

    /*saving the old settings of STDIN_FILENO and copy settings for resetting*/ 
    tcgetattr(STDIN_FILENO, &oldt); 
    newt = oldt; 

    /*setting the approriate bit in the termios struct*/ 
    newt.c_lflag &= ~(ECHO);   

    /*setting the new bits*/ 
    tcsetattr(STDIN_FILENO, TCSANOW, &newt); 

    /*reading the password from the console*/ 
    while ((c = getchar())!= '\n' && c != EOF && i < SIZE){ 
     password[i++] = c; 
    } 
    password[i] = '\0'; 

    /*resetting our old STDIN_FILENO*/ 
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt); 

} 


int main(void) 
{ 
    char password[SIZE]; 
    printf("please enter password\n"); 
    getPassword(password); 

    printf("Do something with the password <<%s>>\n", password); 
    return 0; 
} 
0
 #include<conio.h> 
    #include<iostream> 
    using namespace std; 
    int main(){ 
    char *pass = new char[20]; 
    cout<<"Password :"; 
    int i=0; 
    while((pass[i]=getch()) != '\n' && pass[i] != '\r' && i<19) 
    {putchar('*'); i++;} 
    pass[i]='\0'; 
    cout<<endl; 
    if(strcmp("123456789",pass)==0) // do stuff 
    return 0;} 
2

功能將getpass現在obsolete。使用termios

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

void get_password(char *password) 
{ 
    static struct termios old_terminal; 
    static struct termios new_terminal; 

    //get settings of the actual terminal 
    tcgetattr(STDIN_FILENO, &old_terminal); 

    // do not echo the characters 
    new_terminal = old_terminal; 
    new_terminal.c_lflag &= ~(ECHO); 

    // set this as the new terminal options 
    tcsetattr(STDIN_FILENO, TCSANOW, &new_terminal); 

    // get the password 
    // the user can add chars and delete if he puts it wrong 
    // the input process is done when he hits the enter 
    // the \n is stored, we replace it with \0 
    if (fgets(password, BUFSIZ, stdin) == NULL) 
     password[0] = '\0'; 
    else 
     password[strlen(password)-1] = '\0'; 

    // go back to the old settings 
    tcsetattr(STDIN_FILENO, TCSANOW, &old_terminal); 
} 

int main(void) 
{ 
    char password[BUFSIZ]; 

    puts("Insert password:"); 
    get_password(password); 
    puts(password); 
}