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.
LVL 1
AleksAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
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
aikimarkCommented:

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.
AleksAuthor Commented:
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 ?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

aikimarkCommented:
The string doesn't show on the page.
Do you see anything on the page?
aikimarkCommented:
Does the first character need to be a cap letter?
AleksAuthor Commented:
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
aikimarkCommented:
how about wscript.echo instead of msgbox
AleksAuthor Commented:
Microsoft VBScript runtime  error '800a01a8'

Object required: 'wscript'

/BlueDot/Intranet/Contacts/webaccess_SPadd.asp, line 122
aikimarkCommented:
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

aikimarkCommented:
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.
AleksAuthor Commented:
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.
Big MontyWeb Ninja at largeCommented:
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
%>
AleksAuthor Commented:
I got this error:

Microsoft VBScript compilation  error '800a03ea'

Syntax error

/bluedot/Intranet/Contacts/webaccess_SPadd.asp, line 94
Array(1, "0123456789"),
-----------------------^
Big MontyWeb Ninja at largeCommented:
sorry, get rid of the trailing comment on that line
AleksAuthor Commented:
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")
Big MontyWeb Ninja at largeCommented:
yes, comma, was up late last night watching the Patriots game, brain not working today :)

try this code:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AleksAuthor Commented:
PERFECT !!!!   If I wanted the password to be longer than 8 characters where can I change the code to make it longer ?
AleksAuthor Commented:
Perfect !
AleksAuthor Commented:
I changed the array to this:

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

So it includes characters
Big MontyWeb Ninja at largeCommented:
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.
aikimarkCommented:
@amucinobluedot

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

Did you test the code I submitted?
AleksAuthor Commented:
Because I didn't realize it until after I accepted it :(
AleksAuthor Commented:
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.
Big MontyWeb Ninja at largeCommented:
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.
AleksAuthor Commented:
How can I make it longer than 8 characters ?
Big MontyWeb Ninja at largeCommented:
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
AleksAuthor Commented:
Perfect ! I am good thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.