强大的密码测试 [英] Strong Password Test

查看:79
本文介绍了强大的密码测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vb.net编写桌面应用程序.我想测试用户创建的密码强度.我正在使用此代码,但它似乎对我不起作用.请协助.

I am writing a Desktop Application using vb.net. I want to test the password strength created by the users. I am using this code but it does not seem to work for me. Please assist.

Enum PasswordScore
    Blank = 0
    VeryWeak = 1
    Weak = 2
    Medium = 3
    GOOD = 4
    Strong = 5
    VeryStrong = 6
End Enum

Public Function CheckStrength(ByVal password As String) As PasswordScore
    Dim score As Integer = 1

    If password.Length < 1 Then
        Return PasswordScore.Blank
    End If
    If password.Length < 4 Then
        Return PasswordScore.VeryWeak
    End If

    If password.Length >= 5 Then
        score = score + 1
    End If
    If password.Length >= 10 Then
        score = score + 1
    End If

    If Regex.IsMatch(password, "/\d+/") Then
        score = score + 1
    End If

    If Regex.IsMatch(password, "/[a-z]/") Then

        score = score + 1
    End If

    If Regex.IsMatch(password, "/[0-9]/") Then

        score = score + 1
    End If

    If Regex.IsMatch(password, "/[A-Z]/") Then
        score = score + 1
    End If
    If Regex.IsMatch(password, "/[.,!,@,#,$,%,^,&]/") Then
        score = score + 1
    End If
    If Regex.IsMatch(password, "/[*,?,_,~,-,£,(,)]/") Then
        score = score + 1
    End If


    Return CType(score, PasswordScore)
End Function

推荐答案

,%,^,&]/") 1 结束 如果 如果 Regex.IsMatch(密码," ) 1 结束 如果 返回 CType (得分,PasswordScore) 结束 功能
,%,^,&]/") Then score = score + 1 End If If Regex.IsMatch(password, "/[*,?,_,~,-,£,(,)]/") Then score = score + 1 End If Return CType(score, PasswordScore) End Function


可能的问题是,您的分数可能比您用枚举表示的分数更高.

您可能想尝试的是在return语句之前添加以下代码:

The probable problem is that your score can be a higher value than you have enums to represent it.

What you might want to try is adding this code before the return statement:

score = Math.Min(DirectCast(PasswordScore, integer), score)



至少您会将值归一化为可以使用的值.但是,我将重构整个方法,并且可能不会像使用它们那样使用枚举.



At least you''ll normalize the value into something that can be used. However, I would refactor the entire method, and probably not use enums the way you''re using them.


我有第二个答案-如果您用Google进行强密码检查",您会发现一个网站,该网站建议一些正则表达式子句,您可以用它们来做同样的事情事物.要保持评分范例,请将每个正则表达式放入返回1或0的自己的方法中.

这是一个包含多个正则表达式子句的页面:

http://www.webpronews.com/expertarticles/2006/12/14/validating-strong-passwords-in-c-and-aspnet [
I have a 2nd answer - if you google "strong password check", you''ll find a site that suggests several regex clauses that you could use to do the very same thing. To maintain your scoring paradigm, put each regex into its own method that returns either 1 or 0.

Here''s a page that has several regex clauses:

http://www.webpronews.com/expertarticles/2006/12/14/validating-strong-passwords-in-c-and-aspnet[^]


这篇关于强大的密码测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