문서 서명

[CAPICOM은 Windows Server 2008, Windows Vista 및 Windows XP 운영 체제에서 사용할 수 있는 32비트 전용 구성 요소입니다. 대신 .NET Framework 사용하여 보안 기능을 구현합니다. 자세한 내용은 CAPICOM 사용에 대한 대안을 참조하세요.]

서명의 표준 사용은 텍스트에 서명하고 서명된 텍스트를 파일에 저장하는 것입니다. 서명된 텍스트는 인터넷을 통해 보낼 수도 있습니다. 서명된 메시지는 PKCS #7 형식입니다.

이 예제에서는 분리된 콘텐츠에 대한 서명이 만들어집니다(콘텐츠가 서명에 포함되지 않은 경우). 서명의 받는 사람에게 정확히 서명된 텍스트의 복사본이 있는 경우 분리된 서명이 가장 자주 사용됩니다. 아래 예제에서는 원본 메시지와 분리된 서명이 별도의 파일에 기록됩니다.

CAPICOM 오류에서 Err.Number 의 음수 10진수 값이 반환됩니다. 자세한 내용은 CAPICOM_ERROR_CODE. Err.Number의 양의 10진수 값에 대한 자세한 내용은 Winerror.h를 참조하세요.

서명을 만들면 서명자의 프라이빗 키가 사용됩니다. 서명은 연결된 프라이빗 키가 있는 서명자의 인증서를 사용할 수 있는 경우에만 만들 수 있습니다. Sign 메서드의 이 예제에서는 서명자를 지정하지 않습니다. 서명자를 지정하지 않고 CAPICOM_MY_STORE 인증서에 연결된 프라이빗 키가 없는 경우 Sign 메서드가 실패합니다. CAPICOM_MY_STORE 하나의 인증서에 연결된 프라이빗 키가 있는 경우 해당 인증서와 해당 프라이빗 키가 서명을 만드는 데 사용됩니다. CAPICOM_MY_STORE 저장소에 둘 이상의 인증서에 연결된 프라이빗 키가 있는 경우 대화 상자가 나타나고 사용자가 서명을 만드는 데 사용할 인증서를 선택할 수 있습니다.

웹 기반 애플리케이션에서 Sign 메서드를 사용하는 경우 해당 서명자의 프라이빗 키를 사용하는 서명이 생성되기 전에 프롬프트가 항상 표시되고 사용자의 권한이 필요합니다.

Sub Signfile(ByVal InputFileName As String, _
    ByVal OutputFileName As String)
    
    On Error GoTo ErrorHandler
    Dim c As String
    Dim s As String
    Dim MyStore As New Store
    Dim Signobj As New SignedData
    Dim Signer As New Signer

    ' NOTE: the name 'Attribute' is not a unique name
    ' and must be preceded by 'CAPICOM.'
    Dim SigningTime As New CAPICOM.Attribute

    ' Open the MY store and retrieve the first certificate from the
    ' Store. The signing operation will only work if this
    ' certificate is valid and has access to the signer's private key.
    MyStore.Open CAPICOM_CURRENT_USER_STORE, "MY", _
        CAPICOM_STORE_OPEN_READ_ONLY
    Signer.Certificate = MyStore.Certificates.Item(1)

    ' Open the input file and read the content to be signed from
    ' the file.
    Open App.Path & "\" & InputFileName For Input As #1
    Input #1, c
    Close #1
    
    ' Set the content to be signed.
    Signobj.Content = c

    ' Save the time the data was signed as a signer attribute.
    SigningTime.Name = CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME
    SigningTime.Value = Now
    Signer.AuthenticatedAttributes.Add SigningTime

    ' Sign the content using the signer's private key.
    ' The 'True' parameter indicates that the content signed is not
    ' included in the signature string.
    s = Signobj.Sign(Signer, True)

    Open App.Path & "\" & OutputFileName For Output As #2
    Write #2, s
    Close #2

    MsgBox ("Signature done - Saved to file" & OutputFileName)
    Set Signobj = Nothing
    Set MyStore = Nothing
    Set Signer = Nothing
    Set SigningTime = Nothing

    Exit Sub

ErrorHandler:
    If Err.Number > 0 Then
        MsgBox ("Visual Basic error found:" & Err.Description)
    Else
        MsgBox ("CAPICOM error found : " & Err.Number)
    End If
End Sub

Store.Open

Signer.Certificate

attribute

SignedData