Solved

script for random password not working

Posted on 2014-02-12
7
431 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
  • 3
  • 2
7 Comments
 
LVL 52

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 52

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. 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 : Go t…

758 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

14 Experts available now in Live!

Get 1:1 Help Now