nmretd
asked on
ASP.NET Random Password Generator
Hi
I need an ASP.NET random password generator which gives me a password atleast 6 characters in length mixed with upper/lower case letter and atleast one digit. Can somebody provide me with a function that can do this ?
Thanks.
I need an ASP.NET random password generator which gives me a password atleast 6 characters in length mixed with upper/lower case letter and atleast one digit. Can somebody provide me with a function that can do this ?
Thanks.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I know your looking for VB.NET solution but this VB6 solution could be adapted to VB.NET
Function RandomPW(myLength) As String
'These constant are the minimum and maximum length for random
'length passwords. Adjust these values to your needs.
Const minLength = 6
Const maxLength = 6
Dim X, Y, strPW, Temp
If myLength = 0 Then
Randomize
myLength = Int((maxLength * Rnd) + minLength)
End If
For X = 1 To myLength
'Randomize the type of this character
Y = Int((3 * Rnd) + 1) '(1) Numeric, (2) Uppercase, (3) Lowercase
Select Case Y
Case 1
'Numeric character
Randomize
Temp = Chr(Int((9 * Rnd) + 48))
Do Until (Temp <> "1" And Temp <> "0")
Randomize
Temp = Chr(Int((9 * Rnd) + 48))
Loop
strPW = strPW & Temp
Case 2
'Uppercase character
Randomize
Temp = Chr(Int((25 * Rnd) + 65))
Do Until (Temp <> "L" And Temp <> "I" And Temp <> "O")
Randomize
Temp = Chr(Int((25 * Rnd) + 65))
Loop
strPW = strPW & Temp
Case 3
'Lowercase character
Randomize
Temp = Chr(Int((25 * Rnd) + 97))
Do Until (Temp <> "l" And Temp <> "i" And Temp <> "o")
Randomize
Temp = Chr(Int((25 * Rnd) + 97))
Loop
strPW = strPW & Temp
End Select
Next
RandomPW = strPW
End Function
Function RandomPW(myLength) As String
'These constant are the minimum and maximum length for random
'length passwords. Adjust these values to your needs.
Const minLength = 6
Const maxLength = 6
Dim X, Y, strPW, Temp
If myLength = 0 Then
Randomize
myLength = Int((maxLength * Rnd) + minLength)
End If
For X = 1 To myLength
'Randomize the type of this character
Y = Int((3 * Rnd) + 1) '(1) Numeric, (2) Uppercase, (3) Lowercase
Select Case Y
Case 1
'Numeric character
Randomize
Temp = Chr(Int((9 * Rnd) + 48))
Do Until (Temp <> "1" And Temp <> "0")
Randomize
Temp = Chr(Int((9 * Rnd) + 48))
Loop
strPW = strPW & Temp
Case 2
'Uppercase character
Randomize
Temp = Chr(Int((25 * Rnd) + 65))
Do Until (Temp <> "L" And Temp <> "I" And Temp <> "O")
Randomize
Temp = Chr(Int((25 * Rnd) + 65))
Loop
strPW = strPW & Temp
Case 3
'Lowercase character
Randomize
Temp = Chr(Int((25 * Rnd) + 97))
Do Until (Temp <> "l" And Temp <> "i" And Temp <> "o")
Randomize
Temp = Chr(Int((25 * Rnd) + 97))
Loop
strPW = strPW & Temp
End Select
Next
RandomPW = strPW
End Function
Nice thing about this one is that it omits characters such as L, I and O which can be mistaken for other characters unless the user copies and pastes the password.
why don't you use MD5 functions for creating random strings?
abbdan: Sorry, but I'm really interested to know how you guarantee that there would be at least one digit in the generated password???
I'm also interested to know the advantages of converting some VB 6 code to VB .NET instead of directly using the VB .NET code!
:-)
_______________
Nayer Naguib
I'm also interested to know the advantages of converting some VB 6 code to VB .NET instead of directly using the VB .NET code!
:-)
_______________
Nayer Naguib
Modifications to the password generation process will probably be made tailored to the authors specifications even if they use your code. My code, albeit VB6, gives ideas for guidance as an assist solution. Your code provided needed translations of the random function and could be adapted for the total solution.
I'm just offering related ideas. They don't have to be accepted.
Its good code and offers other ideas that may not have been encompased.
I'm just offering related ideas. They don't have to be accepted.
Its good code and offers other ideas that may not have been encompased.
I know. You just missed the specification of "at least one digit" requested by the author.
Also, it is a good practice to avoid the following structure as much as possible:
Do
Generate a random number
Until the random number meets some condition
Rather, it may be better (for performance reasons), that you first create a collection of *only* the allowed numbers, and then randomly select one. For example, the following two algorithms are supposed to fill an array with values 1 to 1000 randomly shuffled:
Algorithm 1:
_________________
Do
Do
Generate a random number x between 1 and 1000
Until we find x that has not been generated before
Fill the next available element in the array with the value x
Until all 1000 elements are populated
_________________
Algorithm 2:
_________________
Create a linked list with sorted values from 1 to 1000
Do
Generate a random number x with maximum value equal to current list length
Store element x of the list in the next available element of the array
Remove element x from the list
While list is not empty
_________________
Although both algorithms are correct (and you probably won't even notice a difference in performance on a 3 GHz processor!), the first algorithm may require tens of thousands of random number generation operations before generating all values from 1 to 1000. However, the second algorithm, although requires little overhead to populate the linked list, will require generating exactly 1000 random numbers before the algorithm successfully finished execution.
In your code, you have three such Do-Until loops. Although in this specific case allowed numbers will be found in no time, it's generally better to follow the other technique. For example, if you plan to omit 'L', 'I', and 'O', then you can generate a random number between 1 and 23, and then map the generated number to an allowed letter. On the other hand, you successfully used the second technique when you generated a random number between 0 and 25 to select a random letter (where you later add 65 or 97 to match ASCII code for the alphabet). Imagine what the code would have looked like if you wrote a loop that generates random numbers between 0 and 255 until a number in the ranges [65, 90] or [97, 122] is found!!
_______________
Nayer Naguib
Also, it is a good practice to avoid the following structure as much as possible:
Do
Generate a random number
Until the random number meets some condition
Rather, it may be better (for performance reasons), that you first create a collection of *only* the allowed numbers, and then randomly select one. For example, the following two algorithms are supposed to fill an array with values 1 to 1000 randomly shuffled:
Algorithm 1:
_________________
Do
Do
Generate a random number x between 1 and 1000
Until we find x that has not been generated before
Fill the next available element in the array with the value x
Until all 1000 elements are populated
_________________
Algorithm 2:
_________________
Create a linked list with sorted values from 1 to 1000
Do
Generate a random number x with maximum value equal to current list length
Store element x of the list in the next available element of the array
Remove element x from the list
While list is not empty
_________________
Although both algorithms are correct (and you probably won't even notice a difference in performance on a 3 GHz processor!), the first algorithm may require tens of thousands of random number generation operations before generating all values from 1 to 1000. However, the second algorithm, although requires little overhead to populate the linked list, will require generating exactly 1000 random numbers before the algorithm successfully finished execution.
In your code, you have three such Do-Until loops. Although in this specific case allowed numbers will be found in no time, it's generally better to follow the other technique. For example, if you plan to omit 'L', 'I', and 'O', then you can generate a random number between 1 and 23, and then map the generated number to an allowed letter. On the other hand, you successfully used the second technique when you generated a random number between 0 and 25 to select a random letter (where you later add 65 or 97 to match ASCII code for the alphabet). Imagine what the code would have looked like if you wrote a loop that generates random numbers between 0 and 255 until a number in the ranges [65, 90] or [97, 122] is found!!
_______________
Nayer Naguib
In both loops, j is used to hold an encoded value of the randomly generated character to be added. The value is then decoded in the first loop to one of the 52 possible letters (26 uppercase and 26 lowercase), and in the second loop, to a digit between 0 and 9.
Chr(k) returns the character corresponding to ASCII value k.
_______________
Nayer Naguib