Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 软件时空 > 软件相关 > 在.NET中产生随机密码字符串
【标  题】:在.NET中产生随机密码字符串
【关键字】:.NET
【来  源】:http://www.cublog.cn/u/884/showart.php?id=113438

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

Your Ad Here
using System; using System.Security.Cryptography; using System.Text; namespace Utility { 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) { // 假定 lBound >= 0 && lBound < uBound // 返回一个 int >= lBound and < uBound uint urndnum; byte[] rndnum = new Byte[4]; if (lBound == uBound-1) { // 只有iBound返回的情况 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() { // 得到minimum 和 maximum 之间随机的长度 int pwdLength = GetCryptographicRandomNumber(this.Minimum, this.Maximum); StringBuilder pwdBuffer = new StringBuilder(); pwdBuffer.Capacity = this.Maximum; // 产生随机字符 char lastCharacter, nextCharacter; // 初始化标记 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 bool ConsecutiveCharacters { get { return this.hasConsecutive; } set { this.hasConsecutive = value;} } public bool ExcludeSymbols { get { return this.hasSymbols; } set { this.hasSymbols = value;} } public string Exclusions { get { return this.exclusionSet; } set { this.exclusionSet = value; } } public int Maximum { get { return this.maxSize; } set { this.maxSize = value; if ( this.minSize >= this.maxSize ) { this.maxSize = PasswordGenerator.DefaultMaximum; } } } public int Minimum { get { return this.minSize; } set { this.minSize = value; if ( PasswordGenerator.DefaultMinimum > this.minSize ) { this.minSize = PasswordGenerator.DefaultMinimum; } } } public bool RepeatCharacters { get { return this.hasRepeating; } set { this.hasRepeating = value;} } private const int DefaultMaximum = 10; private const int DefaultMinimum = 6; private const int UBoundDigit = 61; private string exclusionSet; private bool hasConsecutive; private bool hasRepeating; private bool hasSymbols; private int maxSize; private int minSize; private char[] pwdCharArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567 89`~!@#$^*()-_=+[]{}\\|;:'\",./".ToCharArray(); private RNGCryptoServiceProvider rng; } }
C#邮件发送组件源代码:【上一篇】
《代码大全(第2版)》勘误表,2006 年 5 月 16 日更新,共 6 条:【下一篇】
【相关文章】
  • Asp.net(c#)实现多线程断点续传
  • ASP.NET画图全攻略
  • 在.Net中操作IIS
  • 用反射调用任意.Net库中的方法
  • 在ASP.NET中处理 datetime 的一些通用函数
  • 通过改善架构来提高 ASP.Net 应用程序的性能
  • ASP.NET 2.0中ReadOnly的TextBox
  • ADO.NET 3.0
  • ASP.NET 2.0 中的新增服务、控件与功能概述
  • 让ASP.NET简便使用SCRIPT.
  • 【随机文章】
  • QQ宠物领养成长完全手则(1)
  • Linux中的SWAP分区
  • Photoshop热键完全版(上)
  • 初始化环境:/etc/skel
  • 隐私报告的问题
  • 无可挑剔的星座女孩
  • baidu分词算法分析之一
  • Dev-C++中察看运行程序结果
  • IE的输出缓冲
  • Sun的Cookies和其他跟踪技术
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.