华南俳烁实业有限公司

考試首頁 | 考試用書 | 培訓課程 | 模擬考場 | 考試論壇  
  當前位置:編程開發(fā) > DotNET > VB.Net > 文章內容
  

vb.net驗證密碼是否復雜的方法

 [ 2017年6月29日 ] 【

  可在安全的系統(tǒng)中使用密碼來向用戶授權。但是,密碼必須難于被未授權用戶猜測出來。攻擊者可以使用一種“字典攻擊”程序,該程序將遍歷一本字典(或不同語言的多本字典)中的所有單詞,并測試是否有任何單詞就是用戶的密碼。諸如“Yankees”或“Mustang”等弱密碼可被很快猜測出來。諸如“?You'L1N3vaFiNdMeyeP@sSWerd!”等強密碼被猜測出來的可能性要小很多。密碼保護系統(tǒng)應確保用戶選擇強密碼。

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

  示例

  '''

Determines if a password is sufficiently complex.

  ''' Password to validate

  ''' Minimum number of password characters.

  ''' Minimum number of uppercase characters.

  ''' Minimum number of lowercase characters.

  ''' Minimum number of numeric characters.

  ''' Minimum number of special characters.

  ''' True if the password is sufficiently complex.

  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 命名空間的成員。如果沒有在代碼中完全限定成員名稱,請?zhí)砑?Imports 語句。有關更多信息,請參見 Imports 語句(.NET 命名空間和類型)。

  安全性

  如果要在網(wǎng)絡中轉移密碼,您需要使用安全的方法來傳輸數(shù)據(jù)。有關更多信息,請參見 ASP.NET Web 應用程序安全性。

  通過添加額外的復雜性檢查,您可以改進 ValidatePassword 函數(shù)的準確性:

  依據(jù)用戶的名稱、用戶標識符和應用程序定義的字典來比較密碼及其子字符串。此外,在執(zhí)行比較時,將看起來類似的字符視為相同字符。例如,將字母“l(fā)”和“e”視為與數(shù)字“1”和“3”相同的字符。

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

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

  不允許這樣的密碼:其中的所有符號都是通過鍵盤最上面的一排鍵輸入的。

本文糾錯】【告訴好友】【打印此文】【返回頂部
將考試網(wǎng)添加到收藏夾 | 每次上網(wǎng)自動訪問考試網(wǎng) | 復制本頁地址,傳給QQ/MSN上的好友 | 申請鏈接 | 意見留言 TOP
關于本站  網(wǎng)站聲明  廣告服務  聯(lián)系方式  站內導航  考試論壇
Copyright © 2007-2013 中華考試網(wǎng)(Examw.com) All Rights Reserved
阿拉尔市| 晋江市| 泰兴市| 龙门县| 雅江县| 阿图什市| 昔阳县| 铜川市| 偃师市| 张家港市| 达拉特旗| 航空| 临武县| 乌兰察布市| 山西省| 三都| 镇江市| 巨野县| 西乡县| 桃园市| 竹北市| 全州县| 益阳市| 宜君县| 改则县| 湟源县| 府谷县| 梁河县| 盐边县| 德庆县| 仙游县| 怀安县| 肇源县| 阿勒泰市| 大新县| 青冈县| 乐都县| 英山县| 万山特区| 乌苏市| 邢台市|