Solved

script for random password not working

Posted on 2014-02-12
7
443 Views
Last Modified: 2014-02-12
I have the following script to generate a random password, but it is assingning the SAME password every time, not randomizing it.

What is wrong with it ?

---

<script language="vbscript" runat="server">

Sub StrRandomize(strSeed)
        Dim i, nSeed
        nSeed = CLng(0)
        For i = 1 To Len(strSeed)
                nSeed = nSeed Xor ((256 * ((i - 1) Mod 4) * AscB(Mid(strSeed, i, 1))))
        Next
        'Randomiser
        Randomize nSeed
End Sub
'--GeneratePassword(nLength)
'--Generates friendly passwords for remembering and pronounciation
Function GeneratePassword(nLength)
        Dim i, bMadeConsonant, c, nRnd
		'You may adjust the below constants to include local,
		'eg. scandinavian characters. This way your passwords
		'will not be limited to latin characters.
        Const strDoubleConsonants = "bdfglmnpst"
        Const strConsonants = "bcdfghklmnpqrstv"
        Const strVocal = "aeiou"
        GeneratePassword = ""
        bMadeConsonant = False
        For i = 0 To nLength
                'Get a random number number between 0 and 1
                nRnd = Rnd
                'Simple or double consonant, or a new vocal?
                'Does not start with a double consonant
				'15% or less chance for the next letter being a double consonant
                If GeneratePassword <> "" AND (bMadeConsonant <> True) AND (nRnd < 0.15) Then
                        'double consonant
                        c = Mid(strDoubleConsonants, Int(Len(strDoubleConsonants) * Rnd + 1), 1)
						'response.write int(Len(strDoubleConsonants) * Rnd + 1)
						'response.write "<br>"
                        c = c & c
						i = i + 1
                        bMadeConsonant = True
                Else
						'80% or less chance for the next letter being a consonant,
						'depending on wether the last letter was a consonant or not.
                        If (bMadeConsonant <> True) And (nRnd < 0.95) Then
                                'Simple consonant
                                c = Mid(strConsonants, Int(Len(strConsonants) * Rnd + 1), 1)
                                bMadeConsonant = True
						'5% or more chance for the next letter being a vocal. 100% if last
						'letter was a consonant - theoreticaly speaking...
                        Else
                                'If last one was a consonant, make vocal
                                c = Mid(strVocal,Int(Len(strVocal) * Rnd + 1), 1)
                                bMadeConsonant = False
                        End If
                End If
                'Add letter
                GeneratePassword = GeneratePassword & c
        Next
        'Is the password long enough, or perhaps too long?
        If Len(GeneratePassword) > nLength Then
                GeneratePassword = Left(GeneratePassword, nLength)
        End If
End Function

StrRandomize CStr(Now) & CStr(Rnd)

</script>

Open in new window

----

Then the value I assign to it is:  

This is part of the code that assigns the value:

Dim SPInsertPassword__Password
SPInsertPassword__Password = "0"
if(EncrytPswd( GeneratePassword( 10 ) ) <> "") then SPInsertPassword__Password = EncrytPswd( GeneratePassword( 10 ) )

Open in new window

