.NET正防爆pression创建强密码密码、NET、pression

2023-09-03 04:10:29 作者:殇情

下面是我用来创建一个强壮的密码(这是不正确的为我的项目密码要求)的.NET正防爆pression:

Here is the .NET Regular Expression that I am using to create a strong password (which is not correct for my project password requirements):

(?=^.{15,25}$)(\d{2,}[a-z]{2,}[A-Z]{2,}[!@#$%&+~?]{2,})

密码要求:

最低15字符(最多25个) 两号 在两个大写字母 在两个小写字母 在两个特殊字符! @#$%&放大器; +〜? Minimum 15 Character (up to 25) Two Numbers Two Uppercase Letters Two Lowercase Letters Two Special Characters ! @ # $ % & + ~ ?

它们不需要是彼此&安培旁;在特定的顺序为普通防爆pression我粘贴要求。

They are not required to be beside one another & in the specific order as the Regular Expression that I pasted requires.

以上普通防爆pression需要这样的密码:12abCD @QWertyP

The above Regular Expression requires a password like this: 12abCD!@QWertyP

这要求他们在RE ...这不是我想要的特定顺序!

It REQUIRES them in the specific order in the RE... which is not what I want!

这应该通过正确格式的RE上面列出的规格:Qq1W w2Ee#3Rr4 @ TT5

This should pass a correctly formatted RE with the specifications listed above: Qq1W!w2Ee#3Rr4@Tt5

如何删除的必要性,他们是旁边彼此才能? 显然,密码应该是随机的,如果该人选择这样做。

How can I remove the necessity for them to be beside one another and in order?? Obviously the password should be random if the person so chooses.

推荐答案

我认为你正在寻找更多的比一个正则表达式的目的是做什么。

I think you're looking for more than what a regex was designed to do.

考虑一个C#/ VB的方法是这样的:

Consider a C#/VB method like this:

bool IsStrongPassword( String password )
{
    int upperCount = 0;
    int lowerCount = 0;
    int digitCount = 0;
    int symbolCount = 0;

    for ( int i = 0; i < password.Length; i++ )
    {
        if ( Char.IsUpper( password[ i ] ) )
            upperCount++;
        else if ( Char.IsLetter( password[ i ] ) )
            lowerCount++;
        else if ( Char.IsDigit( password[ i ] ) )
            digitCount++;
        else if ( Char.IsSymbol( password[ i ] ) )
            symbolCount++;
    }

    return password.Length >= 15 && upperCount >= 2 && lowerCount >= 2 && digitCount >= 2 && symbolCount >= 2;
}