在.NET中产生随机密码字符串

   using System;<br />using System.Security.Cryptography;<br />using System.Text;<br />namespace Utility {<br /> public class PasswordGenerator {<br /> public PasswordGenerator() {<br /> this.Minimum = DefaultMinimum;<br /> this.Maximum = DefaultMaximum;<br /> this.ConsecutiveCharacters = false;<br /> this.RepeatCharacters = true;<br /> this.ExcludeSymbols = false;<br /> this.Exclusions = null;<br /> rng = new RNGCryptoServiceProvider();<br /> } <br /> protected int GetCryptographicRandomNumber(int lBound, int uBound) { <br /> // 假定 lBound >= 0 && lBound < uBound<br /> // 返回一个 int >= lBound and < uBound<br /> uint urndnum; <br /> byte[] rndnum = new Byte[4]; <br /> if (lBound == uBound-1) {<br /> // 只有iBound返回的情况 <br /> return lBound;<br /> }<br /> uint xcludeRndBase = (uint.MaxValue - (uint.MaxValue%(uint)(uBound-lBound))); <br /> do {<br /> rng.GetBytes(rndnum); <br /> urndnum = System.BitConverter.ToUInt32(rndnum,0);<br /> } while (urndnum >= xcludeRndBase); <br /> return (int)(urndnum % (uBound-lBound)) + lBound;<br /> }<br /> protected char GetRandomCharacter() {<br /> int upperBound = pwdCharArray.GetUpperBound(0);<br /> if ( true == this.ExcludeSymbols ) {<br /> upperBound = PasswordGenerator.UBoundDigit;<br /> }<br /> int randomCharPosition = GetCryptographicRandomNumber(pwdCharArray.GetLowerBound(0), upperBound);<br /> char randomChar = pwdCharArray[randomCharPosition];<br /> return randomChar;<br /> } <br /> public string Generate() {<br /> // 得到minimum 和 maximum 之间随机的长度<br /> int pwdLength = GetCryptographicRandomNumber(this.Minimum, this.Maximum);<br /> StringBuilder pwdBuffer = new StringBuilder();<br /> pwdBuffer.Capacity = this.Maximum;<br /> // 产生随机字符<br /> char lastCharacter, nextCharacter;<br /> // 初始化标记<br /> lastCharacter = nextCharacter = '/n'; <br /> for ( int i = 0; i < pwdLength; i++ ) {<br /> nextCharacter = GetRandomCharacter();<br /> if ( false == this.ConsecutiveCharacters ) {<br /> while ( lastCharacter == nextCharacter ) {<br /> nextCharacter = GetRandomCharacter();<br /> } 
 
   <br />} if ( false == this.RepeatCharacters ) { string temp =<br />pwdBuffer.ToString(); int duplicateIndex = temp.IndexOf(nextCharacter);<br />while ( -1 != duplicateIndex ) { nextCharacter = GetRandomCharacter();<br />duplicateIndex = temp.IndexOf(nextCharacter); } } if ( ( null !=<br />this.Exclusions ) ) { while ( -1 !=<br />this.Exclusions.IndexOf(nextCharacter) ) { nextCharacter =<br />GetRandomCharacter(); } } pwdBuffer.Append(nextCharacter);<br />lastCharacter = nextCharacter; } if ( null != pwdBuffer ) { return<br />pwdBuffer.ToString(); } else { return String.Empty; } } public bool<br />ConsecutiveCharacters { get { return this.hasConsecutive; } set {<br />this.hasConsecutive = value;} } public bool ExcludeSymbols { get {<br />return this.hasSymbols; } set { this.hasSymbols = value;} } public<br />string Exclusions { get { return this.exclusionSet; } set {<br />this.exclusionSet = value; } } public int Maximum { get { return<br />this.maxSize; } set { this.maxSize = value; if ( this.minSize >=<br />this.maxSize ) { this.maxSize = PasswordGenerator.DefaultMaximum; } } }<br />public int Minimum { get { return this.minSize; } set { this.minSize =<br />value; if ( PasswordGenerator.DefaultMinimum > this.minSize ) {<br />this.minSize = PasswordGenerator.DefaultMinimum; } } } public bool<br />RepeatCharacters { get { return this.hasRepeating; } set {<br />this.hasRepeating = value;} } private const int DefaultMaximum = 10;<br />private const int DefaultMinimum = 6; private const int UBoundDigit =<br />61; private string exclusionSet; private bool hasConsecutive; private<br />bool hasRepeating; private bool hasSymbols; private int maxSize;<br />private int minSize; private char[] pwdCharArray =<br />"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$^*()-_=+[]{}//|;:'/",./".ToCharArray();<br />private RNGCryptoServiceProvider rng; }<br />} 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值