程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB.NET >> vb.net驗證密碼能否復雜的辦法

vb.net驗證密碼能否復雜的辦法

編輯:VB.NET

vb.net驗證密碼能否復雜的辦法。本站提示廣大學習愛好者:(vb.net驗證密碼能否復雜的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是vb.net驗證密碼能否復雜的辦法正文


可在平安的零碎中運用密碼來向用戶受權。但是,密碼必需難於被未受權用戶猜想出來。攻擊者可以運用一種“字典攻擊”順序,該順序將遍歷一本字典(或不同言語的多本字典)中的一切單詞,並測試能否有任何單詞就是用戶的密碼。諸如“Yankees”或“Mustang”等弱密碼可被很快猜想出來。諸如“?You'L1N3vaFiNdMeyeP@sSWerd!”等強密碼被猜想出來的能夠性要小很多。密碼維護零碎應確保用戶選擇強密碼。

強密碼很復雜(包括大寫、小寫、數字和特殊字符的組合),並且不是單詞。此示例演示如何驗證復雜性。

示例 


''' <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
編譯代碼
經過傳遞包括該密碼的字符串來調用此辦法。

此示例需求:

訪問 System.Text.RegularExpressions 命名空間的成員。假如沒有在代碼中完全限定成員稱號,請添加 Imports 語句。有關更多信息,請參見 Imports 語句(.NET 命名空間和類型)。

平安性
假如要在網絡直達移密碼,您需求運用平安的辦法來傳輸數據。有關更多信息,請參見 ASP.NET Web 使用順序平安性。

經過添加額定的復雜性反省,您可以改良 ValidatePassword 函數的精確性:

根據用戶的稱號、用戶標識符和使用順序定義的字典來比擬密碼及其子字符串。此外,在執行比擬時,將看起來相似的字符視為相反字符。例如,將字母“l”和“e”視為與數字“1”和“3”相反的字符。

假如只要一個大寫字符,請確保它不是密碼的第一個字符。

確保密碼的最後兩個字符是字母字符。

不允許這樣的密碼:其中的一切符號都是經過鍵盤最下面的一排鍵輸出的。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved