Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Generate Complex Password

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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