Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

Script to create random password

I have the code below, it needs to be tweaked and also for some reason the value of the variable is not working. The variable on the ASP shows nothing.

<html>
<head>
<!--#include file="../../includes/bdot/scripts.asp"-->
<meta charset="utf-8">
<title></title>

<script language="vbscript" runat="server">
    Const LETTERS  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Const SYMBOLS  = "~!@#$%^&*()_+-={}|[]\:"";'<>?,./"
    Dim strPassword
    Dim intIndex
    
    Randomize
    
    ' Generate the password string making the 1st character a letter
    strPassword = Mid(LETTERS, Int((26) * Rnd + 1), 1) & "     "
    ' Randomly generate the 2nd through 6th characters.
    ' Second letter
    intIndex = Int((5) * Rnd + 2)
    Do Until Mid(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left(strPassword, intIndex - 1) & Mid(LETTERS, Int((26) * Rnd + 1), 1) & Mid(strPassword, intIndex + 1)
    ' First symbol
    intIndex = Int((5) * Rnd + 2)
    Do Until Mid(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left(strPassword, intIndex - 1) & Mid(SYMBOLS, Int((31) * Rnd + 1), 1) & Mid(strPassword, intIndex + 1)
    Do Until Mid(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left(strPassword, intIndex - 1) & Mid(SYMBOLS, Int((31) * Rnd + 1), 1) & Mid(strPassword, intIndex + 1)
    ' First number
    Do Until Mid(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left(strPassword, intIndex - 1) & Int((10) * Rnd) & Mid(strPassword, intIndex + 1)
    ' Second number
    Do Until Mid(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left(strPassword, intIndex - 1) & Int((10) * Rnd) & Mid(strPassword, intIndex + 1)
    
   
</script>
</head>

<body>
<p>password = <%=strPassword%></p>

</body>
</html>

Open in new window

One more thing. It seems to include letters and characters, it should also include numbers. And the password should be 8 characters long.

The password = <%=strPassword%>  shows nothing on the ASP page.
Avatar of aikimark
aikimark
Flag of United States of America image

Be aware of the shortcomings of the VB random number functions.  They do not produce unique results.  I detail this in my article: http://rdsrc.us/fErAk9

I will look at your code

It seems to include letters and characters, it should also include numbers. And the password should be 8 characters long.
Let's first define the pwd requirements
* eight characters long
* contains upper case alphabetic character(s), numeric digit(s), symbol(s) -- how many of each?
* first pwd character must be alphabetic.


The password = <%=strPassword%>  shows nothing on the ASP page.
do you see the password = string on the page?

=======================
Add a msgbox strPassword statement at the bottom of your script
What is invoking your script?  I would expect to see a button click event or window load event.
Avatar of Aleks

ASKER

I am open to using any other type of code. We need 8 characters long, 2 Caps, 2 numbers and 2 characters.
The string doesn't show on the page.

We can use javascript instead ?
The string doesn't show on the page.
Do you see anything on the page?
Does the first character need to be a cap letter?
Avatar of Aleks

ASKER

Its a plus

Once I added the mssg box code I get this:

Microsoft VBScript runtime  error '800a0046'

Permission denied: 'msgbox'

/BlueDot/Intranet/Contacts/webaccess_SPadd.asp, line 122
how about wscript.echo instead of msgbox
Avatar of Aleks

ASKER

Microsoft VBScript runtime  error '800a01a8'

Object required: 'wscript'

/BlueDot/Intranet/Contacts/webaccess_SPadd.asp, line 122
This code generates pseudo-random passwords.  However, your symbols include characters that might cause the browser to mis-render the generated pwd string.  I used a <span> tag.  Perhaps a different tag might facilitate proper symbol rendering.
<html>
<head>
<!--#include file="../../includes/bdot/scripts.asp"-->
<meta charset="utf-8">
<title></title>

<script language="vbscript" runat="server">
sub GeneratePwd()
    Const LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Const SYMBOLS = "~!@#$%^&*()_+-={}|[]\:"";'<>?,./"
    Const Digits = "01233456789"
	Dim strPassword
    Dim CharClass(2, 1)
    Const cLet = 0
    Const cSym = 1
    Const cDig = 2
    Dim intIndex
    Dim intRnd
    Dim alphabet
    CharClass(cLet, 0) = 0
    CharClass(cSym, 0) = 0
    CharClass(cDig, 0) = 0
    CharClass(cLet, 1) = LETTERS
    CharClass(cSym, 1) = SYMBOLS
    CharClass(cDig, 1) = Digits

' contingency if rnd not good enough
'    Dim strGUID
'    strGUID = Mid(CreateObject("scriptlet.typelib").GUID, 2, 36)
    
    Randomize
    
    ' Generate the password string making the 1st character a letter
    strPassword = Mid(CharClass(cLet, 1), Int(Len(CharClass(cLet, 1)) * Rnd) + 1, 1)
    CharClass(cLet, 0) = 1
    
    'concatenate characters until len(strpassword)=6
    Do
        'build the alphabet from classes with < 2 characters already present
        alphabet = vbNullString
        For intIndex = LBound(CharClass) To UBound(CharClass)
            If CharClass(intIndex, 0) < 2 Then
                alphabet = alphabet & CharClass(intIndex, 1)
            End If
        Next
        'add a character
        intRnd = Int(Len(alphabet) * Rnd) + 1
        strPassword = strPassword & Mid(alphabet, intRnd, 1)
        'increment the class for the just added character
        For intIndex = LBound(CharClass) To UBound(CharClass)
            If InStr(CharClass(intIndex, 1), Mid(alphabet, intRnd, 1)) <> 0 Then
                CharClass(intIndex, 0) = CharClass(intIndex, 0) + 1
                Exit For
            End If
        Next
    Loop Until Len(strPassword) = 6
    
    'all classes included for last two characters
    alphabet = vbNullString
    For intIndex = LBound(CharClass) To UBound(CharClass)
        alphabet = alphabet & CharClass(intIndex, 1)
    Next
    Do
        strPassword = strPassword & Mid(alphabet, Int(Len(alphabet) * Rnd) + 1, 1)
    Loop Until Len(strPassword) = 8

    document.getElementById("pwd").innerHTML = strPassword

	End Sub    
</script>
</head>

<body onload="GeneratePwd()">
<p>password = <span id=pwd></span></p>

</body>
</html>

Open in new window

Ok.  The statement I was thinking about was Alert.

Be aware that vbscript is fine for IE browsers, but not universally supported.  My default browser is Chrome and it took me a while to realize why the script wasn't running.
Avatar of Aleks

ASKER

We can remove the characters that may cause an issue. But I do need help creating the script, because obviously the one here doesn't work properly. Even on IE.
try this code, taken and modified to include TWO upper case characters from here:

<%
function RandomString()

    Randomize()

    dim CharacterSetArray
    CharacterSetArray = Array(_
        Array(5, "abcdefghijklmnopqrstuvwxyz"), _
        Array(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), _
        Array(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), _
        Array(1, "0123456789"), 
    )

    dim i
    dim j
    dim Count
    dim Chars
    dim Index
    dim Temp

    for i = 0 to UBound(CharacterSetArray)

        Count = CharacterSetArray(i)(0)
        Chars = CharacterSetArray(i)(1)

        for j = 1 to Count

            Index = Int(Rnd() * Len(Chars)) + 1
            Temp = Temp & Mid(Chars, Index, 1)

        next

    next

    dim TempCopy

    do until Len(Temp) = 0

        Index = Int(Rnd() * Len(Temp)) + 1
        TempCopy = TempCopy & Mid(Temp, Index, 1)
        Temp = Mid(Temp, 1, Index - 1) & Mid(Temp, Index + 1)

    loop

    RandomString = TempCopy

end function
%>

Open in new window


and to use it:

<%=RandomString()%>

or

<%
strPassword = RandomString()
Response.Write strPassword
%>
Avatar of Aleks

ASKER

I got this error:

Microsoft VBScript compilation  error '800a03ea'

Syntax error

/bluedot/Intranet/Contacts/webaccess_SPadd.asp, line 94
Array(1, "0123456789"),
-----------------------^
sorry, get rid of the trailing comment on that line
Avatar of Aleks

ASKER

You meant comma ?

I did but got this error:

Microsoft VBScript compilation  error '800a03ee'

Expected ')'

/bluedot/Intranet/contacts/webaccess_SPadd.asp, line 94
Array(1, "0123456789")
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

PERFECT !!!!   If I wanted the password to be longer than 8 characters where can I change the code to make it longer ?
Avatar of Aleks

ASKER

Perfect !
Avatar of Aleks

ASKER

I changed the array to this:

  CharacterSetArray = Array(_
         Array(5, "abcdefghijklmnopqrstuvwxyz"), _
         Array(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), _
         Array(1, "!@#%^&*+"), _
         Array(1, "0123456789")  _
     )

So it includes characters
One more thing. I does not include characters, just realized you have to sets of letters instead. Can this be fixed ?

not sure what you mean, can you elaborate?

If I wanted the password to be longer than 8 characters where can I change the code to make it longer ?

the first index of each sub array defines how many of each "condition" you want, so in theory it could be changed to:

  CharacterSetArray = Array(_
        Array(5, "abcdefghijklmnopqrstuvwxyz"), _
        Array(2, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), _
        Array(1, "0123456789")  _
    )

see how the 5+2+1 add up to 8? that's where you would change the length of each condition.
@amucinobluedot

Why did you accept an solution that did not include special characters and did include lower case characters in the pwd string?
@amucinobluedot

Did you test the code I submitted?
Avatar of Aleks

ASKER

Because I didn't realize it until after I accepted it :(
Avatar of Aleks

ASKER

I tweaked it myself to include characters and works fine. Just asked if I want to change the amount of characters what to do.
I did test the other code. Which returned nothing.
One more thing. It seems to include letters and characters, it should also include numbers. And the password should be 8 characters long.

there's no mention of special characters in the OP's question, only in the code they found. However the link I provided instructions on adding special characters if the OP desired to. As for lower case, it DOES include lower case characters, not sure where you're getting that from.
Avatar of Aleks

ASKER

How can I make it longer than 8 characters ?
see my comment above...

CharacterSetArray = Array(_
        Array(5, "abcdefghijklmnopqrstuvwxyz"), _
        Array(2, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), _
        Array(1, "0123456789")  _
    )

this equals 8 chars because 5_2_1=8

CharacterSetArray = Array(_
        Array(11, "abcdefghijklmnopqrstuvwxyz"), _
        Array(2, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), _
        Array(1, "0123456789")  _
    )

this equals 14 chars because 11+2+1=14
Avatar of Aleks

ASKER

Perfect ! I am good thanks