Condividi tramite


Firma di un documento

[CAPICOM è un componente solo a 32 bit disponibile per l'uso nei sistemi operativi seguenti: Windows Server 2008, Windows Vista e Windows XP. Usare invece .NET Framework per implementare le funzionalità di sicurezza. Per altre informazioni, vedere Alternative all'uso di CAPICOM.

Un uso standard di una firma consiste nel firmare un testo e salvare il testo firmato in un file. Il testo firmato può anche essere inviato tramite Internet. Il messaggio firmato è in formato PKCS #7.

In questo esempio la firma viene creata per il contenuto scollegato (quando il contenuto non è incluso con la firma). Una firma scollegata viene spesso usata se il destinatario della firma ha una copia del testo con segno esatto. Nell'esempio seguente, il messaggio originale e la firma scollegata vengono scritti in file separati.

In qualsiasi errore CAPICOM viene restituito un valore decimale negativo di Err.Number . Per altre informazioni, vedere CAPICOM_ERROR_CODE. Per informazioni sui valori decimali positivi di Err.Number, vedere Winerror.h.

La creazione di una firma usa la chiave privata del firmatario. È possibile creare una firma solo se è disponibile il certificato del firmatario con una chiave privata associata. Questo esempio del metodo Sign non specifica un segno. Se un firmatario non è specificato e nessun certificato in CAPICOM_MY_STORE ha una chiave privata associata, il metodo Sign ha esito negativo. Se uno e un solo certificato in CAPICOM_MY_STORE ha una chiave privata associata, tale certificato e la relativa chiave privata vengono usati per creare la firma. Se più certificati nell'archivio CAPICOM_MY_STORE hanno una chiave privata associata, viene visualizzata una finestra di dialogo e l'utente può scegliere il certificato da usare per creare la firma.

Quando un'applicazione basata sul Web usa il metodo Sign , viene sempre visualizzata una richiesta e l'autorizzazione dell'utente è necessaria prima che venga creata una firma che usa la chiave privata del firmatario.

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

Attributo

SignedData