Have you ever wanted to create a random key to store in a session or pass along through the querystring? Well, here is a simple solution to generate a random key using letters and numbers that can be any length you wish:
Code:
Function generateKey(keyLength)
' Initialize variables
sDefaultChars = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789"
iKeyLength = keyLength
iDefaultCharactersLength = Len(sDefaultChars)
' Initialize the random number generator
Randomize
'Loop for the number of characters password is to have
For iCounter = 1 To iKeyLength
iPickedChar = Int((iDefaultCharactersLength * Rnd) + 1)
sMyKey = sMyKey & Mid(sDefaultChars, iPickedChar, 1)
Next
generateKey = sMyKey
End Function
To generate the key (with length of 30 characters), simply use the following:
Code:
strKey = generateKey(30)
You can also modify the
sDefaultChars string to allow other characters as well.
Happy programming.