Solved

Generate Complex Password

Posted on 2010-09-02
3
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 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