-----RandomPasswordCrate.cs
public class RandomPasswordCrate
{
private int RandomInteger(int min, int max)
{
RNGCryptoServiceProvider Rand = new RNGCryptoServiceProvider();
uint scale = uint.MaxValue;
while (scale == uint.MaxValue)
{
// Get four random bytes.
byte[] four_bytes = new byte[4];
Rand.GetBytes(four_bytes);
// Convert that into an uint.
scale = BitConverter.ToUInt32(four_bytes, 0);
}
// Add min to the scaled difference between max and min.
return (int)(min + (max - min) *
(scale / (double)uint.MaxValue));
}
private string RandomChar(string str)
{
return str.Substring(RandomInteger(0, str.Length - 1), 1);
}
private string RandomizeString(string str)
{
string result = "";
while (str.Length > 0)
{
// Pick a random character.
int i = RandomInteger(0, str.Length - 1);
result += str.Substring(i, 1);
str = str.Remove(i, 1);
}
return result;
}
private string RandomPassword(int min_chars, int max_chars)
{
const string LOWER = "abcdefghijklmnopqrstuvwxyz";
const string UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string NUMBER = "0123456789";
//const string SPECIAL = @"~!@#$%^&*():;[]{}<>,.?/\|";
const string SPECIAL = @"~!@#%&_<>";
// Make a list of allowed characters.
string allowed = "";
allowed += LOWER;
allowed += UPPER;
allowed += NUMBER;
allowed += SPECIAL;
// Pick the number of characters.
int num_chars = RandomInteger(min_chars, max_chars);
// Satisfy requirements.
string password = "";
password += RandomChar(LOWER);
password += RandomChar(UPPER);
password += RandomChar(NUMBER);
password += RandomChar(SPECIAL);
// Add the remaining characters randomly.
while (password.Length < num_chars)
password += allowed.Substring(
RandomInteger(0, allowed.Length - 1), 1);
// Randomize (to mix up the required characters at the front).
password = RandomizeString(password);
return password;
}
}
------In cs Page
protected void Page_Load(object sender, EventArgs e)
{
string password = "Your password is: " + RandomPassword(9, 20);
}
public class RandomPasswordCrate
{
private int RandomInteger(int min, int max)
{
RNGCryptoServiceProvider Rand = new RNGCryptoServiceProvider();
uint scale = uint.MaxValue;
while (scale == uint.MaxValue)
{
// Get four random bytes.
byte[] four_bytes = new byte[4];
Rand.GetBytes(four_bytes);
// Convert that into an uint.
scale = BitConverter.ToUInt32(four_bytes, 0);
}
// Add min to the scaled difference between max and min.
return (int)(min + (max - min) *
(scale / (double)uint.MaxValue));
}
private string RandomChar(string str)
{
return str.Substring(RandomInteger(0, str.Length - 1), 1);
}
private string RandomizeString(string str)
{
string result = "";
while (str.Length > 0)
{
// Pick a random character.
int i = RandomInteger(0, str.Length - 1);
result += str.Substring(i, 1);
str = str.Remove(i, 1);
}
return result;
}
private string RandomPassword(int min_chars, int max_chars)
{
const string LOWER = "abcdefghijklmnopqrstuvwxyz";
const string UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string NUMBER = "0123456789";
//const string SPECIAL = @"~!@#$%^&*():;[]{}<>,.?/\|";
const string SPECIAL = @"~!@#%&_<>";
// Make a list of allowed characters.
string allowed = "";
allowed += LOWER;
allowed += UPPER;
allowed += NUMBER;
allowed += SPECIAL;
// Pick the number of characters.
int num_chars = RandomInteger(min_chars, max_chars);
// Satisfy requirements.
string password = "";
password += RandomChar(LOWER);
password += RandomChar(UPPER);
password += RandomChar(NUMBER);
password += RandomChar(SPECIAL);
// Add the remaining characters randomly.
while (password.Length < num_chars)
password += allowed.Substring(
RandomInteger(0, allowed.Length - 1), 1);
// Randomize (to mix up the required characters at the front).
password = RandomizeString(password);
return password;
}
}
------In cs Page
protected void Page_Load(object sender, EventArgs e)
{
string password = "Your password is: " + RandomPassword(9, 20);
}
No comments:
Post a Comment