11 Replies - 6376 Views - Last Post: 12 July 2011 - 10:15 PM Rate Topic: -----

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Censor password in C++

Posted 04 July 2011 - 06:05 AM

Is there a way to censor input (change it to *) in C++? I've gone as far as having a main function like this which fails if you try to enter less than/more than the desired password's digits:
bool bAuth1,bAuth2,bAuth3;
if(_getch()=='p')bAuth1==true;
else{bAuth1==false;}
cout<<"*";
if(_getch()=='i')bAuth2==true;
else{bAuth2==false;}
cout<<"*";
if(_getch()=='e')bAuth3==true;
else{bAuth3==false;}
cout<<"*";
if(bAuth1==true&&bAuth2==true&&bAuth3==true)
{
    cout<<"Welcome.";
}
else
{
    cout<<"Incorrect password";
}



I even searched on google and youtube for it but can't find any clear solutions. :sleepy:

This post has been edited by hulla: 04 July 2011 - 06:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Censor password in C++

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1010
  • View blog
  • Posts: 4,214
  • Joined: 14-February 08

Re: Censor password in C++

Posted 04 July 2011 - 06:24 AM

Well you could just get the letters as usual but just output * in their place, sort of like this.
char ch;
while(ch != 13)
{
    pass.push_back(ch);
    cout << '*';
    ch = _getch();
}



Normally I wouldn't say to use _getch but it looks like you are using conio.h already.
Was This Post Helpful? 0
  • +
  • -

#3 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Censor password in C++

Posted 04 July 2011 - 06:26 AM

You could try looping till the user hits enter:
bool get_password()
{
    unsigned char input;
    std::string total_input("");
    while ((input = _getch()), input != '\n') {
        total_input += input;
        std::cout << '*';
    }
    if (total_input == "password") {
        return true;
    }
    return false;
}
Note the use of the comma operator. This means we first set input, then compare it. This way you don't discard any inputs, and you also don't have to use an uninitialised value.

@stayscrisp: He doesn't have any choice -- conio is required for complex input as he doesn't want to use WinAPI.

This post has been edited by PlasticineGuy: 04 July 2011 - 06:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Censor password in C++

Posted 04 July 2011 - 07:54 AM

@ PlasticineGuy

It wont work......

this line needs to be changed

from
while ((input = _getch()), input != '\n')



to

while ((input = _getch()), input != '\r')



then your function will work.

Best Wishes Snoopy.
Was This Post Helpful? 1
  • +
  • -

#5 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Censor password in C++

Posted 04 July 2011 - 01:26 PM

You could try my snippet for secure password entry in the console. :)
It is, however, Windows specific.

This post has been edited by gabehabe: 04 July 2011 - 01:26 PM

Was This Post Helpful? 3
  • +
  • -

#6 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Censor password in C++

Posted 06 July 2011 - 02:16 AM

Thanks everyone. :)
Was This Post Helpful? 0
  • +
  • -

#7 Narek Babadjanyan  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 47
  • Joined: 30-May 11

Re: Censor password in C++

Posted 08 July 2011 - 10:56 AM

View Poststayscrisp, on 04 July 2011 - 05:24 PM, said:

Well you could just get the letters as usual but just output * in their place, sort of like this.
char ch;
while(ch != 13)
{
    pass.push_back(ch);
    cout << '*';
    ch = _getch();
}



Normally I wouldn't say to use _getch but it looks like you are using conio.h already.

Can we use cin.get() or it wouldn't work ?(i don't know what pass.push_back means)
Was This Post Helpful? 0
  • +
  • -

#8 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Censor password in C++

Posted 08 July 2011 - 11:08 AM

View PostNarek Babadjanyan, on 08 July 2011 - 10:56 AM, said:

View Poststayscrisp, on 04 July 2011 - 05:24 PM, said:

Well you could just get the letters as usual but just output * in their place, sort of like this.
char ch;
while(ch != 13)
{
    pass.push_back(ch);
    cout << '*';
    ch = _getch();
}



Normally I wouldn't say to use _getch but it looks like you are using conio.h already.

Can we use cin.get() or it wouldn't work ?(i don't know what pass.push_back means)


I would assume pass is a vector and the push_back method adds it to the end of the vector. So If I had

vector = [1 2 3 4 5]
vector.push_back(6)
vector = [1 2 3 4 5 6]
Was This Post Helpful? 0
  • +
  • -

#9 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Censor password in C++

Posted 12 July 2011 - 07:00 PM

cin.get() waits for you to press enter. Therefore _getch() is better. Such a shame conio.h is old...
Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Censor password in C++

Posted 12 July 2011 - 07:58 PM

it's unbuffered input vs. buffered input. getch() and _getch() are platform/os/compiler specific, therefore the are non-standard and also non-poratable.

_getch() is supported by visual studio; however, it is still dependent on <conio.h> which is out of date.

You can use window's APIs as an alternative.

I wasn't able to try and compile this because i am not on windows at the moment, but you get the idea.

#include <iostream>
#include <string>
#include <windows.h>