0
Comment
Question by:amucinobluedot
[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
  • 3
  • 2
7 Comments
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 250 total points
ID: 39854497
Put all of your randomizing in a function. See how this test works out....

function testRand()
   randomize
   testRand=Rnd
end function
for x = 1 to 10
   response.write testRand&"<br>"

next
        

Open in new window

0
 
LVL 15

Assisted Solution

by:pateljitu
pateljitu earned 250 total points
ID: 39854543
In your example place Randomize prior to line 62 "StrRandomize CStr(Now) & CStr(Rnd)"

so you code will look like:

<script language="vbscript" runat="server">

Sub StrRandomize(strSeed)
        Dim i, nSeed
        nSeed = CLng(0)
        For i = 1 To Len(strSeed)
                nSeed = nSeed Xor ((256 * ((i - 1) Mod 4) * AscB(Mid(strSeed, i, 1))))
        Next
        'Randomiser
        Randomize nSeed
End Sub
'--GeneratePassword(nLength)
'--Generates friendly passwords for remembering and pronounciation
Function GeneratePassword(nLength)
        Dim i, bMadeConsonant, c, nRnd
		'You may adjust the below constants to include local,
		'eg. scandinavian characters. This way your passwords
		'will not be limited to latin characters.
        Const strDoubleConsonants = "bdfglmnpst"
        Const strConsonants = "bcdfghklmnpqrstv"
        Const strVocal = "aeiou"
        GeneratePassword = ""
        bMadeConsonant = False
        For i = 0 To nLength
                'Get a random number number between 0 and 1
                nRnd = Rnd
                'Simple or double consonant, or a new vocal?
                'Does not start with a double consonant
				'15% or less chance for the next letter being a double consonant
                If GeneratePassword <> "" AND (bMadeConsonant <> True) AND (nRnd < 0.15) Then
                        'double consonant
                        c = Mid(strDoubleConsonants, Int(Len(strDoubleConsonants) * Rnd + 1), 1)
						'response.write int(Len(strDoubleConsonants) * Rnd + 1)
						'response.write "<br>"
                        c = c & c
						i = i + 1
                        bMadeConsonant = True
                Else
						'80% or less chance for the next letter being a consonant,
						'depending on wether the last letter was a consonant or not.
                        If (bMadeConsonant <> True) And (nRnd < 0.95) Then
                                'Simple consonant
                                c = Mid(strConsonants, Int(Len(strConsonants) * Rnd + 1), 1)
                                bMadeConsonant = True
						'5% or more chance for the next letter being a vocal. 100% if last
						'letter was a consonant - theoreticaly speaking...
                        Else
                                'If last one was a consonant, make vocal
                                c = Mid(strVocal,Int(Len(strVocal) * Rnd + 1), 1)
                                bMadeConsonant = False
                        End If
                End If
                'Add letter
                GeneratePassword = GeneratePassword & c
        Next
        'Is the password long enough, or perhaps too long?
        If Len(GeneratePassword) > nLength Then
                GeneratePassword = Left(GeneratePassword, nLength)
        End If
End Function

Randomize
StrRandomize CStr(Now) & CStr(Rnd)

</script>
                                  

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 39854725
I tried the above and still always gives me back the SAME password
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:amucinobluedot
ID: 39854739
It generates the password but it doesn't ranfomize it, its always the same.
0
 

Author Comment

by:amucinobluedot
ID: 39854788
I am trying this now:

----

<%
Dim firstNumber, firstLower, firstUpper, firstOther, latterNumber, _
latterLower, latterUpper, latterOther, passwordLength, extraChars 

firstNumber = "true"
firstLower =  "true"
firstUpper =  "true"
firstOther =  "false"
latterNumber = "true"
latterLower = "true"
latterUpper = "true"
latterOther = "false"
passwordLength = 10
extraChars = "@$!"

Function getRandomNum(lbound, ubound) 
For j = 1 To (250 - ubound)
	Randomize 
	getRandomNum = Int(((ubound - lbound) * Rnd) + 1)
Next 
End Function

Function getRandomChar(number, lower, upper, other, extra) 
numberChars = "0123456789"
lowerChars = "abcdefghijklmnopqrstuvwxyz"
upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:"""'\,<.>/? "
charSet = extra
	if (number = "true") Then charSet = charSet + numberChars
	if (lower = "true") Then charSet = charSet + lowerChars
	if (upper = "true") Then charSet = charSet + upperChars
	if (other = "true") Then charSet = charSet + otherChars
jmi = Len(charSet) 
getRandomChar = Mid(charSet, getRandomNum(1, jmi), 1)
End Function

Function getPassword(length, extraChars, firstNumber, firstLower, _
firstUpper, firstOther, latterNumber, latterLower, latterUpper, latterOther)
rc = ""
	If (length > 0) Then
		rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars)
	End If

	For idx = 1 To length - 1
		rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars)
	Next
getPassword = rc
End Function

strUltraPass = getPassword(passwordLength, extraChars, _
firstNumber, firstLower, firstUpper, firstOther, _
latterNumber, latterLower, latterUpper, latterOther)
%>
<%Session("svPassword") = strUltraPass%>

Open in new window

----

But apparently no value is being entered into the database.



Dim SPInsertPassword__Password
SPInsertPassword__Password = "0"
if(EncrytPswd( Session("svPassword") ) <> "") then SPInsertPassword__Password = EncrytPswd( Session("svPassword") )

Open in new window



What could the issue be ?
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39855119
Just another idea to that may make this a little easier.  You can use the ascii character set http://ascii-code.com/.  In asp you can access this by chr(x) where x is the character number. This little sample uses just the lower case set.  But if you changed the min value to 22 you would get special characters and  upper case.  

<%

function makeWord()
Dim max,min
 max=122
 min=97
 Randomize
 number = (Int((max-min+1)*Rnd+min))
 makeWord =chr(number)
end function
newPass=""
for x =1 to 10
	newPass=newPass&makeWord
next
response.write newPass
%>

Open in new window

0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

691 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