日期:2014-05-18 浏览次数:21083 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace Pwd { public partial class CheckPwdStrength : UserControl { public CheckPwdStrength() { InitializeComponent(); } private string _pssword = string.Empty; /// <summary> /// 密码 /// </summary> [Description(" 密码" ), Category(" 密码验证设置" ), Browsable(true)] public string Pssword { get { return _pssword; } set { _pssword = value; // 执行密码强度验证 CheckPwd(); } } #region 检查密码强度 /// <summary> /// 检查字符是属于哪一类 /// </summary> /// <param name="_in"></param> /// <returns></returns> private int CharMode(int _in) { if (_in >= 48 && _in <= 57) // 数字 return 1; if (_in >= 65 && _in <= 90) // 大写字母 return 2; if (_in >= 97 && _in <= 122) // 小写 return 4; else return 8; // 特殊字符 } /// <summary> /// 计算出当前密码当中一共有多少种模式 /// </summary> /// <param name="_num"></param> /// <returns></returns> private int BitTotal(int _num) { int modes = 0; for (int i = 0; i < 4; i++) { // 逐位运算 if ((_num & 1) == 0) modes++; // 右移位运算 _num >>= 1; } return modes; } /// <summary> /// 检查强度,返回密码的强度级别 /// </summary> /// <param name="_pwd"></param> /// <returns></returns> private int CheckStrong(string _pwd) { // 判断密码长度是否大于6位 if (_pwd.Length <= 6) { return 0; // 密码太短 } else { int Modes = 0; for (int i = 0; i < _pwd.Length; i++) { // 获取密码字符串字节数组 byte[] by_pwd = System.Text.Encoding.Default.GetBytes(_pwd); // 或赋值运算 Modes |= CharMode(by_pwd[i]); } return BitTotal(Modes); } } #endregion #region 执行密码强度验证 /// <summary> /// 执行密码强度验证 /// </summary> private void CheckPwd() { if (Pssword.Trim().ToString().Length == 0) { // 弱 lblWeak.Visible = false;