2009-11-23 28 views
12

मैं उपयोगकर्ताओं को कमांड लाइन इंटरफ़ेस का उपयोग करके पासवर्ड दर्ज करने की अनुमति देना चाहता हूं। लेकिन मैं स्क्रीन पर यह पासवर्ड प्रदर्शित नहीं करना चाहता (या "****" प्रदर्शित करें)।सी कमांड लाइन पासवर्ड इनपुट

सी में इसे कैसे करें? धन्यवाद।

अद्यतन:

मैं केवल लिनक्स पर काम कर रहा हूं। इसलिए मुझे वास्तव में विन या अन्य सिस्टम की परवाह नहीं है। मैंने लुकास के समाधान की कोशिश की और यह ठीक काम किया।

  1. यह एक एकल प्रक्रिया & एकल थ्रेड ऐप्स है, तो बदल रहा termios की स्थापना विभिन्न टर्मिनलों को प्रभावित करता है: हालांकि, मैं अभी भी एक और सवाल है?

  2. लगभग 1 प्रक्रिया - बहु धागे, बहु प्रक्रियाएं - बहु धागे? बहुत ज्यादा

धन्यवाद।

+7

http://www.linux.com/learn/docs/man/3177-getpass3 – sambowry

+0

कौन सा कंसोल और ओएस आप पर कर रहे हैं? ज्यादातर मामलों में उपयोगकर्ता इनपुट कमांड लाइन पर वापस प्रतिबिंबित किया जाएगा। इसे बदला जा सकता है लेकिन मंच पर निर्भर होगा। – Lucas

+2

डुप्लिकेट: http://stackoverflow.com/questions/1413445/read-a-password-from-stdcin –

उत्तर

2

सी/कमांडलाइन/लिनक्स देखें:

man getch 
man noecho 

getch के बारे में noecho में coment देखते हैं। मैंने कभी यह कोशिश नहीं की है।

बैश में यदि आप read -s का उपयोग यह स्क्रीन पर गूंज नहीं करता है:

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

यह पात्रों के रूप में कुछ भी प्रदर्शित नहीं करेगा:

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

आरटीएफएम दर्शन और क्रॉस प्लेटफॉर्म संकेत के लिए उपरोक्त। – conradkdotcom

16

आपके सिस्टम यह प्रदान करता है, getpass एक विकल्प है टाइप किया गया है

2

पोर्टेबल तरीके से ऐसा करने के लिए आपको एक मानक या डी-फैक्टो मानक लाइब्रेरी का उपयोग करने की आवश्यकता होगी।

man 3 termios और man 3 ncurses देखें।

यहाँ एक प्रोग्राम है जो Linux और अन्य यूनिक्स सिस्टम पर काम करेंगे है ...

#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; 
} 

सुधार की एक संख्या संभव हो रहे हैं। टर्मियो का उपयोग करना शायद बेहतर होगा, और यह प्रणाली के फोर्क और संभावित सुरक्षा मुद्दों से बच जाएगा()। पासवर्ड पढ़ने के लिए मानक I/O का उपयोग करना शायद बेहतर होगा। (जैसा लिखा है कि टाइप की गई नई लाइन को हटा देना होगा।) लिनक्स में एक गेटपास() फ़ंक्शन है और कुछ अन्य हालांकि इसे "अप्रचलित" के रूप में चिह्नित किया गया है। इसका उपयोग न करें। SIGTSTP से निपटने के लिए यह एक अच्छा विचार हो सकता है।

कुल मिलाकर, मैं एक मौजूदा समाधान है कि इन सभी छोटे मुद्दों के साथ सौदों के लिए लग सकता है ...

1

आप शाप पुस्तकालय के लिए उपयोग किया है, तो आप noecho उपयोग कर सकते हैं। यदि आप माइक्रोसॉफ्ट के सी कंपाइलर का उपयोग कर रहे हैं, तो आप _getch का उपयोग कर सकते हैं। लेकिन afaik, ये दोनों कंसोल के लिए टाई। यदि आपको कंसोल या फ़ाइल या पाइप कमांड से आता है, भले ही आपको stdin पढ़ने की आवश्यकता है, तो आपको ऑपरेटिंग सिस्टम को यह बताना होगा कि इसे handle the console कैसे करना चाहिए।

9

यदि आप यूनिक्स पर्यावरण का उपयोग कर रहे हैं तो ऐसा कुछ कमांड लाइन के ईसीएचओ को बंद कर सकता है।

#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); 
} 
संबंधित मुद्दे