Solved

Auto Password script

Posted on 2009-03-30
18
266 Views
Last Modified: 2013-11-10
Hi,
I'm looking to create an automatic way that will create a password using a script to output it in a message box and copy it to the clipboard.

The password must:
"Be at least 8 positions in length but up to 10.
"Contain at least one English letter and one digit character
"Should not contain a digit character in the first and last position!(0-9)
"Should not contain identical consecutive characters (aa, ##, 88.)
"Should not contain consecutive keyboard characters (qwer)

For Example:

Correct password will be  C0mputer
(8 letters, starts and ends with an English letter)

Incorrect password - Monitor1
(ends with a digit character)

Incorrect password - monitor
(there is no digit character, less than 8 positions in length.)
0
Comment
Question by:johnnyjonathan
  • 10
  • 8
18 Comments
 

Author Comment

by:johnnyjonathan
ID: 24020828
Forgot, also it cannot containt the word "help" or "desk"
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24022923
>> "Should not contain consecutive keyboard characters (qwer)

This one's going to be just about impossible.  If that were a strict rule,
C0mputer

would not valid either because e and r are consecutive on the keyboard.

Check this article out for validating a password:
http://www.microsoft.com/technet/scriptcenter/funzone/games/solutions08/avbsol05.mspx

Regards,

Rob.
0
 

Author Comment

by:johnnyjonathan
ID: 24023482
your right, maybe i'm getting too strict :)
anyway without this rule ?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24023984
Yeah, the rest should be doable.....do you want to validate a user-inputted password, or do want a password automatically created?  The latter will be more difficult, but I have an idea of how to do it....

Regards,

Rob.
0
 

Author Comment

by:johnnyjonathan
ID: 24024043
I'm looking for the latter, just a one click script that can auto generate the password according to the rules mentioned before.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24024486
OK, finally got there.

Give this a shot.

Regards,

Rob.
' Basis from here:

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

' Question link:

'http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24276924.html

'

' Password rules:

' The password must: 

' "Be at least 8 positions in length but up to 10.

' "Contain at least one English letter and one digit character

' "Should not contain a digit character in the first and last position!(0-9)

' OMITTED: "Should not contain identical consecutive characters (aa, ##, 88.)

' The last rule was omitted due to the complexity of it.
 

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 english character

	If boolValid = True Then

		objRegEx.Pattern = "[a-z][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 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 Comment

by:johnnyjonathan
ID: 24026191
it works great.
one note though, can you please let me know how i can turn off the special characters addition, and the CAPS letters in ABC, to make sure it will stay coordinated with future policy?
0
 

Author Comment

by:johnnyjonathan
ID: 24026761
Also, sorry i forgot this, but it must start and end with a letter is there an option to set it?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24033093
OK, so by omitting special characters, do you basically want only lowercase letters, and digits available for use?  The ASCII table can be found here:
http://asciitable.com/

I can make the password only include characters from decimal codes 48 through 57 for digits, and 97 through 122 for lowercase letters.

Regards,

Rob.
0
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.

 

Author Comment

by:johnnyjonathan
ID: 24034712
That's correct, however like i said, it must start and end with a letter as well.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 24034992
>>  it must start and end with a letter as well.

This will happen anyway if we restrict the random characters to digts and lowercase letters only.

This should work.

Regards,

Rob.
' Basis from here:

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

' Question link:

'http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24276924.html

'

' Password rules:

' The password must: 

' "Be at least 8 positions in length but up to 10.

' "Contain at least one English letter and one digit character

' "Should not contain a digit character in the first and last position!(0-9)

' "No special characters - lower case letters and digits only

' "Must begin and end with a letter

' OMITTED: "Should not contain identical consecutive characters (aa, ##, 88.)

' The last rule was omitted due to the complexity of it.
 

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))

		If (Asc(strChr) >= 48 And Asc(strChr) <= 57) Or (Asc(strChr) >= 97 And Asc(strChr) <= 122) Then 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 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 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:johnnyjonathan
ID: 31564286
Works great! thank you
0
 

Author Comment

by:johnnyjonathan
ID: 24038564
Works like a charm, tell me please, for the future, is there an option i can block specific words from appearing even by random?
 
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24043437
Yeah sure, if you make your Validate_Password like this, it will invalidate any passwords that match words in the array.

Regards,

Rob.
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 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 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

	' Prevent specific words from being the password

	Set objDict = CreateObject("Scripting.Dictionary")

	objDict.Add "password", 0

	objDict.Add "workman", 0

	objDict.Add "hospital", 0

	If objDict.Exists(strValPwd) = True the boolValid = False

	' Return the valid code

	ValidatePassword = boolValid

End Function

Open in new window

0
 

Author Comment

by:johnnyjonathan
ID: 24043787
great, understood.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24044700
Although.....that won't actually stop passwords like
hospital1a

so, I've now changed it to an InStr match, so the word can *not* be anywhere in the whole password.

Regards,

Rob.
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 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 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

	' Prevent specific words from being the password

	arrWords = Array( _

		"help", _

		"desk", _

		"hospital" _

	)

	For Each strWord In arrWords

		If InStr(strValPwd, strWord) > 0 Then boolValid = False

	Next

	' Return the valid code

	ValidatePassword = boolValid

End Function

Open in new window

0
 

Author Comment

by:johnnyjonathan
ID: 24075893
Thank you Rob,
any way i can export the password to the clipbrd as well ?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24082531
Yeah sure, this should work.

Regards,

Rob.
' Basis from here:

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

' Question link:

'http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24276924.html

'

' Password rules:

' The password must: 

' "Be at least 8 positions in length but up to 10.

' "Contain at least one English letter and one digit character

' "Should not contain a digit character in the first and last position!(0-9)

' "No special characters - lower case letters and digits only

' "Must begin and end with a letter

' OMITTED: "Should not contain identical consecutive characters (aa, ##, 88.)

' The last rule was omitted due to the complexity of it.

 

Const MinLength = 8

Const MaxLength = 10

 

While ValidatePassword(strPassword) = False

	strPassword = GeneratePassword

Wend
 

CopyToClipboard strPassword
 

MsgBox strPassword & " has been copied to the clipboard."

 

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))

		If (Asc(strChr) >= 48 And Asc(strChr) <= 57) Or (Asc(strChr) >= 97 And Asc(strChr) <= 122) Then 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 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 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

	' Prevent specific words from being the password

	arrWords = Array( _

		"help", _

		"desk", _

		"hospital" _

	)

	For Each strWord In arrWords

		If InStr(strValPwd, strWord) > 0 Then boolValid = False

	Next

	' Return the valid code

	ValidatePassword = boolValid

End Function
 

Sub CopyToClipboard(strTextForClipboard)

	Set objIE = CreateObject("InternetExplorer.Application")

	objIE.Navigate("about:blank")

	objIE.document.parentwindow.clipboardData.SetData "text", strTextForClipboard

	objIE.Quit

End Sub

Open in new window

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

Suggested Solutions

Title # Comments Views Activity
tidtcpserver connection lost handle 2 69
Script is being strange 8 62
how to update exe applicatio from internet ? 6 66
VB Script to add site to Java Exception List 4 48
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

943 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

7 Experts available now in Live!

Get 1:1 Help Now