项目需要为用户提供一个登录账号初始化的功能,这就要求一个产生随机密码的类,这看似简单,但需要做一些限制,比如,密码的长度、复杂度,是否包含数字、大写字母、小写字母、符号等等。同事搜索了一下在网上直接dowm下了一个类。他整理出来代码如下:
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace SYSU.FD.UA.BLL
6{
7 /**//// <remarks>产生随机密码</remarks>
8 internal class RandomPassword
9 {
10
11 /**//// <summary>
12 /// 生成的位数
13 /// </summary>
14 private int count = 8;
15
16 public int Count
17 {
18 get { return count; }
19 set { count = value; }
20 }
21 /**//// <summary>
22 /// 生成的种类
23 /// </summary>
24 private int type = 3;
25
26 public int Type
27 {
28 get { return type; }
29 set { type = value; }
30 }
31
32 /**//// <summary>
33 /// 构造函数
34 /// </summary>
35 /// <param name="count">长度</param>
36 /// <param name="type">种类</param>
37 public RandomPassword(int count, int type)
38 {
39 this.count = count;
40 this.type = type;
41 }
42
43 /**//// <summary>
44 /// 构造函数
45 /// </summary>
46 /// <param name="count">长度</param>
47 public RandomPassword(int count)
48 {
49 this.count = count;
50 }
51
52 /**//// <summary>
53 /// 构造函数
54 /// </summary>
55 public RandomPassword()
56 {
57
58 }
59
60 /**//// <summary>
61 /// 根据实例化返回随机生成的字符串
62 /// </summary>
63 /// <returns>返回随机生成的字符串</returns>
64 public string GetRandomNumber()
65 {
66 string strOut = GetRandomStringFromIntArray(intArray(this.count, this.type));
67 return strOut;
68 }
69
70 /**//// <summary>
71 /// 返回总数为生成位数的数组
72 /// </summary>
73 /// <param name="intCount">要生成的位数</param>
74 /// <param name="intTypeCount">生成类型的数目</param>
75 /// <returns>输出总数为生成位数的数组</returns>
76 private int[] intArray(int intCount, int intTypeCount)
77 {
78 int[] intArr = new int[intTypeCount];
79 int intTemp = 0;
80 int intTempNumber = 0;
81 System.Random ranNumber = new System.Random(System.DateTime.Now.Millisecond);
82 for (int i = 0; i < intTypeCount - 1; i++)
83 {
84 do
85 {
86 intTempNumber = ranNumber.Next(0, intCount);
87 }
88 while (intTemp + intTempNumber > intCount);
89 intTemp += intTempNumber;
90 intArr[i] = intTempNumber;
91 }
92 intArr[intTypeCount - 1] = intCount - intTemp;
93 return intArr;
94 }
95
96 /**//// <summary>
97 /// 由序列数组生成字符串
98 /// </summary>
99 /// <param name="intArrayNumber">由intArray生成的数据</param>
100 /// <returns>返加字符串</returns>
101 private string GetRandomStringFromIntArray(int[] intArrayNumber)
102 {
103 System.Random CharRand = new Random(System.DateTime.Now.Millisecond);
104 string strTemp = "";
105 for (int i = 0; i < intArrayNumber.Length; i++)
106 {
107 for (int j = 0; j < intArrayNumber[i]; j++)
108 {
109 if (i == 0)
110 strTemp += Convert.ToChar(CharRand.Next(65, 90)).ToString(); //A-Z
111 if (i == 1)
112 strTemp += Convert.ToChar(CharRand.Next(97, 122)).ToString(); //a-z
113 if (i == 2)
114 strTemp += Convert.ToChar(CharRand.Next(49, 57)).ToString(); //1-9 没用"0"是为了避免和字母O混淆
115 }
116 }
117 return strTemp;
118 }
119
120 }
121}
122
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace SYSU.FD.UA.BLL
6{
7 /**//// <remarks>产生随机密码</remarks>
8 internal class RandomPassword
9 {
10
11 /**//// <summary>
12 /// 生成的位数
13 /// </summary>
14 private int count = 8;
15
16 public int Count
17 {
18 get { return count; }
19 set { count = value; }
20 }
21 /**//// <summary>
22 /// 生成的种类
23 /// </summary>
24 private int type = 3;
25
26 public int Type
27 {
28 get { return type; }
29 set { type = value; }
30 }
31
32 /**//// <summary>
33 /// 构造函数
34 /// </summary>
35 /// <param name="count">长度</param>
36 /// <param name="type">种类</param>
37 public RandomPassword(int count, int type)
38 {
39 this.count = count;
40 this.type = type;
41 }
42
43 /**//// <summary>
44 /// 构造函数
45 /// </summary>
46 /// <param name="count">长度</param>
47 public RandomPassword(int count)
48 {
49 this.count = count;
50 }
51
52 /**//// <summary>
53 /// 构造函数
54 /// </summary>
55 public RandomPassword()
56 {
57
58 }
59
60 /**//// <summary>
61 /// 根据实例化返回随机生成的字符串
62 /// </summary>
63 /// <returns>返回随机生成的字符串</returns>
64 public string GetRandomNumber()
65 {
66 string strOut = GetRandomStringFromIntArray(intArray(this.count, this.type));
67 return strOut;
68 }
69
70 /**//// <summary>
71 /// 返回总数为生成位数的数组
72 /// </summary>
73 /// <param name="intCount">要生成的位数</param>
74 /// <param name="intTypeCount">生成类型的数目</param>
75 /// <returns>输出总数为生成位数的数组</returns>
76 private int[] intArray(int intCount, int intTypeCount)
77 {
78 int[] intArr = new int[intTypeCount];
79 int intTemp = 0;
80 int intTempNumber = 0;
81 System.Random ranNumber = new System.Random(System.DateTime.Now.Millisecond);
82 for (int i = 0; i < intTypeCount - 1; i++)
83 {
84 do
85 {
86 intTempNumber = ranNumber.Next(0, intCount);
87 }
88 while (intTemp + intTempNumber > intCount);
89 intTemp += intTempNumber;
90 intArr[i] = intTempNumber;
91 }
92 intArr[intTypeCount - 1] = intCount - intTemp;
93 return intArr;
94 }
95
96 /**//// <summary>
97 /// 由序列数组生成字符串
98 /// </summary>
99 /// <param name="intArrayNumber">由intArray生成的数据</param>
100 /// <returns>返加字符串</returns>
101 private string GetRandomStringFromIntArray(int[] intArrayNumber)
102 {
103 System.Random CharRand = new Random(System.DateTime.Now.Millisecond);
104 string strTemp = "";
105 for (int i = 0; i < intArrayNumber.Length; i++)
106 {
107 for (int j = 0; j < intArrayNumber[i]; j++)
108 {
109 if (i == 0)
110 strTemp += Convert.ToChar(CharRand.Next(65, 90)).ToString(); //A-Z
111 if (i == 1)
112 strTemp += Convert.ToChar(CharRand.Next(97, 122)).ToString(); //a-z
113 if (i == 2)
114 strTemp += Convert.ToChar(CharRand.Next(49, 57)).ToString(); //1-9 没用"0"是为了避免和字母O混淆
115 }
116 }
117 return strTemp;
118 }
119
120 }
121}
122
一看,挺复杂,而且好像也具备了必要的灵活性,我觉得还不错,也就没有仔细阅读理解代码了。但一运行才发现问题,“De147353”、“bpde2765”、“XNGGXMX3”、“RXVNA368”,里面包含的大写字母、小写字母和数字,同一类型的都是一起产生,感觉这个随机性有点莫名其妙。
最好,还是调整思路,自己写了一个,虽说并不完善,但能很好的满足自己的需要,也有一定灵活性。思路是这样的,先创建四个个字段,分别保存数字字符、大写字母、小写字母、符号,再创建一个保存全部字符,同样还有一个最重要的是保存密码长度。提供多种构造函数,让用户随意定制自己的随机字符串格式,用户可以选择自己需要的字符,也可以使用系统默认的,并且可以设置产生密码的长度。在类实例化的最后,将字符混合(可以按照一定策略),并填充到另一字段。在用户获取随机字符串时,根据要求的随机字符长度,随机的挑出所有字符中的部分。一般一个实例只允许产生一种规格的密码,但我也提供了一个函数,用于补充字符,稍微改变规格。
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace SYSU.FD.UA.BLL
6{
7 /**//// <remarks>产生随机字符串</remarks>
8 public class RandomString
9 {
10 /**//// <summary>
11 /// 大学字母
12 /// </summary>
13 private string majuscule;
14 /**//// <summary>
15 /// 小写字母
16 /// </summary>
17 private string lowercase;
18 /**//// <summary>
19 /// 数字
20 /// </summary>
21 private string number;
22 /**//// <summary>
23 /// 常见符号
24 /// </summary>
25 private string symbol;
26 /**//// <summary>
27 /// 生成随机字符串的长度
28 /// </summary>
29 private int length;
30
31 /**//// <summary>
32 /// 所有的待选择字符
33 /// </summary>
34 private string allchar;
35
36 /**//// <summary>
37 /// 全参数构造函数
38 /// </summary>
39 /// <param name="majuscule">大写字母</param>
40 /// <param name="lowercase">小写字母</param>
41 /// <param name="number">数字</param>
42 /// <param name="symbol">符号</param>
43 /// <param name="length">生成随机字符串的长度</param>
44 public RandomString(string majuscule, string lowercase, string number, string symbol, int length)
45 {
46 this.majuscule = majuscule;
47 this.lowercase = lowercase;
48 this.number = number;
49 this.symbol = symbol;
50 this.length = length;
51 this.SetAllChar();
52 }
53
54 /**//// <summary>
55 /// 构造函数
56 /// </summary>
57 /// <param name="majuscule">大写字母</param>
58 /// <param name="lowercase">小写字母</param>
59 /// <param name="number">数字</param>
60 /// <param name="symbol">符号</param>
61 public RandomString(string majuscule, string lowercase, string number, string symbol)
62 {
63 this.majuscule = majuscule;
64 this.lowercase = lowercase;
65 this.number = number;
66 this.symbol = symbol;
67 this.length = 8;
68 this.SetAllChar();
69 }
70
71 /**//// <summary>
72 /// 构造函数,为待选字符串进行一次性赋值
73 /// </summary>
74 /// <param name="anyChar">待选字符串</param>
75 public RandomString(string anyChar)
76 {
77 this.allchar = anyChar;
78 this.length = 8;
79 }
80
81 /**//// <summary>
82 /// 构造函数,为待选字符串进行一次性赋值,并初始化随机串长度
83 /// </summary>
84 /// <param name="anyChar">待选字符串</param>
85 /// <param name="length">生成随机字符串的长度</param>
86 public RandomString(string anyChar, int length)
87 {
88 this.allchar = anyChar;
89 this.length = 8;
90 }
91
92 /**//// <summary>
93 /// 构造函数,待选字符都为默认值
94 /// </summary>
95 /// <param name="length">生成随机字符串的长度</param>
96 public RandomString(int length)
97 {
98 this.InitDefaultValues();
99 this.length = length;
100 this.SetAllChar();
101 }
102
103 /**//// <summary>
104 /// 构造函数,待选字符和随机串长度都为默认值
105 /// </summary>
106 public RandomString()
107 {
108 this.InitDefaultValues();
109 this.length = 8;
110 this.SetAllChar();
111 }
112
113 /**//// <summary>
114 /// 初始化默认值
115 /// </summary>
116 private void InitDefaultValues()
117 {
118 //隐藏了
119 }
120
121 /**//// <summary>
122 /// 主要方法,产生随机字符串
123 /// </summary>
124 /// <remarks>通过实例,多次调用该方法,产生的随机串位数和随机字符组成规则是相同的,但实际的字符串并不相同。</remarks>
125 /// <returns>一个限长的随机字符串,其中包含的随机字符组成有一定规则</returns>
126 public string CreateRandomString()
127 {
128 //隐藏了
129 }
130
131 /**//// <summary>
132 /// 增加字符,以字符串的形式
133 /// </summary>
134 /// <remarks>直接增加但待选字符串中,不排除各种重复情况,适当重复待选字符,可以调整随机几率</remarks>
135 /// <param name="anyChar">包含增加字符的字符串</param>
136 /// <returns>无</returns>
137 public void AddChar(string anyChar)
138 {
139 this.allchar += anyChar;
140 }
141
142 /**//// <summary>
143 /// 将某些字符从待选随机串中移除
144 /// </summary>
145 public void RemoveChar(string anyChar)
146 {
147 string newAllChar = "";
148 foreach (Char theChar in this.allchar)
149 {
150 if(!anyChar.Contains(theChar.ToString()))
151 newAllChar +=theChar;
152 }
153 }
154
155 /**//// <summary>
156 /// 将各种待选字符进行组合
157 /// </summary>
158 private void SetAllChar()
159 {
160 this.allchar = this.majuscule + this.lowercase + this.number + this.symbol;
161 }
162 }
163}
164
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace SYSU.FD.UA.BLL
6{
7 /**//// <remarks>产生随机字符串</remarks>
8 public class RandomString
9 {
10 /**//// <summary>
11 /// 大学字母
12 /// </summary>
13 private string majuscule;
14 /**//// <summary>
15 /// 小写字母
16 /// </summary>
17 private string lowercase;
18 /**//// <summary>
19 /// 数字
20 /// </summary>
21 private string number;
22 /**//// <summary>
23 /// 常见符号
24 /// </summary>
25 private string symbol;
26 /**//// <summary>
27 /// 生成随机字符串的长度
28 /// </summary>
29 private int length;
30
31 /**//// <summary>
32 /// 所有的待选择字符
33 /// </summary>
34 private string allchar;
35
36 /**//// <summary>
37 /// 全参数构造函数
38 /// </summary>
39 /// <param name="majuscule">大写字母</param>
40 /// <param name="lowercase">小写字母</param>
41 /// <param name="number">数字</param>
42 /// <param name="symbol">符号</param>
43 /// <param name="length">生成随机字符串的长度</param>
44 public RandomString(string majuscule, string lowercase, string number, string symbol, int length)
45 {
46 this.majuscule = majuscule;
47 this.lowercase = lowercase;
48 this.number = number;
49 this.symbol = symbol;
50 this.length = length;
51 this.SetAllChar();
52 }
53
54 /**//// <summary>
55 /// 构造函数
56 /// </summary>
57 /// <param name="majuscule">大写字母</param>
58 /// <param name="lowercase">小写字母</param>
59 /// <param name="number">数字</param>
60 /// <param name="symbol">符号</param>
61 public RandomString(string majuscule, string lowercase, string number, string symbol)
62 {
63 this.majuscule = majuscule;
64 this.lowercase = lowercase;
65 this.number = number;
66 this.symbol = symbol;
67 this.length = 8;
68 this.SetAllChar();
69 }
70
71 /**//// <summary>
72 /// 构造函数,为待选字符串进行一次性赋值
73 /// </summary>
74 /// <param name="anyChar">待选字符串</param>
75 public RandomString(string anyChar)
76 {
77 this.allchar = anyChar;
78 this.length = 8;
79 }
80
81 /**//// <summary>
82 /// 构造函数,为待选字符串进行一次性赋值,并初始化随机串长度
83 /// </summary>
84 /// <param name="anyChar">待选字符串</param>
85 /// <param name="length">生成随机字符串的长度</param>
86 public RandomString(string anyChar, int length)
87 {
88 this.allchar = anyChar;
89 this.length = 8;
90 }
91
92 /**//// <summary>
93 /// 构造函数,待选字符都为默认值
94 /// </summary>
95 /// <param name="length">生成随机字符串的长度</param>
96 public RandomString(int length)
97 {
98 this.InitDefaultValues();
99 this.length = length;
100 this.SetAllChar();
101 }
102
103 /**//// <summary>
104 /// 构造函数,待选字符和随机串长度都为默认值
105 /// </summary>
106 public RandomString()
107 {
108 this.InitDefaultValues();
109 this.length = 8;
110 this.SetAllChar();
111 }
112
113 /**//// <summary>
114 /// 初始化默认值
115 /// </summary>
116 private void InitDefaultValues()
117 {
118 //隐藏了
119 }
120
121 /**//// <summary>
122 /// 主要方法,产生随机字符串
123 /// </summary>
124 /// <remarks>通过实例,多次调用该方法,产生的随机串位数和随机字符组成规则是相同的,但实际的字符串并不相同。</remarks>
125 /// <returns>一个限长的随机字符串,其中包含的随机字符组成有一定规则</returns>
126 public string CreateRandomString()
127 {
128 //隐藏了
129 }
130
131 /**//// <summary>
132 /// 增加字符,以字符串的形式
133 /// </summary>
134 /// <remarks>直接增加但待选字符串中,不排除各种重复情况,适当重复待选字符,可以调整随机几率</remarks>
135 /// <param name="anyChar">包含增加字符的字符串</param>
136 /// <returns>无</returns>
137 public void AddChar(string anyChar)
138 {
139 this.allchar += anyChar;
140 }
141
142 /**//// <summary>
143 /// 将某些字符从待选随机串中移除
144 /// </summary>
145 public void RemoveChar(string anyChar)
146 {
147 string newAllChar = "";
148 foreach (Char theChar in this.allchar)
149 {
150 if(!anyChar.Contains(theChar.ToString()))
151 newAllChar +=theChar;
152 }
153 }
154
155 /**//// <summary>
156 /// 将各种待选字符进行组合
157 /// </summary>
158 private void SetAllChar()
159 {
160 this.allchar = this.majuscule + this.lowercase + this.number + this.symbol;
161 }
162 }
163}
164