判断是否是简单密码,不能包含相同的字符(如aaa,111),递增序列(abc,123),递减序列(cda,321)

判断是否是简单密码,不能包含相同的字符(如aaa,111),递增序列(abc,123),递减序列(cda,321)


/// <summary>
    /// 判断是否是简单密码,不能包含相同的字符(如aaa,111),递增序列(abc,123),递减序列(cda,321)
    /// </summary>
    /// <param name="strPassword">密码</param>
    /// <param name="intTimes">相同字符或递增递减字符的个数</param>
    /// <returns></returns>
    private static bool HaveSimpleCode(string strPassword, int intTimes)
    {
        #region === 基本参数 ===

        System.Collections.ArrayList arrChar = new System.Collections.ArrayList();
        string strChar = "";
        string strTempA = "";
        string strTempB = "";
        string strTempSameChar = "";

        #endregion

        // 遍历所有字符
        for (int i = 0; i < strPassword.Length; i++)
        {
            #region === 避免重复访问多次 ===

            strChar = strPassword.Substring(i, 1);
            // 避免重复访问多次
            if (!arrChar.Contains(strChar))
            {
                arrChar.Add(strChar);
            }
            else
            {
                continue;
            }

            #endregion

            #region === 多次相同字符 ===
            strTempSameChar = "";
            // 多次相同字符
            for (int k = 0; k < intTimes; k++)
            {
                strTempSameChar = strTempSameChar + strChar;
            }

            // 若包含多个相同字符,则返回
            if (strPassword.Contains(strTempSameChar))
            {
                return true;
            }

            #endregion

            #region === 是否拥有递增或递减的序列 ===

            System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();

            int intAsciiCode = (int)asciiEncoding.GetBytes(strChar)[0];

            strTempA = strChar;

            strTempB = strChar;

            // 0--9 A--Z a--z
            if ((intAsciiCode >= 48 && intAsciiCode <= 58 - intTimes) || (intAsciiCode >= 65 && intAsciiCode <= 91 - intTimes) || (intAsciiCode >= 97 && intAsciiCode <= 123 - intTimes))
            {
                int k = 1;
                // 执行intTimes次,构建 ABC或CBA,123或321 字符串
                while (k < intTimes)
                {
                    byte[] byteArray = new byte[] { (byte)(intAsciiCode + k) };

                    string strCharacter = asciiEncoding.GetString(byteArray);

                    strTempA = strTempA + strCharacter;

                    strTempB = strCharacter + strTempB;

                    k++;
                }
                // 判断是否拥有递增或递减的序列
                if (strPassword.Contains(strTempA) || strPassword.Contains(strTempB))
                    return true;
            }
            #endregion
        }
        // 运行到此,肯定不包含简单密码了
        return false;
    }

博文最后更新时间:


评论

  • 暂无评论

发表评论

博客统计

访问量:5581037

博文总数:751 评论总数:920189

原创126 翻译20 转载605