The case of GeneratePassword() and special characters

(You can find a German language version of this post here )

Sometimes you might need to quickly generate a complex passwords in a script or an application. Of course, there are tons of functions available on the Web, but isn’t there something out of the box? Turns out, there is: [System.Web.Security.Membership]::GeneratePassword() to the rescue!

At first, the result of

looks promising. But if you call the method often, you’ll start noticing that there are sometimes more non-alphanumeric characters in the generated string than have been specified in the second parameter. WTF? Let’s investigate.

According to the documentation linked above, the password length (first argument) may vary from 1 to 128. The second argument, which is the required count of non-alpahnumeric characters, can take any integer value from 0 up to the password length requested. So we can iterate through all possible combination and see how the non-alphanumeric character count behaves in each.

For this, let’s generate 1000 passwords for every combination and count the cases where the actual non-alphanumeric character count differs from the requested. In the end, we get a metric that can vary from 0 to 1, closer to zero is better. Here’s the script (not optimized for performance in any way):

The results of a sample run can be downloaded here: pwd-nonalpha . In short, the more non-alphanumeric chars you request (relative to the total length), the better your chance to actually get that number delivered. Colourised for visualisation purposes, my sample run looks like this:

So not very encouraging if you’re only required to include one or two special chars in your random passwords – you’ll most certainly end up with more than you request!

Happy password generating!