/// <summary> /// 產生英數字夾雜密碼 /// </summary> /// <param name="length">密碼長度</param> /// <returns></returns> public static string GeneratePassword(int length) { Collection<int> asciiCode = new Collection<int>(); // 數字的ASCII code for (int i = 48; i <= 57; i++) { asciiCode.Add(i); } // 英文A~Z的ASCII code for (int i = 65; i <= 90; i++) { asciiCode.Add(i); } // 英文a~z的ASCII code for (int i = 97; i <= 122; i++) { asciiCode.Add(i); } StringBuilder password = new StringBuilder(); Random random = new Random(); int count = asciiCode.Count; for (int i = 0; i < length; i++) { password.Append(Convert.ToChar(asciiCode[random.Next(count - 1)]).ToString()); } return password.ToString(); }
July 30, 2010
產生英數字夾雜密碼
不知有無更快方式,在此先紀錄一下,主要是利用ASCII Code加轉型產生密碼。
Subscribe to:
Posts (Atom)