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

来源:互联网 发布:大数据预处理 编辑:程序博客网 时间:2024/05/21 09:53
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 &amp;gt;= 0 &amp;amp;&amp;amp; lBound &amp;lt; uBound<br /> // 返回一个 int &amp;gt;= lBound and &amp;lt; 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&amp;amp;#37;(uint)(uBound-lBound))); <br /> do {<br /> rng.GetBytes(rndnum); <br /> urndnum = System.BitConverter.ToUInt32(rndnum,0);<br /> } while (urndnum &amp;gt;= xcludeRndBase); <br /> return (int)(urndnum &amp;amp;#37; (uBound-lBound)) &amp;amp;#43; 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 &amp;lt; pwdLength; i&amp;amp;#43;&amp;amp;#43; ) {<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 &amp;gt;=<br />this.maxSize ) { this.maxSize = PasswordGenerator.DefaultMaximum; } } }<br />public int Minimum { get { return this.minSize; } set { this.minSize =<br />value; if ( PasswordGenerator.DefaultMinimum &amp;gt; 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 />&quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$^*()-_=&amp;amp;#43;[]{}//|;:'/&quot;,./&quot;.ToCharArray();<br />private RNGCryptoServiceProvider rng; }<br />}
原创粉丝点击