首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>NET专区>C#语言>在.NET中产生随机密码字符串
在.NET中产生随机密码字符串
来源: 发布时间:2007-09-05 发布人: 浏览: 人次   字体: [ ]  
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#实现查询IP所在区段的功能
·实例解释C#中Dispose和Close的区别
·C#区别和认识四个判等函数
·c#中的ArrayList属性祥解及其语法...
·C#中关于四舍五入函数的讨论及其解...
·使用C#编写查询IP段功能的程序
·关于C#在lucene.net下的中文切词
·关于C#程序中Bug的快速修复方法
·代码分析如何构造一个C#语言的爬虫程...
·c#中收发邮件处理代码(POP3,IMAP,...
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·有助快速理解C#委托和事件之...
·C#泛型学习笔记总结
·c#语言从dbf数据库提取数据并
·C#编写控制线程的运行和线程...
·怎样在C#中删除只读文件及其...
·C#函数中返回多个值的两种方...
·如何在C#中插入照片到Excel文
·如何在c#中获取事件注册的方...
·C#对timer类的使用操作代码
·什么是泛型?泛型的一些概述
·C#如何从文本文件读取信息并...
·如何在c#中去掉字符串中的回...
·如何使用C#获取IIS服务器版本...
·什么是泛型编程思想?及其简...
·c#子线程控制进度条的一个简...
热 门 文 章
·c#中收发邮件处理代码(POP3,...
·C#如何获取文件路径中的文件...
·什么是c#深拷贝与浅拷贝及其...
·C#中关于四舍五入函数的讨论...
·c#中的ArrayList属性祥解及其...
·C#语言实现创建、删除和移动...
·快速进行List排序的通用方法...
·C#对timer类的使用操作代码
·String.Split 方法使用及其代...
·如何在c#中去掉字符串中的回...
·用C#编写ActiveX控件代码实例
·C#语言实现从XML文件导出数据...
·C#如何从文本文件读取信息并...
·浅谈接口和抽象类的区别,加...
·c#子线程控制进度条的一个简...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .