Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Generate Complex Password

Posted on 2010-09-02
3
Medium Priority
?
483 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 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

886 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