Thread: Password form in C program

  1. #1
    Unregistered
    Guest

    Exclamation Password form in C program

    I am a beginner to C and I am trying to write a program that needs the user to key in his/her ID and password. How can I write the program so that the password that the user key in won't be revealed? (i.e. password shown in asterix *) Please help! Thank you! :-)

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    *sigh* SOMEONE PUT THIS IN THE FAQ!!!

    use a function like getch() which doesn't reveal the letter you typed, and use putch() to put a '*' every time ther person types.

    ie:
    Code:
    x=0;
    while(!done)/*whatever you want to use*/
    {
         passarr[x] = getch();
         putch('*');
         x++;
    }

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    To bad getch() isn't ANSI C though... although getch() is the only way I can think of to do it... so you can't do this portably... so be careful when you try to port any such programs...
    one fish two fish
    red fish blue fish

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > To bad getch() isn't ANSI C though...

    Like that stopped the "clear screen" entry from the faq...
    Anyway, like my tag line says...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158

    Talking

    THANK YOU!!!!!

  6. #6
    Unregistered
    Guest

    Question

    I tried to insert the Loop into the program and I have no idea how to make the program understand that the user has done the typing. Do I have to use some code to detect when the user hit the RETURN key?

    Please help!!!

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    For that particular code, you could use a sentinel value. Prompt the user to enter whatever they wish except a rarely used key on the keyboard, then edit the code so that when it reads that value the loop breaks. This is what !done means in KEN's code.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158

    Exclamation

    do
    {
    password[x]=getch();
    if('\r'==password[x])
    break;
    putch('*');
    x++;
    }while(password[x-1]!='\r');
    password[x-1]='\0';

    Above is the current code I am using. It will end when I hit the enter key but I still have a problem. When I made a typing mistake, I am not able to use the backspace function.

    Again, please help!
    And again, thanks in advance!
    It's unfulfilled dreams that keep you alive.

    //netboy

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    if( ch == '\b' ) { x--; putch( '\b' ); }

    That should help. You may want to check for '\n' in addition to '\r'.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158

    Smile

    My password program is approaching its final phase now.
    The masking part (***) is done....

    Now for the final section: The decrypting part.
    I am having a problem.

    My code are as follow:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int CheckPass(char *user_id, char *password);
    void AccessGranted();

    main()
    {
    char user_id[20], password[20];
    int flag, change=3;
    int count=3;

    do
    {
    /*Show the number of attempt(s) left*/
    printf("Number of attempt(s) left: %d\n", count);

    /*Gets user ID from user*/
    fflush(stdin);
    printf("Enter your user ID > ");
    scanf("%[^\n]", user_id);

    /*Gets password from user*/
    fflush(stdin);
    printf("Enter your password > ");
    scanf("%[^\n]", password);

    system("cls");

    flag=CheckPass(user_id, password);

    if(flag==1)
    {
    AccessGranted();
    }
    else
    {
    printf("\nACCESS DENIED\nUser ID and password does not match\n\n");
    count--;
    if(count==0)
    printf("\nYou have no more attempt(s) left.\nProgram terminated\n\n");
    }
    }while(count>0);

    return 0;
    }


    int CheckPass(char *user_id, char *password)
    {
    FILE *fpPass;

    char fuser_id[20], fpassword[20];
    int i, temp;
    int found=0;

    if(!(fpPass=fopen("Password.txt", "r")))
    {
    printf("Error: Unable to open password file.\nSorry for the inconvenience\n\n");
    exit(0);
    }

    while(fscanf(fpPass, "%[^\n]\n%[^\n]\n", fuser_id, fpassword)!=EOF && !found)
    {
    /*Check for valid user ID*/
    if(strlen(fuser_id)==strlen(user_id))
    {
    for(i=0, temp=strlen(fuser_id); i<temp; i++)
    {
    found=1;
    if(*(user_id+i)!=fuser_id[i])
    found=0;
    }

    if(found==0)
    return 2;
    else
    {
    /*Check for valid password*/
    if(strlen(password)!=strlen(fpassword))
    return 0;
    else
    {
    for(i=0, temp=strlen(fpassword); i<temp; i++)
    if(*(password+i)!=fpassword[i]-2)
    return 0;
    else
    return 1;
    }
    }
    }
    fflush(stdin);
    }

    fclose(fpPass);
    }

    void AccessGranted()
    {
    printf("\nACCESS GRANTED\n\n");
    printf("Thank you for using this system.\n\n");
    exit(0);
    }


    The contain of Password.txt is:
    joe
    dcvocp
    joe1
    dcvocp1

    Problem:
    When I key in joe (correct user ID) in the first attempt and
    bat (incorrect password), Access is Denied. But, for the second attempt, I only need to key in a wrong user id (ie. joey) and correct password (batman) to access granted. Why??

    Also: What is the function og the conio.h header file?

    Thanks in advance!!!
    It's unfulfilled dreams that keep you alive.

    //netboy

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( fscanf( fpPass, "%[^\n]\n%[^\n]\n", fuser_id, fpassword)!=EOF ) 
    { 
        /*Check for valid user ID*/ 
        if(strlen(fuser_id)==strlen(user_id)) 
        {
            found = 0;
            for(i=0, temp=strlen(fuser_id); i<temp; i++) 
                if( user_id[i] != fuser_id[i] ) { found = 0; break; }
            found = 1;
        }
        if( found == 0 )
        {
            fclose( fpPass ); /* need to remember this */
            return 2;
        }
        else 
        { 
            /*Check for valid password*/ 
            if( strlen(password) != strlen(fpassword) ) 
            {
                fclose( fpPass ); /* need to remember this */
                return 0;
            }
            else 
            {
                for(i=0, temp=strlen(fpassword); i<temp; i++)
                    if( password[i] != (fpassword[i] - 2) )
                    {
                        fclose( fpPass ); /* need to remember this */
                        return 0;
                    }
                fclose( fpPass );
                return 1; 
            }
        } 
    }
    You'll quickly see the problem with having multiple returns in a single function. You have to make sure each time you return, that you've cleaned up after yourself. (This means making sure you close your file pointers.) Perhaps it would be better to rewrite it so you're only setting a value which is returned at the end? Additionally, I prefer to simply use them (pointers) as arrays rather than dereferencing them + some number.

    Also, stop fflusing(stdin). It is not an ANSI function, and while it may work on your current compiler, it's behaviour is undefined, meaning, that if you take this code elsewhere, you may get wierd behaviour.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12

  13. #13
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158

    Question

    Code:
    do
    	{
    		password[x]=getch();
    		if(password[x]=='\r') /*Check for return key*/
    			break;
    		
    		else if(password[x]=='\b') /*Check for backspace key*/
    		{
    			if(x==0)
    			{
    				continue;
    			}
    			else
    			{
    				putchar('\b');
    				putchar(' ');
    				x--;
    			}
    		}
    
    		else /*User still keying in password*/
    		{
    			x++;
    		}
    
    	}while(password[x]!='\r');
    	password[x]='\0';
    The above code hides the characters the user is typing when keying his/her password. I found a problem. When I use this code, if I type in the wrong password for the first attempt, I willl still get ACCESS DENIED even if I key in the correct ID and password in the second attempt. However, if I simply use the scanf("%s", %password) function which doesn't hide the password characters, the problem won't appear. The following code is the function to check for valid ID and password.

    Code:
    int CheckPass(char *ID, char *password)
    {
    	FILE *fpPass;
    
    	char fuser_id[20], fpassword[20];
    	int i, temp;
    	int found=0;
    
    	if(!(fpPass=fopen("Password.txt", "r")))
    	{
    		printf("Error: Unable to open password file.\nSorry for the inconvenience\n\n");
    		exit(0);
    	}
    
    	while(fscanf(fpPass, "%[^\n]\n%[^\n]\n", fuser_id, fpassword)!=EOF && !found)
    	{
    		/*Check for valid user ID*/
    		if(strlen(fuser_id)==strlen(ID))
    		{
    			for(i=0, temp=strlen(fuser_id); i<temp; i++)
    			{
    				found=1;
    				if(*(ID+i)!=fuser_id[i])
    				{
    					fclose(fpPass);
    					found=0;
    				}
    			}
    	
    			if(found==0)
    			{
    				fclose(fpPass);
    				return 0;
    			}
    			else
    			{
    				/*Check for valid password*/
    				if(strlen(password)!=strlen(fpassword))
    				{
    					fclose(fpPass);
    					return 0;
    				}
    				else
    				{
    					for(i=0, temp=strlen(fpassword); i<temp; i++)
    						if(*(password+i)!=fpassword[i]-2)
    						{
    							fclose(fpPass);
    							return 0;
    						}
    						else
    						{
    							fclose(fpPass);
    							return 1;
    						}
    				}
    			}
    		}
    	}
    
    	return 0;
    }
    Please help!!!
    It's unfulfilled dreams that keep you alive.

    //netboy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With Program
    By mustanghaven in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2008, 06:28 PM
  2. Password Program Question
    By SirTalkAlot415 in forum C++ Programming
    Replies: 13
    Last Post: 11-06-2007, 12:35 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Password recovery program..
    By ExDigit in forum Windows Programming
    Replies: 1
    Last Post: 01-12-2002, 09:45 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM