Monday, December 27, 2010

Different Ways of Generating Random Passwords in Asp.net (We Can Use Same for Creating Captchas)

1.Using the NewGuid function
2.Generate the random password in our own way.


Introduction

How to generate random passwords, I came across this question many times in forums. Recently again I came across this question, so I decided to write this article. This may help the required ones. Actually it is a simple task; usually there are two best or common methods to generate the random passwords as follows:
  • Using the NewGuid function
  • Generate the random password in our own way

Using the NewGuid function

NewGuid method is a member of the System.Guid structure, this generates a 32 bit hexadecimal string and it will globally unique. If you are familiar with the components then, you can refer this to the CLSIDs.
  1.  
  2. ‘ VB.NET
  3. Dim Result as String = System.Guid.NewGuid().ToString()
  4.  
  5. // C#
  6. string Result = System.Guid.NewGuid().ToString();
  7.  
The result will be as follows:
  1.  
  2. 63dac3e4- 63b9-4057-862b-7d28e2868389
  3.  
Now, you can say like, how to use this crazy thing as the password. Let me explain it, every time when the new GUID is generated it will be unique. So, our passwords will be unique for all the users, so I think this is some what good option, right!! But, the problem is, the string is lengthy.
We will solve this problem by taking the required amount of characters from the generated GUID as follows:
  1.  
  2. public string GenerateRandomPasswordUsingGUID(int length)
  3. {
  4.   // Get the GUID
  5.   string Result = System.Guid.NewGuid().ToString();
  6.  
  7.   // Remove the hyphens
  8.   Result = Result.Replace("-"string.Empty);
  9.  
  10.   // Make sure length is valid
  11.   if (length <= 0 || length > Result.Length)
  12.     throw new ArgumentException("Length must be between 1 and "+ guidResult.Length);
  13.  
  14.   // Return the first length bytes
  15.   return guidResult.Substring(0, length);
  16. }
  17.  
Now, if the length is 8 , the return password from this function will be as follows:
  1.  
  2. 63dac3e4
  3.  
It is a good and healthy password, cheers.

Generate the random password in our own way

Generating passwords with GUID is simple and effective but, some times their will be other things to consider like, if our password needs only alphabets, then GUID will not be the answer. Like this kind of custom rules, we need our own function to generate passwords. There are many ways to write this function, I will show an example how to write this:
Condition like, includes like no numbers, but only, combination of capital and small letters as follows:
  1.  
  2. string alphabets = "abcdefghijklmnopqrstuvwxyz";
  3.  
  4. StringBuilder password = new StringBuilder();
  5.  
  6. Random r = new Random();
  7.  
  8. for(int j=0; j<=20;j++)
  9. {
  10. for(int i=0;i<=5;i++)
  11. {
  12. password.Append(alphabets[r.Next(alphabets.Length)]);
  13. string temp = alphabets[r.Next(alphabets.Length)];
  14. password.Append(temp.ToUpper);
  15.  
  16. }
  17. Response.Write(password.ToString());
  18. password.Remove(0,password.Length);
  19.  
  20. }
  21.  
We are using the string builder to reduce the memory usage and append the password string. In the for loop, we are taking a random alphabet from the string and appending to the password and then, we are taking the random alphabet from the string and converting it to upper case and appending to string.

No comments:

Post a Comment