CHAR win_getch (void)
{
  HANDLE hStdin = GetStdHandle (STD_INPUT_HANDLE);
  INPUT_RECORD irInputRecord;
  DWORD dwEventsRead;
  CHAR cChar;
	while(ReadConsoleInputA (hStdin, &irInputRecord, 1, &dwEventsRead)) { /* Read key press */
		if (irInputRecord.EventType == KEY_EVENT) {
			cChar = irInputRecord.Event.KeyEvent.uChar.AsciiChar;
			ReadConsoleInputA (hStdin, &irInputRecord , 1, &dwEventsRead); /* Read key release */
			return cChar;
		}
	}
  return EOF;
}
int main()
{
	std::string password;
	char c;
	while(true) {
		if((c = win_getch()) == VK_RETURN) {
			break;
		}
		std::cout<<'*';
		password.push_back(c);
	}
	
	std::cout<<"You Entered : "<<password<<std::endl;
	std::cin.ignore();
	std::cin.get();
	return 0;
}


This post has been edited by ImaSexy: 12 July 2011 - 07:58 PM

Was This Post Helpful? 1
  • +
  • -

#11 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Censor password in C++

Posted 12 July 2011 - 08:27 PM

Thanks.
Was This Post Helpful? 0
  • +
  • -

#12 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Censor password in C++

Posted 12 July 2011 - 10:15 PM

Awesome, I've done it :D Here is the source code in case you want to read it.

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<Windows.h>
using namespace std;
void correctPass()
{
    cout<<"\n\nYou're entry is correct.\n\n";
    system("PAUSE");
}
int main()
{
    short C=0;
    cout<<"Enter the password: ";
    bool bDoubleBackspace,bAuth1p=false,bAuth2a=false,bAuth3s=false,bAuth4s=false,bAuth5w=false,bAuth6o=false,bAuth7r=false,bAuth8d=false,bAuth9h=false,bAuth10e=false,bAuth11r=false,bAuth12e=false,bSecure12letters=true;
    while(true)
    {
        char ch=_getch();
        if(C<0)//Make sure that C is always 0 or more
        {
            C=0;
        }
        if(ch==13||ch==8||ch==27)
        {
            if(ch==13)
            {
                if(bAuth1p==true&&bAuth2a==true&&bAuth3s==true&&bAuth4s==true&&bAuth5w==true&&bAuth6o==true&&bAuth7r==true&&bAuth8d==true&&bAuth9h==true&&bAuth10e==true&&bAuth11r==true&&bAuth12e==true&&bSecure12letters==true)//Correct password
                {
                    correctPass();
                }
                else//Wrong password
                {
                    cout<<"\n\nIncorrect entry.\n\nPress any key to exit...";
                    _getch();
                    break;
                }
                return 0;
            }
            if(ch==27)//for escape
            {
                exit(0);
            }
            if(ch==8)//For backspace
            {
                if(C==1)
                {
                    system("CLS");
                    cout<<"Enter the password: ";
                }
                if(C==2)
                {
                    system("CLS");
                    cout<<"Enter the password: *";
                }
                if(C==3)
                {
                    system("CLS");
                    cout<<"Enter the password: **";
                }
                if(C==4)
                {
                    system("CLS");
                    cout<<"Enter the password: ***";
                }
                if(C==5)
                {
                    system("CLS");
                    cout<<"Enter the password: ****";
                }
                if(C==6)
                {
                    system("CLS");
                    cout<<"Enter the password: *****";
                }
                if(C==7)
                {
                    system("CLS");
                    cout<<"Enter the password: ******";
                }
                if(C==8)
                {
                    system("CLS");
                    cout<<"Enter the password: *******";
                }
                if(C==9)
                {
                    system("CLS");
                    cout<<"Enter the password: ********";
                }
                if(C==10)
                {
                    system("CLS");
                    cout<<"Enter the password: *********";
                }
                if(C==11)
                {
                    system("CLS");
                    cout<<"Enter the password: **********";
                }
                if(C==12)
                {
                    system("CLS");
                    cout<<"Enter the password: ***********";
                }
                C--;
                if(bDoubleBackspace==true)
                {
                    cout<<"";
                    bDoubleBackspace=false;
                }
                else
                {
                    cout<<"";
                }
            }
        }
        else
        {
            {//Authentication check
                bDoubleBackspace=true;
                if(C==0&&ch=='p')
                {
                    bAuth1p=true;
                }
                if(C==1&&ch=='a')
                {
                    bAuth2a=true;
                }
                if(C==2&&ch=='s')
                {
                    bAuth3s=true;
                }
                if(C==3&&ch=='s')
                {
                    bAuth4s=true;
                }
                if(C==4&&ch=='w')
                {
                    bAuth5w=true;
                }
                if(C==5&&ch=='o')
                {
                    bAuth6o=true;
                }
                if(C==6&&ch=='r')
                {
                    bAuth7r=true;
                }
                if(C==7&&ch=='d')
                {
                    bAuth8d=true;
                }
                if(C==8&&ch=='h')
                {
                    bAuth9h=true;
                }
                if(C==9&&ch=='e')
                {
                    bAuth10e=true;
                }
                if(C==10&&ch=='r')
                {
                    bAuth11r=true;
                }
                if(C==11&&ch=='e')
                {
                    bAuth12e=true;
                }
                if(C==12)
                {
                    bSecure12letters=false;
                }
            }
            cout<<"*";
            C++;
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1