C# 密码生成器代码
我正在做一个C#项目,需要生成随机密码 有人能提供一些代码或高级密码生成方法吗 应可以指定以下内容:C# 密码生成器代码,c#,passwords,generator,C#,Passwords,Generator,我正在做一个C#项目,需要生成随机密码 有人能提供一些代码或高级密码生成方法吗 应可以指定以下内容: 最小密码长度 最大密码长度 有效大写字符 大写字符的最小数目 有效小写字符 最小小写字符数 有效数字字符 最小数字字符数 有效的alfanum字符 最小alfanum字符数 这有用吗?你可以自己在谷歌上找到它。) namespace WorkingCode.CodeProject.PwdGen { using System; using System.Security.Cryp
这有用吗?你可以自己在谷歌上找到它。)
namespace WorkingCode.CodeProject.PwdGen
{
using System;
using System.Security.Cryptography;
using System.Text;
public class PasswordGenerator
{
public PasswordGenerator()
{
this.Minimum = DefaultMinimum;
this.Maximum = DefaultMaximum;
this.ConsecutiveCharacters = false;
this.RepeatCharacters = true;
this.ExcludeSymbols = false;
this.Exclusions = null;
rng = new RNGCryptoServiceProvider();
}
protected int GetCryptographicRandomNumber(int lBound, int uBound)
{
// Assumes lBound >= 0 && lBound < uBound
// returns an int >= lBound and < uBound
uint urndnum;
byte[] rndnum = new Byte[4];
if (lBound == uBound-1)
{
// test for degenerate case where only lBound can be returned
return lBound;
}
uint xcludeRndBase = (uint.MaxValue -
(uint.MaxValue%(uint)(uBound-lBound)));
do
{
rng.GetBytes(rndnum);
urndnum = System.BitConverter.ToUInt32(rndnum,0);
} while (urndnum >= xcludeRndBase);
return (int)(urndnum % (uBound-lBound)) + lBound;
}
protected char GetRandomCharacter()
{
int upperBound = pwdCharArray.GetUpperBound(0);
if ( true == this.ExcludeSymbols )
{
upperBound = PasswordGenerator.UBoundDigit;
}
int randomCharPosition = GetCryptographicRandomNumber(
pwdCharArray.GetLowerBound(0), upperBound);
char randomChar = pwdCharArray[randomCharPosition];
return randomChar;
}
public string Generate()
{
// Pick random length between minimum and maximum
int pwdLength = GetCryptographicRandomNumber(this.Minimum,
this.Maximum);
StringBuilder pwdBuffer = new StringBuilder();
pwdBuffer.Capacity = this.Maximum;
// Generate random characters
char lastCharacter, nextCharacter;
// Initial dummy character flag
lastCharacter = nextCharacter = '\n';
for ( int i = 0; i < pwdLength; i++ )
{
nextCharacter = GetRandomCharacter();
if ( false == this.ConsecutiveCharacters )
{
while ( lastCharacter == nextCharacter )
{
nextCharacter = GetRandomCharacter();
}
}
if ( false == this.RepeatCharacters )
{
string temp = pwdBuffer.ToString();
int duplicateIndex = temp.IndexOf(nextCharacter);
while ( -1 != duplicateIndex )
{
nextCharacter = GetRandomCharacter();
duplicateIndex = temp.IndexOf(nextCharacter);
}
}
if ( ( null != this.Exclusions ) )
{
while ( -1 != this.Exclusions.IndexOf(nextCharacter) )
{
nextCharacter = GetRandomCharacter();
}
}
pwdBuffer.Append(nextCharacter);
lastCharacter = nextCharacter;
}
if ( null != pwdBuffer )
{
return pwdBuffer.ToString();
}
else
{
return String.Empty;
}
}
public string Exclusions
{
get { return this.exclusionSet; }
set { this.exclusionSet = value; }
}
public int Minimum
{
get { return this.minSize; }
set
{
this.minSize = value;
if ( PasswordGenerator.DefaultMinimum > this.minSize )
{
this.minSize = PasswordGenerator.DefaultMinimum;
}
}
}
public int Maximum
{
get { return this.maxSize; }
set
{
this.maxSize = value;
if ( this.minSize >= this.maxSize )
{
this.maxSize = PasswordGenerator.DefaultMaximum;
}
}
}
public bool ExcludeSymbols
{
get { return this.hasSymbols; }
set { this.hasSymbols = value;}
}
public bool RepeatCharacters
{
get { return this.hasRepeating; }
set { this.hasRepeating = value;}
}
public bool ConsecutiveCharacters
{
get { return this.hasConsecutive; }
set { this.hasConsecutive = value;}
}
private const int DefaultMinimum = 6;
private const int DefaultMaximum = 10;
private const int UBoundDigit = 61;
private RNGCryptoServiceProvider rng;
private int minSize;
private int maxSize;
private bool hasRepeating;
private bool hasConsecutive;
private bool hasSymbols;
private string exclusionSet;
private char[] pwdCharArray = "abcdefghijklmnopqrstuvwxyzABCDEFG" +
"HIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_=+[]{}\\|;:'\",<" +
".>/?".ToCharArray();
}
}
namespace WorkingCode.CodeProject.PwdGen
{
使用制度;
使用System.Security.Cryptography;
使用系统文本;
公共类密码生成器
{
公共密码生成器()
{
此。最小值=默认最小值;
this.max=defaultmax;
this.conceutiveCharacters=false;
this.RepeatCharacters=true;
此.ExcludeSymbols=false;
此。排除=空;
rng=新的RNGCryptoServiceProvider();
}
受保护的整数GetCryptographicRandomNumber(整数lBound,整数uBound)
{
//假设lBound>=0&&lBound=lBound和=ExcluderdBase);
返回值(整数)(urndnum%(uBound lBound))+lBound;
}
受保护的字符GetRandomCharacter()
{
int upperBound=pwdCharArray.GetUpperBound(0);
if(true==this.ExcludeSymbols)
{
上限=PasswordGenerator.UBoundDigit;
}
int randomCharPosition=GetCryptographicRandomNumber(
pwdcharray.GetLowerBound(0),上限);
char randomChar=pwdcharray[randomCharPosition];
返回随机字符;
}
公共字符串生成()
{
//在最小值和最大值之间选择随机长度
int pwdLength=GetCryptographicRandomNumber(此值为最小值,
这是(最大值);
StringBuilder pwdBuffer=新StringBuilder();
pwdBuffer.Capacity=此最大值;
//生成随机字符
字符lastCharacter,nextCharacter;
//初始虚拟字符标志
lastCharacter=nextCharacter='\n';
对于(int i=0;ithis.minSize)
{
this.minSize=PasswordGenerator.DefaultMinimum;
}
}
}
公共整数最大值
{
获取{返回this.maxSize;}
设置
{
this.maxSize=值;
如果(this.minSize>=this.maxSize)
{
this.maxSize=PasswordGenerator.defaultmax;
}
}
}
公共布尔排除符号
{
获取{返回this.hasSymbols;}
设置{this.hasSymbols=value;}
}
公共布尔重复字符
{
获取{返回this.hasRepeating;}
设置{this.hasRepeating=value;}
}
公共布尔连续字符
{
获取{返回this.hasContinuous;}
设置{this.hasContinuous=value;}
}
private const int DefaultMinimum=6;
private const int defaultmax=10;
私有const int UBoundDigit=61;
私人RNGCryptoServiceProvider rng;
私有整数
private static string CreateRandomPassword(int passwordLength)
{
string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
char[] chars = new char[passwordLength];
Random rd = new Random();
for (int i = 0; i < passwordLength; i++)
{
chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
}
return new string(chars);
}
public static class PasswordGenerator
{
private const int DefaultMinimum = 6;
private const int DefaultMaximum = 10;
private const int UBoundDigit = 61;
private readonly static char[] PwdCharArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_=+[]{}\\|;:'\",./?".ToCharArray();
private static readonly RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider();
private static int _minSize=DefaultMinimum;
private static int _maxSize=DefaultMaximum;
public static int Minimum
{
get { return _minSize; }
set
{
_minSize = value;
if (DefaultMinimum > _minSize)
{
_minSize = DefaultMinimum;
}
}
}
public static int Maximum
{
get { return _maxSize; }
set
{
_maxSize = value;
if (_minSize >= _maxSize)
{
_maxSize = _minSize + 2;
}
}
}
public static string Exclusions { get; set; }
public static bool ExcludeSymbols { get; set; }
public static bool RepeatCharacters { get; set; }
public static bool ConsecutiveCharacters { get; set; }
static PasswordGenerator()
{
Minimum = DefaultMinimum;
Maximum = DefaultMaximum;
ConsecutiveCharacters = false;
RepeatCharacters = true;
ExcludeSymbols = false;
Exclusions = null;
}
private static int GetCryptographicRandomNumber(int lBound, int uBound)
{
// Assumes lBound >= 0 && lBound < uBound
// returns an int >= lBound and < uBound
uint urndnum;
var rndnum = new Byte[4];
if (lBound == uBound - 1)
{
// test for degenerate case where only lBound can be returned
return lBound;
}
uint xcludeRndBase = (uint.MaxValue -
(uint.MaxValue % (uint)(uBound - lBound)));
do
{
_rng.GetBytes(rndnum);
urndnum = BitConverter.ToUInt32(rndnum, 0);
} while (urndnum >= xcludeRndBase);
return (int)(urndnum % (uBound - lBound)) + lBound;
}
private static char GetRandomCharacter()
{
var upperBound = PwdCharArray.GetUpperBound(0);
if (ExcludeSymbols)
{
upperBound = UBoundDigit;
}
int randomCharPosition = GetCryptographicRandomNumber(
PwdCharArray.GetLowerBound(0), upperBound);
char randomChar = PwdCharArray[randomCharPosition];
return randomChar;
}
public static string Generate()
{
// Pick random length between minimum and maximum
var pwdLength = GetCryptographicRandomNumber(Minimum,Maximum);
var pwdBuffer = new StringBuilder {Capacity = Maximum};
// Initial dummy character flag
char lastCharacter = '\n';
for (var i = 0; i < pwdLength; i++)
{
var nextCharacter = GetRandomCharacter();
while (nextCharacter == lastCharacter)
{
nextCharacter = GetRandomCharacter();
}
if (false == RepeatCharacters)
{
var temp = pwdBuffer.ToString();
var duplicateIndex = temp.IndexOf(nextCharacter);
while (-1 != duplicateIndex)
{
nextCharacter = GetRandomCharacter();
duplicateIndex = temp.IndexOf(nextCharacter);
}
}
if ((null != Exclusions))
{
while (-1 != Exclusions.IndexOf(nextCharacter))
{
nextCharacter = GetRandomCharacter();
}
}
pwdBuffer.Append(nextCharacter);
lastCharacter = nextCharacter;
}
return pwdBuffer.ToString();
}
}