Aleks
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.
The password = <%=strPassword%> shows nothing on the ASP page.
<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>
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.
Let's first define the pwd requirements
It seems to include letters and characters, it should also include numbers. And the password should be 8 characters long.
* eight characters long
* contains upper case alphabetic character(s), numeric digit(s), symbol(s) -- how many of each?
* first pwd character must be alphabetic.
do you see the password = string on the page?
The password = <%=strPassword%> shows nothing on the ASP 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.
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.
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?
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
Once I added the mssg box code I get this:
Microsoft VBScript runtime error '800a0046'
Permission denied: 'msgbox'
/BlueDot/Intranet/Contacts
how about wscript.echo instead of msgbox
ASKER
Microsoft VBScript runtime error '800a01a8'
Object required: 'wscript'
/BlueDot/Intranet/Contacts /webaccess _SPadd.asp , line 122
Object required: 'wscript'
/BlueDot/Intranet/Contacts
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>
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.
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.
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:
and to use it:
<%=RandomString()%>
or
<%
strPassword = RandomString()
Response.Write strPassword
%>
<%
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
%>
and to use it:
<%=RandomString()%>
or
<%
strPassword = RandomString()
Response.Write strPassword
%>
ASKER
I got this error:
Microsoft VBScript compilation error '800a03ea'
Syntax error
/bluedot/Intranet/Contacts /webaccess _SPadd.asp , line 94
Array(1, "0123456789"),
-----------------------^
Microsoft VBScript compilation error '800a03ea'
Syntax error
/bluedot/Intranet/Contacts
Array(1, "0123456789"),
-----------------------^
sorry, get rid of the trailing comment on that line
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")
I did but got this error:
Microsoft VBScript compilation error '800a03ee'
Expected ')'
/bluedot/Intranet/contacts
Array(1, "0123456789")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
PERFECT !!!! If I wanted the password to be longer than 8 characters where can I change the code to make it longer ?
ASKER
Perfect !
ASKER
I changed the array to this:
CharacterSetArray = Array(_
Array(5, "abcdefghijklmnopqrstuvwxy z"), _
Array(1, "ABCDEFGHIJKLMNOPQRSTUVWXY Z"), _
Array(1, "!@#%^&*+"), _
Array(1, "0123456789") _
)
So it includes characters
CharacterSetArray = Array(_
Array(5, "abcdefghijklmnopqrstuvwxy
Array(1, "ABCDEFGHIJKLMNOPQRSTUVWXY
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, "abcdefghijklmnopqrstuvwxy
Array(2, "ABCDEFGHIJKLMNOPQRSTUVWXY
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?
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?
Did you test the code I submitted?
ASKER
Because I didn't realize it until after I accepted it :(
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.
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.
ASKER
How can I make it longer than 8 characters ?
see my comment above...
CharacterSetArray = Array(_
Array(5, "abcdefghijklmnopqrstuvwxy z"), _
Array(2, "ABCDEFGHIJKLMNOPQRSTUVWXY Z"), _
Array(1, "0123456789") _
)
this equals 8 chars because 5_2_1=8
CharacterSetArray = Array(_
Array(11, "abcdefghijklmnopqrstuvwxy z"), _
Array(2, "ABCDEFGHIJKLMNOPQRSTUVWXY Z"), _
Array(1, "0123456789") _
)
this equals 14 chars because 11+2+1=14
CharacterSetArray = Array(_
Array(5, "abcdefghijklmnopqrstuvwxy
Array(2, "ABCDEFGHIJKLMNOPQRSTUVWXY
Array(1, "0123456789") _
)
this equals 8 chars because 5_2_1=8
CharacterSetArray = Array(_
Array(11, "abcdefghijklmnopqrstuvwxy
Array(2, "ABCDEFGHIJKLMNOPQRSTUVWXY
Array(1, "0123456789") _
)
this equals 14 chars because 11+2+1=14
ASKER
Perfect ! I am good thanks
I will look at your code