zl程序教程

您现在的位置是:首页 >  后端

当前栏目

vb.net验证密码是否复杂的方法

Net密码方法密码 是否 验证 复杂 vb
2023-06-13 09:14:01 时间

可在安全的系统中使用密码来向用户授权。但是,密码必须难于被未授权用户猜测出来。攻击者可以使用一种“字典攻击”程序,该程序将遍历一本字典(或不同语言的多本字典)中的所有单词,并测试是否有任何单词就是用户的密码。诸如“Yankees”或“Mustang”等弱密码可被很快猜测出来。诸如“?You"L1N3vaFiNdMeyeP@sSWerd!”等强密码被猜测出来的可能性要小很多。密码保护系统应确保用户选择强密码。

强密码很复杂(包含大写、小写、数字和特殊字符的组合),并且不是单词。此示例演示如何验证复杂性。

示例 

复制代码代码如下:

"""<summary>Determinesifapasswordissufficientlycomplex.</summary>
"""<paramname="pwd">Passwordtovalidate</param>
"""<paramname="minLength">Minimumnumberofpasswordcharacters.</param>
"""<paramname="numUpper">Minimumnumberofuppercasecharacters.</param>
"""<paramname="numLower">Minimumnumberoflowercasecharacters.</param>
"""<paramname="numNumbers">Minimumnumberofnumericcharacters.</param>
"""<paramname="numSpecial">Minimumnumberofspecialcharacters.</param>
"""<returns>Trueifthepasswordissufficientlycomplex.</returns>
FunctionValidatePassword(ByValpwdAsString,_
OptionalByValminLengthAsInteger=8,_
OptionalByValnumUpperAsInteger=2,_
OptionalByValnumLowerAsInteger=2,_
OptionalByValnumNumbersAsInteger=2,_
OptionalByValnumSpecialAsInteger=2)_
AsBoolean
"Replace[A-Z]with\p{Lu},toallowforUnicodeuppercaseletters.
DimupperAsNewSystem.Text.RegularExpressions.Regex("[A-Z]")
DimlowerAsNewSystem.Text.RegularExpressions.Regex("[a-z]")
DimnumberAsNewSystem.Text.RegularExpressions.Regex("[0-9]")
"Specialis"noneoftheabove".
DimspecialAsNewSystem.Text.RegularExpressions.Regex("[^a-zA-Z0-9]")
"Checkthelength.
IfLen(pwd)<minLengthThenReturnFalse
"Checkforminimumnumberofoccurrences.
Ifupper.Matches(pwd).Count<numUpperThenReturnFalse
Iflower.Matches(pwd).Count<numLowerThenReturnFalse
Ifnumber.Matches(pwd).Count<numNumbersThenReturnFalse
Ifspecial.Matches(pwd).Count<numSpecialThenReturnFalse
"Passedallchecks.
ReturnTrue
EndFunction
SubTestValidatePassword()
DimpasswordAsString="Password"
"Demonstratethat"Password"isnotcomplex.
MsgBox(password&"iscomplex:"&ValidatePassword(password))
password="Z9f%a>2kQ"
"Demonstratethat"Z9f%a>2kQ"isnotcomplex.
MsgBox(password&"iscomplex:"&ValidatePassword(password))
EndSub
编译代码
通过传递包含该密码的字符串来调用此方法。

此示例需要:

访问System.Text.RegularExpressions命名空间的成员。如果没有在代码中完全限定成员名称,请添加Imports语句。有关更多信息,请参见Imports语句(.NET命名空间和类型)。

安全性
如果要在网络中转移密码,您需要使用安全的方法来传输数据。有关更多信息,请参见ASP.NETWeb应用程序安全性。

通过添加额外的复杂性检查,您可以改进ValidatePassword函数的准确性:

依据用户的名称、用户标识符和应用程序定义的字典来比较密码及其子字符串。此外,在执行比较时,将看起来类似的字符视为相同字符。例如,将字母“l”和“e”视为与数字“1”和“3”相同的字符。

如果只有一个大写字符,请确保它不是密码的第一个字符。

确保密码的最后两个字符是字母字符。

不允许这样的密码:其中的所有符号都是通过键盘最上面的一排键输入的。