温故而知新,看了一个C语言利用指针写的穷举密码的函数。正好有几个自己写的简易工具里需要使用到,随写一个C#版本。 public class PasswordString { private static readonly string _number = "0123456789"; private static readonly string _uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static readonly string _lowercase = "abcdefghijklmnopqrstuvwxyz"; private static string _other; #endregion public static string Number { get { return _number; } } public static string Uppercase { get { return _uppercase; } } public static string Lowercase { get { return _lowercase; } } public static string Other { get { return _other; } set { _other = Other; } } } public class EnumPassword { private ulong _Counter = 0; private string _Source = PasswordString.Number + PasswordString.Lowercase + PasswordString.Uppercase; private string _Password = ""; private ulong _Min = 3; private ulong _Max = 8; #endregion private string _GetPassword(ulong counter) { char [] str = new char[this._Max]; char [] src = _Source.ToCharArray(); ulong n = 0; ulong len = (ulong)src.Length; str[n] = src[counter % len]; while(counter >= len) { counter /= (ulong)src.Length; str[n + 1] = src[counter % len]; n++; } this._Password = new string(str).Substring(0, (int)(n + 1)); return this._Password; } #endregion public EnumPassword() { this._Counter = (ulong)Math.Pow(this._Source.Length, this._Min - 1); } public EnumPassword(ulong min, ulong max) { if(min > max) throw new Exception("Min is smaller than max."); this._Min = min; this._Max = max; this._Counter = (ulong)Math.Pow(this._Source.Length, this._Min - 1); } public string Password() { return this._Password; } public string Next() { ulong counter; lock(this) { counter = this._Counter; this._Counter++; } string pass = this._GetPassword(counter); return pass; } public string Source { get { return this._Source; } set { this._Source = Source; } } } 还有很多功能没有实现,不过那些都是简单玩意了。主要问题是效率不高,主要是string类型的使用实在是太XXX,等过几天,再优化优化~~~~大家有什么建议,帮我看看,提提~~ 相关文章: 2021-11-08 2021-09-18 2021-12-08 2021-10-12 2021-10-11 2022-12-23 2022-12-23