C#检测密码强度,评分规则仿google
				
									
					
					
						 | 
						
							
							freeflydom 
							
							
								2025年5月17日 9:54
								本文热度 2125
							
							 
						 | 
					
					
				 
				参考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: 非常弱
代码如下
    
    
    
    
    
    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}";
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    public static int CheckPasswordStrong(string password)  
    {
        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++;
        }
        
        
        
        
        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;
        
        
        
        
        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 编辑过