Solved

Generate Complex Password

Posted on 2010-09-02
3
474 Views
Last Modified: 2012-06-21
I need to create a vb script that generates a complex password with the following:

8 Characters in length
1 Upper Case
1 Lower Case
1 Number
1 Special Character

The script needs to ensure that it contains at lease one of each of the criteria. I have found quite a few scripts,but they all rely on the random generator, and cannot guarantee each of the values.
0
Comment
Question by:brendanlefavre
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 33592279
Hi, I have built this....I'm not that great with regular expressions, so it could probably be simplified, but it should do the job, nevertheless.

Regards,

Rob.
' Basis from here:

'http://www.microsoft.com/technet/scriptcenter/funzone/games/solutions08/avbsol05.mspx



Const MinLength = 8

Const MaxLength = 10



While ValidatePassword(strPassword) = False

	strPassword = GeneratePassword

Wend

MsgBox strPassword



Function GeneratePassword()



	strRndPwd = ""

	

	If MinLength = "" Or MaxLength = "" Then

		WScript.Echo "Min and max lengths are not defined. Please set MinLength and MaxLength globally."

		Exit Function

	End If

	

	Randomize

	intLength = Int((MaxLength - MinLength + 1) * Rnd + MinLength)



	Const AsciiLower = 32

	Const AsciiUpper = 126



	While Len(strRndPwd) <= intLength

		'Randomize

		strChr = Chr(Int((AsciiUpper - AsciiLower + 1) * Rnd + AsciiLower))

		strRndPwd = strRndPwd & strChr

	Wend



	GeneratePassword = strRndPwd

End Function



Function ValidatePassword(strValPwd)

	Set objRegEx = CreateObject("VBScript.RegExp")

	boolValid = True

	' Check the length requirement

	If Len(strValPwd) < MinLength Or Len(strValPwd) > MaxLength Then boolValid = False

	' Look for an lowercase english character

	If boolValid = True Then

		objRegEx.Pattern = "[a-z]"

		Set colMatches = objRegEx.Execute(strValPwd)  

		If colMatches.Count = 0 Then boolValid = False

	End If

	' Look for an uppercase english character

	If boolValid = True Then

		objRegEx.Pattern = "[A-Z]"

		Set colMatches = objRegEx.Execute(strValPwd)  

		If colMatches.Count = 0 Then boolValid = False

	End If

	' Look for a digit

	If boolValid = True Then

		objRegEx.Pattern = "[0-9]"

		Set colMatches = objRegEx.Execute(strValPwd)  

		If colMatches.Count = 0 Then boolValid = False

	End If

	' Look for a non alphanumeric character

	If boolValid = True Then

		objRegEx.Pattern = "^[a-z][A-Z][0-9]"

		Set colMatches = objRegEx.Execute(strValPwd)  

		If colMatches.Count = 0 Then boolValid = False

	End If

	' Look for a digit in the first or last position

	'If boolValid = True Then

	'	strFirstChr = Left(strValPwd, 1)

	'	strLastChr = Right(strValPwd, 1)

	'	If IsNumeric(strFirstChr) Or IsNumeric(strLastChr) Then boolValid = False

	'End If

	ValidatePassword = boolValid

End Function

Open in new window

0
 

Author Closing Comment

by:brendanlefavre
ID: 33592371
thank you so much, this is exactly what i was looking for.

Cheers,
Brendan
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33592386
No problem. Thanks for the grade.

Regards,

Rob.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now