在很多场景下,我们需要一个强密码来保证可访问的数据或系统。如何检查你的密码安全级别,可以到Microsoft Online Safety – Using strong password这个网站。代码并不复杂,看下面片段代码:
1: /// <summary>
2: /// create another string which is a concatenation of all above
3: /// </summary>
4: private string allChars = alphaCaps + alphaLow + numerics + special;
5: /// <summary>
6: /// create constant strings for each type of characters
7: /// </summary>
8: private static readonly string alphaCaps = "QWERTYUIOPASDFGHJKLZXCVBNM";
9: private static readonly string alphaLow = "qwertyuiopasdfghjklzxcvbnm";
10: private static readonly string numerics = "1234567890";
11: private Random random = new Random();
12: private static readonly string special = "@#$";
13:
14: #endregion Fields
15:
16: #region Methods (3)
17:
18: // Public Methods (1)
19:
20: /// <summary>
21: /// Generates the strong password.
22: /// </summary>
23: /// <remarks>
24: /// Generate four repeating random numbers are postions of lower, upper, numeric and special characters
25: /// By filling these positions with corresponding characters,we can ensure the password has atleast one character of those types
26: /// </remarks>
27: /// <param name="length">The length.</param>
28: /// <returns>password string</returns>
29: public string GenerateStrongPassword(int length)
30: {
31: StringBuilder generatedPassword = new StringBuilder(length);
32:
33: if (length < 4)
34: throw new Exception("Number of characters should be greater than 4.");
35:
36: int pLower, pUpper, pNumber, pSpecial;
37: string posArray = "0123456789";
38: if (length < posArray.Length)
39: posArray = posArray.Substring(0, length);
40: pLower = GetRandomPosition(ref posArray);
41: pUpper = GetRandomPosition(ref posArray);
42: pNumber = GetRandomPosition(ref posArray);
43: pSpecial = GetRandomPosition(ref posArray);
44:
45: for (int i = 0; i < length; i++)
46: {
47: if (i == pLower)
48: generatedPassword.Append(GetRandomChar(alphaCaps));
49: else if (i == pUpper)
50: generatedPassword.Append(GetRandomChar(alphaLow));
51: else if (i == pNumber)
52: generatedPassword.Append(GetRandomChar(numerics));
53: else if (i == pSpecial)
54: generatedPassword.Append(GetRandomChar(special));
55: else
56: generatedPassword.Append(GetRandomChar(allChars));
57: }
58: return generatedPassword.ToString();
59: }
60: // Private Methods (2)
61:
62: /// <summary>
63: /// Gets the random char.
64: /// </summary>
65: /// <param name="fullString">The full string.</param>
66: /// <returns></returns>
67: private string GetRandomChar(string fullString)
68: {
69: return fullString.ToCharArray()[(int)Math.Floor(random.NextDouble() * fullString.Length)].ToString();
70: }
71:
72: /// <summary>
73: /// Gets the random position.
74: /// </summary>
75: /// <param name="posArray">The pos array.</param>
76: /// <returns></returns>
77: private int GetRandomPosition(ref string posArray)
78: {
79: string randomChar = posArray.ToCharArray()[(int) Math.Floor(random.NextDouble()
80: *posArray.Length)].ToString();
81: int pos = int.Parse(randomChar);
82: posArray = posArray.Replace(randomChar, "");
83: return pos;
84: }
例如我们用上面的CODE生成一个长度为8位密码是: ASSu#zO3,你可以写一个UI去显示它们了。
希望对您有帮助。
Author: Petter Liu http://wintersun.cnblogs.com