Problem with random password

I am using the code below to create a 10 digit random string.
After the string is generated it is encrypted and saved in the database, the encryption doesn't modify the string content other than encrypting it, then I am testing and displaying the string decrypted on the page and I find that most times the string is LESS than 10 characters long.

Is there something off with the script below or maybe one of the characters being used may be causing the string to change size ?
I don't have access to the encryption code at this time so perhaps we can figure it out with the script below:

IT SHOULD ALWAYS BE 10 digits long.

function RandomString()


    dim CharacterSetArray
   CharacterSetArray = Array(_
         Array(4, "abcdefghijklmnopqrstuvwxyz"), _
         Array(2, "!@#%&*+"), _
         Array(2, "0123456789")  _

    dim i
    dim j
    dim Count
    dim Chars
    dim Index
    dim Temp

    for i = 0 to UBound(CharacterSetArray)

        Count = CharacterSetArray(i)(0)
        Chars = CharacterSetArray(i)(1)

        for j = 1 to Count

            Index = Int(Rnd() * Len(Chars)) + 1
            Temp = Temp & Mid(Chars, Index, 1)



    dim TempCopy

    do until Len(Temp) = 0

        Index = Int(Rnd() * Len(Temp)) + 1
        TempCopy = TempCopy & Mid(Temp, Index, 1)
        Temp = Mid(Temp, 1, Index - 1) & Mid(Temp, Index + 1)


    RandomString = TempCopy

end function

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Since this is a security-related issue, you should know about weaknesses with the VB random number functions.

An Examination of Visual Basic's Random Number Generation:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AleksAuthor Commented:
Yes thank you, but for now the question is different.
I don't see anything wrong with your code.  It should always produce a character string of length 10 that contains characters from the groups in the amounts specified by the count values in each group.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

AleksAuthor Commented:
So maybe the encrypt function breaks somewhere with one of those special characters. Any of them that you think could be an issue ?
Dave BaldwinFixer of ProblemsCommented:
Can you show us the simple example of how you are using it and some input strings that give a result you aren't expecting?
All the characters could cause problems, depending on the code that's processing the string.
AleksAuthor Commented:
I am changing the encryption altogether. If I have issues with the new one Ill repost then.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.