Passwords

Nov 7, 2009 at 6:17pm
I'm going to show how to get passwords on windows and linux

UNIX:
One of the simplest ways is to use getpass() function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
#include <iostream>
using namespace std;

const char *mypass="vista";

int main()
{
char *password=getpass("Enter password: ");	// yes, that's all you need!

if(strcmp(password,mypass)==0) cout <<"Correct password!\n";
else cout <<"Incorrect password!\n";

// I hope you are using a smart IDE...
return 0;
}

Windows: Requires conio.h getch() function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
#define MAX_LENGHT     128
#define ENTER           13
#define BACKSPACE        8
    
    char ch;
    char password[MAX_LENGHT];
    const char *mypass="asilas";
    int pos=0;
    
int main()
{
   cout <<"Please enter the password:\n";
   while(true)
     { 
       ch=getch();
       if(pos>=MAX_LENGHT) {cout<<'\a'; continue;} /* beep if password is too long */
       
       if(ch==ENTER) break;  /* User have pressed ENTER*/
       
       else if(ch==BACKSPACE)  /* BACKSPACE was pressed*/
         {
           cout <<"\b \b";   
           password[--pos]='\0';
         }
       else/* A..Z a...z  BUG: I forgot what... */
         {
           cout <<"*";
           password[pos++]=ch;
           password[pos]='\0';
         }
       if(pos<=0) pos=0;
     }
    
 if(strcmp(password,mypass)==0) cout <<"\nCorrect password!\n";   
 else  cout <<"\nAccess denied!\n";  
    
    
    
    cout.flush();
    cin.get(); 
    return EXIT_SUCCESS;
}


CROSS-PLATFORM WAY:

Use ncurses

Example 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdlib>
#include <cstring>
#include <curses.h>
using namespace std;
const char *password="long password";

char mypass[128];    

int main()
{
   initscr(); // enable ncurses
   noecho();  // disable character echoing
  
   printw("Please enter the password: ");
   getstr(mypass);
   
   if(strcmp(password,mypass)==0) printw("\nCorrect password!");  
   else  printw("\nAccess denied!");  
    
    
    getch();
    endwin(); // disable ncurses
    
    return 0;
}


Example 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cstdlib>
#include <iostream>
#include <curses.h>
using namespace std;
#define MAX_LENGHT     128
#define ENTER           10  // Bug? I tried #define ENTER KEY_ENTER but it didn't work... 
#define BACKSPACE        8
    
    int ch;
    char password[MAX_LENGHT];
    char *mypass="password";
    int pos=0;
    
int main()
{
   initscr();
   noecho();  // turn off echoing
   printw("Please enter the password:\n");
   while(true)
     { 
       ch=getch();
       if(pos>=MAX_LENGHT) {cout<<'\a'; continue;} /* beep if password is too long */
       
       if(ch==ENTER) break;  /* User has pressed ENTER*/
       
       else if(ch==BACKSPACE)  /* BACKSPACE was pressed*/
         {
           cout <<"\b \b";   
           password[--pos]='\0';
         }
       else/* A..Z a...z  */
         {
           cout <<"*";
           password[pos++]=ch;
           password[pos]='\0';
         }
       if(pos<=0) pos=0; 
     }
    
    
   if(strcmp(password,mypass)==0) printw("\nCorrect password!\n");  
   else  printw("\nAccess denied\n");
    
   echo(); // you can turn on echoing now
    
   getch();
   endwin();
    return 0;
}


Notes:
1. Download curses here:
http://sourceforge.net/projects/pdcurses/files/
2. You must link your project with pdcurses.lib library.
3. On widows pdcurses.dll must be in your program directory.
--------------
FIXME/CHECK: Does the second exaple work on linux?
Last edited on Nov 8, 2009 at 10:01am
Nov 7, 2009 at 7:07pm
This is a cool tutorial. I didn't know about the getpass() function.

I have a tested version of your program for UNIX based OSs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdlib>
#include <cstring>
#include <iostream>

int main(void) {
    const char* correct = "password_for_root";
    char* username = (char*)malloc(sizeof(char) * 1024);
    strcpy(username, "password for ");
    strcat(username, getlogin());
    strcat(username, ": ");
    
    char* passwd = (char*)malloc(sizeof(char) * 1024);
    
    while (strcmp(passwd, correct)) {   
        passwd = getpass(username);        
        std::cerr << "Sorry, try again.\n";
    }

    std::cout << "Password is correct.\n";
    
    free(username);
    free(passwd);
    
    return 0;
}


Ignore the extremely inelegant prompt part.


For the Windows one I think you can use SetConsoleMode http://msdn.microsoft.com/en-us/library/ms686033(VS.85).aspx to disable input echoing temporarily and then print asterisks instead. Or you could use fclose() on stdout or something.
Last edited on Nov 7, 2009 at 7:10pm
Nov 8, 2009 at 7:09am
Or you could use fclose() on stdout or something.

Wouldn't that cause undefined behavior? I thought you aren't allowed to close/open the standard IO stuff.
Nov 8, 2009 at 12:02pm
Interesting idea... But how will you open stdout? Where is it?
Nov 8, 2009 at 9:48pm
Wouldn't that cause undefined behaviour? I thought you aren't allowed to close/open the standard IO stuff.

I don't know for Windows. UNIX systems have open() and close(); I've used them on stdin and stdout (but never stderr :P) before.

Interesting idea... But how will you open stdout? Where is it?

Again; I don't know how windows handles it; but on UNIX because you have file descriptors, stdin is 0, stdout is 1 and stderr is 2. Then you have the rest (up to 9 on sysv, up to 256 on Linux I think) to open other files.
Topic archived. No new replies allowed.