ServersRandom Password Generation

Random Password Generation

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




Watkins

corner.jpg (739 bytes) Random Password Generation corner2.jpg (786 bytes)

See the code in action: Click
here

If you let users sign up for accounts online, most likely you will be automatically giving them temporary passwords after signup. Sometimes these passwords are given out through email or instantly presented on a web page. Either way, you need to be able to provide users with passwords that are dynamic and secure.

If you let users sign up for accounts online, most likely you will be automatically
giving them temporary passwords after signup. Sometimes these passwords are given out
through email or instantly presented on a web page. Either way, you need to be able to
provide users with passwords that are dynamic and secure.

A simple, yet time consuming way would be to populate a database with
pre-configured passwords and then randomly select records from a record set. This is a
functional, but very limited approach. The next approach, which is very scaleable allows
you to specify a length for a randomly generated password.

We will use a function to nicely organize everything and allow easy access
to generate passwords. Next we need to create an array of all the characters,
symbols, and numbers to include in the generated password. I’m going to use all upper case
letters and numbers 2-9. I will be excluding, O, 0, I and 1. These look too much alike and
will confuse users. You can use the following code with or without the function. If your
only going to generate one password, you may want to include the code in the ASP page
without using it as a function, but it will work fine either way.

Dim strPasswd
Dim intPassLength

Function rndPasswd(intPassLength)
     Dim araChars(33) ‘Create the array and add the content below
     araChars(0) = “A”
     araChars(1) = “B”
     araChars(2) = “C”
     araChars(3) = “D”
     araChars(4) = “E”
     araChars(5) = “F”
     araChars(6) = “G”
     araChars(7) = “H”
     araChars(8) = “J”
     araChars(9) = “K”
     araChars(10) = “L”
     araChars(11) = “M”
     araChars(12) = “N”
     araChars(13) = “P”
     araChars(14) = “Q”
     araChars(15) = “R”
     araChars(16) = “S”
     araChars(17) = “T”
     araChars(18) = “U”
     araChars(19) = “V”
     araChars(20) = “W”
     araChars(21) = “X”
     araChars(22) = “Y”
     araChars(23) = “Z”
     araChars(26) = “2”
     araChars(28) = “3”
     araChars(29) = “4”
     araChars(30) = “5”
     araChars(31) = “6”
     araChars(32) = “8”
     araChars(33) = “9”

     strPassword = “”
     Randomize
     Do Until Len(strPassword) = intPassLength
           strPassword = strPassword &
araChars(int(rnd()*33))
     Loop
     rndPasswd = strPassword
End Function

Now you can generate a random password by passing a number to specify the
length of the password to the function
rndPasswd(intPassLength)
. Because rnd() will create a number between 0 and
1, multipling the random number by the number of elements in the array and then converting
it to an integer and selecting that array element, each element can be referenced
randomly. This script will create random passwords that you can use in many different kinds of applications.

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories