LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

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

freeflydom
2025年5月17日 9:54 本文热度 57

参考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: 非常弱

代码如下

    /// <summary>
    /// 得到强度的文本描述
    /// </summary>
    /// <param name="password"></param>
    /// <returns></returns>
    public static string GetkPasswordStrong(string password)
    {
        int strong=CheckPasswordStrong(password);
        if (strong >= 90)
            return $"非常安全{strong}";
        else if (strong >= 80)
            return $"安全{strong}";
        else if (strong >= 70)
            return $"非常强{strong}";
        else if (strong >= 60)
            return $"强{strong}";
        else if (strong >= 50)
            return $"一般{strong}";
        else if (strong >= 25)
            return $"弱{strong}";
        else
            return $"非常弱{strong}";
    }
    /// <summary>
    /// 检测密码强度,规则(仿 google)
    /// 
    ///= 90: 非常安全
    ///>= 80: 安全(Secure)
    ///>= 70: 非常强
    ///>= 60: 强(Strong)
    ///>= 50: 一般(Average)
    ///>= 25: 弱(Weak)
    ///>= 0: 非常弱
    /// </summary>
    /// <param name="password"></param>
    /// <returns></returns>
    public static int CheckPasswordStrong(string password)  //检测密码强度  规则(仿 google)
    {
        int Result = 0;
        int i, countLowercase, countUppercase, countDigit, countSymbol;
        int iLen=password.Length;
        if (iLen <= 4)
            Result = 5;
        else if (iLen >= 5 && iLen <= 7)
            Result = 10;
        else if (iLen >= 8)
            Result = 25;
        countLowercase = 0;
        countUppercase = 0;
        countDigit = 0;
        countSymbol = 0;
        for (i = 0; i < iLen;i++)
        {
            char c= password[i];
            if (c >= '0' && c <= '9')
                countDigit++;
            else if (c >= 'a' && c <= 'z')
                countLowercase++;
            else if (c >= 'A' && c <= 'Z')
                countUppercase++;
            else
                countSymbol++;
        }
        //大小写
        //0 分: 没有字母
        //10 分: 全都是小(大)写字母
        //20 分: 大小写混合字母
        if (countLowercase == 0 && countUppercase == 0)
            Result = Result + 0;
        else if ((countLowercase == 0 && countUppercase > 0) || (countLowercase > 0 && countUppercase == 0))
            Result = Result + 10;
        else if (countLowercase >0 && countUppercase > 0)
            Result = Result + 20;
        // 计算数字得分
        if (countDigit == 0)
            Result = Result + 0;
        else if (countDigit == 1)
            Result = Result + 10;
        else if (countDigit >= 3)
            Result = Result + 20;
        //符号评分
        if (countSymbol == 0)
            Result = Result + 0;
        else if (countSymbol == 1)
            Result = Result + 10;
        else if (countSymbol > 1)
            Result = Result + 25;
        //五、奖励:
        //2 分: 字母和数字
        //3 分: 字母、数字和符号
        //5 分: 大小写字母、数字和符号
        if ((countLowercase > 0) && (countUppercase > 0) && (countDigit > 0) && (countSymbol == 0))
            Result = Result + 2;
        else if ((countLowercase > 0) && (countUppercase > 0) && (countDigit > 0) && (countSymbol > 0))
            Result = Result + 5;
       else if (((countLowercase > 0) || (countUppercase > 0)) & (countDigit > 0) && (countSymbol > 0))
            Result = Result + 3;
        return Result;
    }

转自https://www.cnblogs.com/james-yu/p/18251480


该文章在 2025/5/17 9:55:31 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved