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

本文介绍了一个使用C#实现的安全密码生成器。该生成器利用了RNGCryptoServiceProvider来确保密码的随机性和安全性,并提供了多种配置选项,如设置密码长度范围、避免连续字符和重复字符等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 />}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值