vb.net驗(yàn)證密碼是否復(fù)雜的方法
可在安全的系統(tǒng)中使用密碼來(lái)向用戶授權(quán)。但是,密碼必須難于被未授權(quán)用戶猜測(cè)出來(lái)。攻擊者可以使用一種“字典攻擊”程序,該程序?qū)⒈闅v一本字典(或不同語(yǔ)言的多本字典)中的所有單詞,并測(cè)試是否有任何單詞就是用戶的密碼。諸如“Yankees”或“Mustang”等弱密碼可被很快猜測(cè)出來(lái)。諸如“?You'L1N3vaFiNdMeyeP@sSWerd!”等強(qiáng)密碼被猜測(cè)出來(lái)的可能性要小很多。密碼保護(hù)系統(tǒng)應(yīng)確保用戶選擇強(qiáng)密碼。
強(qiáng)密碼很復(fù)雜(包含大寫(xiě)、小寫(xiě)、數(shù)字和特殊字符的組合),并且不是單詞。此示例演示如何驗(yàn)證復(fù)雜性。
示例
''' <summary>Determines if a password is sufficiently complex.</summary>
''' <param name="pwd">Password to validate</param>
''' <param name="minLength">Minimum number of password characters.</param>
''' <param name="numUpper">Minimum number of uppercase characters.</param>
''' <param name="numLower">Minimum number of lowercase characters.</param>
''' <param name="numNumbers">Minimum number of numeric characters.</param>
''' <param name="numSpecial">Minimum number of special characters.</param>
''' <returns>True if the password is sufficiently complex.</returns>
Function ValidatePassword(ByVal pwd As String, _
Optional ByVal minLength As Integer = 8, _
Optional ByVal numUpper As Integer = 2, _
Optional ByVal numLower As Integer = 2, _
Optional ByVal numNumbers As Integer = 2, _
Optional ByVal numSpecial As Integer = 2) _
As Boolean
' Replace [A-Z] with \p{Lu}, to allow for Unicode uppercase letters.
Dim upper As New System.Text.RegularExpressions.Regex("[A-Z]")
Dim lower As New System.Text.RegularExpressions.Regex("[a-z]")
Dim number As New System.Text.RegularExpressions.Regex("[0-9]")
' Special is "none of the above".
Dim special As New System.Text.RegularExpressions.Regex("[^a-zA-Z0-9]")
' Check the length.
If Len(pwd) < minLength Then Return False
' Check for minimum number of occurrences.
If upper.Matches(pwd).Count < numUpper Then Return False
If lower.Matches(pwd).Count < numLower Then Return False
If number.Matches(pwd).Count < numNumbers Then Return False
If special.Matches(pwd).Count < numSpecial Then Return False
' Passed all checks.
Return True
End Function
Sub TestValidatePassword()
Dim password As String = "Password"
' Demonstrate that "Password" is not complex.
MsgBox(password & " is complex: " & ValidatePassword(password))
password = "Z9f%a>2kQ"
' Demonstrate that "Z9f%a>2kQ" is not complex.
MsgBox(password & " is complex: " & ValidatePassword(password))
End Sub
通過(guò)傳遞包含該密碼的字符串來(lái)調(diào)用此方法。
此示例需要:
訪問(wèn) System.Text.RegularExpressions 命名空間的成員。如果沒(méi)有在代碼中完全限定成員名稱,請(qǐng)?zhí)砑?Imports 語(yǔ)句。有關(guān)更多信息,請(qǐng)參見(jiàn) Imports 語(yǔ)句(.NET 命名空間和類型)。
安全性
如果要在網(wǎng)絡(luò)中轉(zhuǎn)移密碼,您需要使用安全的方法來(lái)傳輸數(shù)據(jù)。有關(guān)更多信息,請(qǐng)參見(jiàn) ASP.NET Web 應(yīng)用程序安全性。
通過(guò)添加額外的復(fù)雜性檢查,您可以改進(jìn) ValidatePassword 函數(shù)的準(zhǔn)確性:
依據(jù)用戶的名稱、用戶標(biāo)識(shí)符和應(yīng)用程序定義的字典來(lái)比較密碼及其子字符串。此外,在執(zhí)行比較時(shí),將看起來(lái)類似的字符視為相同字符。例如,將字母“l(fā)”和“e”視為與數(shù)字“1”和“3”相同的字符。
如果只有一個(gè)大寫(xiě)字符,請(qǐng)確保它不是密碼的第一個(gè)字符。
確保密碼的最后兩個(gè)字符是字母字符。
不允許這樣的密碼:其中的所有符號(hào)都是通過(guò)鍵盤(pán)最上面的一排鍵輸入的。
相關(guān)文章
VB.NET實(shí)現(xiàn)驗(yàn)證信用卡卡號(hào)
這篇文章主要介紹了VB.NET實(shí)現(xiàn)驗(yàn)證信用卡卡號(hào)是否正確的代碼,主要根據(jù)luhn算法來(lái)驗(yàn)證,有需要的小伙伴可以參考下。2015-05-05VB.NET中使用種子填充算法實(shí)現(xiàn)給圖片著色的例子
這篇文章主要介紹了VB.NET中使用種子填充算法實(shí)現(xiàn)給圖片著色的例子,在開(kāi)發(fā)一個(gè)畫(huà)圖工具時(shí)遇到的問(wèn)題,需要的朋友可以參考下2014-07-07vb.net操作注冊(cè)表的方法分析【增加,修改,刪除,查詢】
這篇文章主要介紹了vb.net操作注冊(cè)表的方法,結(jié)合實(shí)例形式分析了vb.net針對(duì)注冊(cè)表的增加,修改,刪除及查詢操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03VB實(shí)現(xiàn)的遞歸復(fù)制文件和搜索文件的代碼分享
這篇文章主要介紹了VB實(shí)現(xiàn)的遞歸復(fù)制文件和搜索文件的代碼分享,代碼寫(xiě)的比較簡(jiǎn)單,容易看懂,需要的朋友可以參考下2014-07-07