在很多场景下,我们需要一个强密码来保证可访问的数据或系统。如何检查你的密码安全级别,可以到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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)