Home
Manage Your Code
Snippet: Random Password Generator (C#)
Title: Random Password Generator Language: C#
Description: This method generates random password based on the length specified as input Views: 217
Author: Surjeet Gill Date Added: 11/21/2007
Copy Code  
1public static string GenerateRandomPassword(int pwdLength)
2{
3    byte[] data = new byte[pwdLength];
4
5    new RNGCryptoServiceProvider().GetBytes(data);
6    
7    StringBuilder sb = new StringBuilder();
8
9    foreach (byte oneByte in data)
10    {
11        sb.Append(oneByte.ToString("X2")); // The number is converted to a string of hexadecimal digits

12    }
13    return sb.ToString();
14}
Usage
string pwd = GenerateRandomPassword(4);
Notes
Tested with .NET 2.0