A Digital Age Deserves A Digital Leader

Tutorial: C# .NET Password Strength using Regular Expressions

Tutorial: C# .NET Password Strength using Regular Expressions

Postby jbullard » Sun Nov 26, 2006 7:49 am

Language: C# .NET 2005

Experience: Intermediate

Time to Learn: 30 minutes

Description: This was written to facilitate users when trying to do basic password strengthening for clients. It will enable you to physically show your users that a password may/may not be strong enough to protect against crackers.


So, I basically was just creating a program and figured I would add some niceties for my users. I thought that a password strengthener would be very nice so I could tell them whether or not their password was good enough. In this example I used RegularExpressions and a Password Point scale (fully customizeable).

Regular Expressions
If you have never used RegEx it may seem like it is overwhelming to learn all this stuff. In reality it is not. I have seen people spend hours on end creating a list of characters to use in there queries of strings and then spend hours creating a custom method to facilitate their search. These people spent all day on something to find out that it didn't work and they went back to the drawing board. Where I have used RegEx to do the same thing and with less code, better code. If you don't know Regular Expressions, don't become overwhelmed by all the slashes and dots. It is just special things that the RegEx praser uses.

Password Points
The password points was created just because. I could have easily used a different standard for evaluating where the password actually stands. However, math never lies, plus it is a very basic feature and doesn't require large amounts of code to do the same trick.


Step 1: Create a Windows Application in Visual Studio 2005 or Visual C# .NET Express Edition 2005
- I named mine WNDAPP

Step 2: Add a Class Library project to your current project
- I named mine crypt

Step 3: In your class add the following:
Code: Select all
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;



Step 4: Create the GetPasswordStrength() Method
Code: Select all
namespace crypt
{
    public class crypt
    {
        public static String GetPasswordStrength(String inPassword)
        {
        }
    }
}



Step 5: Create variables
- lCaseReg = regular expression of lower case characters
- uCaseReg = regular expression of upper case characters
- nCaseReg = regular expression of numeric characters
- sCaseReg = regular expression of special characters

- lCaseRegCnt = Total matches of lower case characters
- uCaseRegCnt = Total matches of upper case characters
- nCaseRegCnt = Total matches of numeric characters
- sCaseRegCnt = Total matches of special characters

- passwordPoints = Total number of points earned

- passBegin = A sentence I used to show in a nice display
- passStrength = A list of strength words (Weak, Medium, Sat, Strong)
[code]
Regex lCaseReg = new Regex(".[a-z].");
Regex uCaseReg = new Regex(".[A-Z].");
Regex nCaseReg = new Regex(".[0-9].");
Regex sCaseReg = new Regex(".[
VP - Software
User avatar
Posts: 3653
Joined: Sun Jun 06, 2004 10:17 pm
Location: Utah
Real Name: Jason Bullard

Return to HTML, CSS, and Scripts

Who is online

Users browsing this forum: No registered users and 15 guests