翼度科技»论坛 编程开发 .net 查看内容

c# 检测密码强度,评分规则仿google

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
参考delphi的代码更改为C#
Delphi 检测密码强度 规则(仿 google)
仿 google 评分规则

一、密码长度:

5 分: 小于等于 4 个字符
10 分: 5 到 7 字符
25 分: 大于等于 8 个字符
二、字母:

0 分: 没有字母
10 分: 全都是小(大)写字母
20 分: 大小写混合字母
三、数字:

0 分: 没有数字
10 分: 1 个数字
20 分: 大于等于 3 个数字
四、符号:

0 分: 没有符号
10 分: 1 个符号
25 分: 大于 1 个符号
五、奖励:

2 分: 字母和数字
3 分: 字母、数字和符号
5 分: 大小写字母、数字和符号
最后的评分标准:

>= 90: 非常安全
>= 80: 安全(Secure)
>= 70: 非常强
>= 60: 强(Strong)
>= 50: 一般(Average)
>= 25: 弱(Weak)
>= 0: 非常弱
代码如下

`
  1.     /// <summary>
  2.     /// 得到强度的文本描述
  3.     /// </summary>
  4.     /// <param name="password"></param>
  5.     /// <returns></returns>
  6.     public static string GetkPasswordStrong(string password)
  7.     {
  8.         int strong=CheckPasswordStrong(password);
  9.         if (strong >= 90)
  10.             return $"非常安全{strong}";
  11.         else if (strong >= 80)
  12.             return $"安全{strong}";
  13.         else if (strong >= 70)
  14.             return $"非常强{strong}";
  15.         else if (strong >= 60)
  16.             return $"强{strong}";
  17.         else if (strong >= 50)
  18.             return $"一般{strong}";
  19.         else if (strong >= 25)
  20.             return $"弱{strong}";
  21.         else
  22.             return $"非常弱{strong}";
  23.     }
  24.     /// <summary>
  25.     /// 检测密码强度,规则(仿 google)
  26.     ///
  27.     ///= 90: 非常安全
  28.     ///>= 80: 安全(Secure)
  29.     ///>= 70: 非常强
  30.     ///>= 60: 强(Strong)
  31.     ///>= 50: 一般(Average)
  32.     ///>= 25: 弱(Weak)
  33.     ///>= 0: 非常弱
  34.     /// </summary>
  35.     /// <param name="password"></param>
  36.     /// <returns></returns>
  37.     public static int CheckPasswordStrong(string password)  //检测密码强度  规则(仿 google)
  38.     {
  39.         int Result = 0;
  40.         int i, countLowercase, countUppercase, countDigit, countSymbol;
  41.         int iLen=password.Length;
  42.         if (iLen <= 4)
  43.             Result = 5;
  44.         else if (iLen >= 5 && iLen <= 7)
  45.             Result = 10;
  46.         else if (iLen >= 8)
  47.             Result = 25;
  48.         countLowercase = 0;
  49.         countUppercase = 0;
  50.         countDigit = 0;
  51.         countSymbol = 0;
  52.         for (i = 0; i < iLen;i++)
  53.         {
  54.             char c= password[i];
  55.             if (c >= '0' && c <= '9')
  56.                 countDigit++;
  57.             else if (c >= 'a' && c <= 'z')
  58.                 countLowercase++;
  59.             else if (c >= 'A' && c <= 'Z')
  60.                 countUppercase++;
  61.             else
  62.                 countSymbol++;
  63.         }
  64.         //大小写
  65.         //0 分: 没有字母
  66.         //10 分: 全都是小(大)写字母
  67.         //20 分: 大小写混合字母
  68.         if (countLowercase == 0 && countUppercase == 0)
  69.             Result = Result + 0;
  70.         else if ((countLowercase == 0 && countUppercase > 0) || (countLowercase > 0 && countUppercase == 0))
  71.             Result = Result + 10;
  72.         else if (countLowercase >0 && countUppercase > 0)
  73.             Result = Result + 20;
  74.         // 计算数字得分
  75.         if (countDigit == 0)
  76.             Result = Result + 0;
  77.         else if (countDigit == 1)
  78.             Result = Result + 10;
  79.         else if (countDigit >= 3)
  80.             Result = Result + 20;
  81.         //符号评分
  82.         if (countSymbol == 0)
  83.             Result = Result + 0;
  84.         else if (countSymbol == 1)
  85.             Result = Result + 10;
  86.         else if (countSymbol > 1)
  87.             Result = Result + 25;
  88.         //五、奖励:
  89.         //2 分: 字母和数字
  90.         //3 分: 字母、数字和符号
  91.         //5 分: 大小写字母、数字和符号
  92.         if ((countLowercase > 0) && (countUppercase > 0) && (countDigit > 0) && (countSymbol == 0))
  93.             Result = Result + 2;
  94.         else if ((countLowercase > 0) && (countUppercase > 0) && (countDigit > 0) && (countSymbol > 0))
  95.             Result = Result + 5;
  96.        else if (((countLowercase > 0) || (countUppercase > 0)) & (countDigit > 0) && (countSymbol > 0))
  97.             Result = Result + 3;
  98.         return Result;
  99.     }
复制代码
`

来源:https://www.cnblogs.com/james-yu/p/18251480
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具