Thread: password masking

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    Unhappy password masking

    I designed a program in C that I would like to use passwords for, but I can't seem to make a function for masking the passwords. I figure that it has something to do with kbhit(); in <conio.h>, but I can't figure out how to properly use that function in this case. Anybody have an answer to this problem?

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    kbhit() shouldn't be necessary but you'd probably want to write your own fgets() using getch().
    Read a character, then gotoxy(wherex()-1, wherey()) and putchar('*') and repeat until a newline is found.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    That would depend on your OS and compiler.
    Particularly, since you mention conio.h, are you programming for real DOS or merely using a DOS compiler in a win32 console, or perhaps even a proper 32 bit compiler in a win32 console.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    OS and Compiler

    I'm using Digital Mars C/C++ compiler for Windows, and I'm on Windows XP.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    ok...

    heres some code I came up with for a function for this crap. Maybe you can tell me what im doing wrong:

    Code:
    #include <stdio.h>
    #include <string.h>
    #inlcude <conio.h>
    
    void passgets(char password[], int maxlength)
    {
      int n, a = 0;
      char s[1];
    
      for (n = getch(); a < maxlength; ++a)
      {
        itoa(n, s);
        if (s[1] == '\n' || s[1] == '\r')
        {
          a = maxlength;
        }
        strcat(password, s);
        printf("*");
        s[1] = '\0';
      }
    }
    can anyone tell me what the heck is wrong with this code?

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Going out of bonds and unnecessarily converting an int to a char array.
    You can compare n with '\n' and '\r' without problem.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    well...

    how do i do that? I dont really understand what you mean. Since getch() produces an int value, how am i supposed to get the characters in the password to use or display them? all ill have is a bunch of numbers!

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    also...

    whats going out of bounds? one of the characters, i assume. what do i do to avoid the use of itoa and things like that?

  9. #9
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    You made an array of 1 char. The first and also last element of it is s[0] but in the code you used s[1].
    The fix would be to change s to an array of 2 characters and change the if-statement comparson to use s[0] and have the s[1] = '\0'; come before the strcat(). And itoa() doesn't make much sense either. You probably just wanted s[0] = n;

    Try this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #inlcude <conio.h>
    
    void passgets(char password[], int maxlength)
    {
      int n, a;
    
      for (a = 0; a < maxlength; ++a)
      {
        n = getch();
        if (n == '\n' || n == '\r')
          break;
        password[a] = n;
        putchar('*');
      }
      password[a] = '\0';
    }
    [edit] No wait, getch() didn't output the character so it's unnecessary.
    Last edited by OnionKnight; 03-09-2006 at 03:20 PM.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    thanks

    well, you definitely showed me my error, but what is gotoxy()? is it a function defined in <conio.h>? I've never used it before.

  11. #11
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Yes, it's a conio.h function. It allows you to move around in the console screen. wherex() and wherey() are also conio functions and will return the position the console cursor is for those axises.
    Also, look at the edit. You don't have to use it.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    ok

    well, thats a big help. thanks a ton.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    what s wrong now?

    Code:
    #include <stdio.h>
    #include <string.h>
    #inlcude <conio.h>
    
    void passgets(char password[], int maxlength)
    {
      int n, a;
    
      for (a = 0; a < maxlength; ++a)
      {
        n = getch();
        if (n == '\n' || n == '\r')
          break;
        password[a] = n;
        putchar('*');
      }
      password[a] = '\0';
    }
    this compiled with no errors, but now, whenever I type the password, no characters are displayed until you press enter. what do i do about that?

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    i think i got it:

    Code:
    #include <stdio.h>
    #include <string.h>
    #inlcude <conio.h>
    
    void passgets(char password[], int maxlength)
    {
      int n, a;
    
      for (a = 0; a < maxlength; ++a)
      {
        n = getch();
        if (n == '\n' || n == '\r')
          break;
        password[a] = n;
        putchar('*');
      }
      password[a] = '\0';
    }
    password[a] = n;
    putchar('*');

    this was being included in the if statement.

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    nope

    well, that didnt work. any other ideas, because this is really starting to make me mad....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char swaps, and echo() password masking
    By spencerhs5 in forum C Programming
    Replies: 2
    Last Post: 07-25-2006, 08:25 PM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 03:42 AM
  3. Masking password input
    By cboard_member in forum C++ Programming
    Replies: 9
    Last Post: 07-18-2005, 06:47 PM
  4. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM