次の方法で共有


チュートリアル: パスワードの複雑さの検証 (Visual Basic)

このメソッドでは、強力なパスワードの特性をチェックし、不合格になったチェックに関する情報で文字列パラメーターを更新します。

セキュリティで保護されたシステムでは、パスワードを使用してユーザーを承認できます。 ただし、パスワードは、承認されていないユーザーが推測するのは難しいものである必要があります。 攻撃者は、辞書 (またはさまざまな言語の複数の辞書) のすべての単語を反復処理する "辞書攻撃" プログラムを使用し、単語のいずれかがユーザーのパスワードとして機能するかどうかをテストする可能性があります。 "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

コードのコンパイル

対象のパスワードを含む文字列を渡して、このメソッドを呼び出します。

この例で必要な要素は次のとおりです。

セキュリティ

ネットワーク経由でパスワードを移動する場合は、セキュリティで保護された方法でデータを転送する必要があります。 詳細については、「ASP.NET Web Application Security (ASP.NET Web アプリケーションのセキュリティ)」をご覧ください。

複雑さのチェックをさらに追加することで、ValidatePassword 関数の精度を高めることができます。

  • パスワードとその部分文字列を、ユーザーの名前、ユーザー識別子、アプリケーション定義の辞書と比較します。 さらに、比較を行うときに、見た目が似ている文字は同等に扱います。 たとえば、文字 "l"、"e" は数字 "1"、"3" と同等に扱います。

  • 大文字が 1 つしかない場合は、パスワードの最初の文字ではないことを確認します。

  • パスワードの最後の 2 文字が文字であることを確認します。

  • すべての記号がキーボードの上部の行から入力されているパスワードは許可しないでください。

関連項目