C# C/S程序密码强度判断 怎么弄?
最好是有一个适合于C# C/S程序的控件来验证,就像asp.Net中的控件那样。
或者自己写应该怎么判断?
我是设想当超过6位字符开始判断
大于6个长度后每超过1个长度得2分
每出现一类(大写、小写、数字、0-9上方的标点、主键盘区右侧的标点共5类)得3分
其中特殊符号每出现1种就可以得3分
大小写相互切换1次得2分
大小写每出现一个之前没出现过的得2分
数字每出现一个之前没出现的得1分
求各位指导一下
[解决办法]
用正则来
给个参考http://blog.csdn.net/jinru2560/article/details/4412344
[解决办法]
用户控件CheckPwdStrength
添加控件:
ProgressBar prgStrength 进度条
Label lblWeak 强
Label lblModerate 中
Label lblStrong 弱
代码:
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; // 中 lblModerate.Visible = false; // 强 lblStrong.Visible = false; progbStrength.Value = 0; } // 密码长度是否大于6位 else if (Pssword.Trim().ToString().Length <= 6) { lblWeak.Visible = true; lblWeak.Text = " 密码太短" ; lblModerate.Visible = false; lblStrong.Visible = false; progbStrength.Value = 10; } else { // 获取密码强度 int _Strength = CheckStrong(Pssword.Trim().ToString()); // 强 if (_Strength == 1) { lblStrong.Visible = true; lblWeak.Visible = false; lblModerate.Visible = false; progbStrength.Value = 100; } // 中 else if (_Strength == 2) { lblModerate.Visible = true; lblWeak.Visible = false; lblStrong.Visible = false; progbStrength.Value = 60; } // 弱 else if (_Strength == 3) { lblWeak.Visible = true; lblWeak.Text = " 弱" ; lblModerate.Visible = false; lblStrong.Visible = false; progbStrength.Value = 20; } else { lblWeak.Visible = false; lblModerate.Visible = false; lblStrong.Visible = false; progbStrength.Value = 0; } } } #endregion } }