연습: 암호의 복합성 검사(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 웹 애플리케이션 보안을 참조하세요.

추가 복잡성 검사를 추가하여 ValidatePassword 함수의 정확도를 향상시킬 수 있습니다.

  • 암호와 해당 하위 문자열을 사용자의 이름, 사용자 식별자, 애플리케이션 정의 사전과 비교합니다. 또한 비교를 수행할 때 시각적으로 유사한 문자를 동등한 문자로 처리합니다. 예를 들어 글자 "l" 및 "e"를 숫자 "1" 및 "3"과 동일하게 처리합니다.

  • 대문자 하나만 있는 경우 암호의 첫 번째 문자가 아닌지 확인합니다.

  • 암호의 마지막 두 문자가 글자 문자인지 확인합니다.

  • 키보드의 맨 윗줄에서 모든 기호를 입력하는 암호는 허용하지 않습니다.

참고 항목