Friday, May 11, 2012

Password STRENGTH and VALIDITY checking C program

/* password Strength and Validity checking C program-

This program takes your password and checks weather its valid
are not.
If valid then its checks the password strength and gives
one of the following result : LOW, MEDIUM, HIGH, VERY HIGH.


note-this is my algorithm(most followed), you can change as you need.
*/


#include stdio.h #include conio.h #include string.h #define SIZE 32 short int checkValid(char*); void checkStrength(char*); short int alphabetCount=0, digitCount=0; short int length=0; short int symbolCount=0; //to count symbols like ;'./ etc void main() { char pw[SIZE],c; short int count,error,i=0; clrscr();
printf("enter the password:"); while( (c=getch()) !='\r') //this loop displays { //'*' for the chars you pw[i]=c; //enter putchar('*'); i++; } pw[i]='\0'; length=strlen(pw); if( error=checkValid(pw) ) { printf("\n\nits a valid password"); checkStrength(pw); } else printf("\n\n its not a valid password"); printf("\n\n entered password is- %s",pw); getch(); } short int checkValid(char *x) { if(length<6) printf("\n password should be atleast 6 characters wide"); while((*x)!='\0') { if( isalpha(*x)) alphabetCount++; //alphabet, digits and symbol else if( isdigit(*x) ) //count are made to check the digitCount++; //password strength else if( iscntrl(*x) ) { printf("\ncontrol characters are not allowed"); return 0; } else if( isascii(*x) ) symbolCount++; x++; } if(alphabetCount==length) //this condition is true when u enter { //only alphabets, which is invalid printf("\n password should contain both alphabets and digits"); return 0; } else if(digitCount==length) //this condion is true when u enter { //only digits, which is invalid printf("\n password should contain both alphabets and digits"); return 0; } else return 1; } void checkStrength(char *p) { short int strengthVar=0; if( length>8 ) strengthVar++; if( symbolCount >= 1 ) strengthVar++; if( digitCount >= alphabetCount ) strengthVar++; if( length>12 ) strengthVar++; printf("\n\n\tPASSWORD STRENGTH : "); switch(strengthVar) { case 0 :printf("low\n"); break; case 1 :printf("medium\n"); break; case 2 :printf("fair\n"); break; case 3 :printf("high\n"); break; case 4 :printf("very high\n"); break; } }

3 comments:

  1. Many thanks for sharing well information.Password issue is very important in our computing world,because most of have acquainted with internet service for our commercial,social and education,research field.To chase our requirement and demand we have to utilize a unique password for our safety issue.So your information will surely helpful for us to check our password strength.

    ReplyDelete
  2. Thank you! I was looking for something that did exactly this. Since I was compiling for linux, I had to get around using conio.h, getch(), and clrscr().

    I found help with it here: http://www.cplusplus.com/forum/beginner/33529/

    The code as modified below should be portable between different OS'es.

    Thanks again!

    Erik

    /* password Strength and Validity checking C program-

    This program takes your password and checks weather its valid
    are not.
    If valid then its checks the password strength and gives
    one of the following result : LOW, MEDIUM, HIGH, VERY HIGH.


    note-this is my algorithm(most followed), you can change as you need.
    */


    #include
    /* #include */
    #include
    #define SIZE 32

    short int checkValid(char*);
    void checkStrength(char*);

    short int alphabetCount=0, digitCount=0;
    short int length=0;
    short int symbolCount=0; //to count symbols like ;'./ etc

    void main()
    {
    char pw[SIZE],c;
    short int count,error,i=0;
    system("clear");

    #if defined(WINDOWS)
    static DWORD stdin_mode;
    #elif defined(POSIX)
    static tcflag_t c_lflag = tty_attr.c_lflag;
    #endif

    int save_tty_attributes()
    {
    #if defined(WINDOWS)
    HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);

    if (!GetConsoleMode(hstdin, &mode))
    return -1;

    if (hstdin == INVALID_HANDLE_VALUE || !(SetConsoleMode(hstdin, 0)))
    return -1; /* Failed to disable buffering */
    #elif defined(POSIX)
    struct termios tty_attr;

    if (tcgetattr(STDIN_FILENO, &tty_attr) < 0)
    return -1;

    tty_attr.c_lflag &= ~ICANON;
    tty_attr.c_lflag &= ~ECHO;

    if (tcsetattr(STDIN_FILENO, 0, &tty_attr) < 0)
    return -1;
    #endif
    }

    int restore_tty_attributes()
    {
    #if defined(WINDOWS)
    if (!SetConsoleMode(hstdin, mode))
    return -1;
    #elif defined(POSIX)
    tty_attr.c_lflag = c_lflag;

    if (tcsetattr(STDIN_FILENO, 0, &tty_attr) < 0)
    return -1;
    #endif
    }

    int sgetchar()
    {
    int c = 0;
    save_tty_attributes();
    c = getchar();
    restore_tty_attributes();
    return c;
    }

    printf("enter the password:");
    while( (c=sgetchar()) !='\n') //this loop displays
    { //'*' for the chars you
    pw[i]=c; //enter
    putchar('*');
    i++;
    }
    pw[i]='\0';
    length=strlen(pw);

    if( error=checkValid(pw) )
    {
    printf("\n\nits a valid password");
    checkStrength(pw);
    }
    else
    printf("\n\n its not a valid password");

    printf("\n\n entered password is- %s",pw);
    sgetchar();

    }

    short int checkValid(char *x)
    {
    if(length<6)
    printf("\n password should be atleast 6 characters wide");

    while((*x)!='\0')
    {
    if( isalpha(*x))
    alphabetCount++; //alphabet, digits and symbol
    else if( isdigit(*x) ) //count are made to check the
    digitCount++; //password strength
    else if( iscntrl(*x) )
    {
    printf("\ncontrol characters are not allowed");
    return 0;
    }
    else if( isascii(*x) )
    symbolCount++;

    x++;
    }

    if(alphabetCount==length) //this condition is true when u enter
    { //only alphabets, which is invalid
    printf("\n password should contain both alphabets and digits");
    return 0;
    }
    else if(digitCount==length) //this condion is true when u enter
    { //only digits, which is invalid
    printf("\n password should contain both alphabets and digits");
    return 0;
    }
    else
    return 1;
    }

    void checkStrength(char *p)
    {
    short int strengthVar=0;

    if( length>8 )
    strengthVar++;
    if( symbolCount >= 1 )
    strengthVar++;
    if( digitCount >= alphabetCount )
    strengthVar++;
    if( length>12 )
    strengthVar++;

    printf("\n\n\tPASSWORD STRENGTH : ");
    switch(strengthVar)
    {
    case 0 :printf("low\n");
    break;
    case 1 :printf("medium\n");
    break;
    case 2 :printf("fair\n");
    break;
    case 3 :printf("high\n");
    break;
    case 4 :printf("very high\n");
    break;
    }

    }

    ReplyDelete