Solved

Generate Complex Password

Posted on 2010-09-02
3
472 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

14 Experts available now in Live!

Get 1:1 Help Now