.net教程:在.net中产生随机密码字符串
[size=3]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;
}
}
[/size]
页:
[1]