Solved

random password

Posted on 2008-10-17
14
707 Views
Last Modified: 2012-05-05
hi,
i am using this code to generate a random number:
Session["Id"] = new Random().Next(1, 1000);
how can i also include uppercase and or lowercase characters to make it like a random pasword. but it must be unique.

or is there another way to generate a random but unique password and indicate how long it can be in asp.net and classic asp?

0
Comment
Question by:eaweb
  • 3
  • 2
  • 2
  • +5
14 Comments
 
LVL 5

Expert Comment

by:Goofytouy
Comment Utility

Hi eaweb

Can't post the code here (i should think it), but you can use:

chr(Random().Next(65,90)) to obtain uppercase random letters
chr(Random().Next(97,122)) to obtain lowercase random letters.
Also, if you use range 65 to 122 you can include some symbols ([\]^_`)
By lowering 65 to 58, you may obtain too: :;<=>?@



0
 
LVL 5

Expert Comment

by:Goofytouy
Comment Utility

Oopss.. it leave me before I've finished.

You should stablish which it's going to be the length of the random password, and iterate through this functions.
You also may do a Select Case from a random 1 to 3, and according to that random, call one of the three options (numbers, lower or uppercase)


Sorry for only giving you the ideas, but I do not have reachable my working framework.
Regards
Goofy
0
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
Comment Utility
0
 
LVL 9

Expert Comment

by:deathtospam
Comment Utility
What itkamaraj said -- generating a GUID (globally unique ID) is a pretty good way to go.

http://articles.techrepublic.com.com/5100-10878_11-5708732.html
http://aspnet.4guysfromrolla.com/articles/101205-1.aspx
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
you can build your own function:

public string CreateRandomPassword(int size)
{
      Random rand = new Random();

      string pass = "";
      for (int i=0; i<size;)
      {
             char val = (char)rand.Next(48,127);
             if (char.IsLetterOrDigit(val))
             {
                  pass += val; i++;
             }
      }
      return pass;
}

So you can use as:

Session["Id"] = CreateRandomPassword(10);
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
The author stated:

    "...a random but unique password..."

As the posts have shown thus far, there are MANY ways to generate a random string of characters that can be used as a password.

What has not been addressed is the "UNIQUE" aspect in the requirements.

Unique with respect to what?  Do you have a set of all previously generated passwords that you need to check against to prevent duplicates?

Give us more details...
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:eaweb
Comment Utility
oh sorry,
i need the generation to be based on globally unique ID.

i need a way to do it in asp.net vb and classic asp vb
0
 
LVL 13

Expert Comment

by:TechTiger007
Comment Utility
use Guid.NewGuid()
this will give a globally unique id every time
this class is a part of dotnet framework
0
 

Author Comment

by:eaweb
Comment Utility
how can i convert the following .net code into classic asp code?

Public Function GetRandomPasswordUsingGUID(ByVal length as Integer) as String  
  'Get the GUID
  Dim guidResult as String = System.Guid.NewGuid().ToString()
 
  'Remove the hyphens
  guidResult = guidResult.Replace("-", String.Empty)
 
  'Make sure length is valid
  If length <= 0 OrElse length > guidResult.Length Then
    Throw New ArgumentException("Length must be between 1 and " & guidResult.Length)
  End If
 
  'Return the first length bytes
  Return guidResult.Substring(0, length)
End Function
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
In order to ensure uniqueness, you will have to use all 32 characters of the GUID.  If your password is less than the GUID length, you will need to combine the GUID bytes in a way that ensures uniqueness.

Otherwise, you will need to retain/persist the passwords you've generated and do a look-up to ensure uniqueness before issuing a new password.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Right...there isn't any guarantee at all that GUIDs are actually unique.  They don't have any notion of what has been generated before...

The "uniqueness" is simply based on the fact that they are so long that there are literally millions of combinations that can be produced and it is unlikely to generate the same sequence in any short period.

Directly from the documentation:
http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx

    "There is a very low probability that the value of the new Guid is all zeroes or equal to any other Guid."

By shortening the length of the value you are decreasing the number of combinations to pick from and increasing the likelihood of a repeat.  The actual probability depends on how often you generate values, how long you need a generated value to remain "unique", and whether you are disposing of the values afterwards or need to persist them so you know they have been used before.
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
@eaweb

1. What is the big picture for this question?  Why would passwords need to be guaranteed/absolutely unique versus probably unique?

2. How long are these passwords?

3. What kind of character set will comprise these passwords?

4. Are these passwords for use by humans or machines?  (memorability might be important)
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
PAQ with no refund
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now