參考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 編輯過