Solved

script for random password not working

Posted on 2014-02-12
7
438 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

863 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

22 Experts available now in Live!

Get 1:1 Help Now