Поделиться через


Шифрование сообщения в CAPICOM

[CAPICOM — это 32-разрядный компонент, доступный для использования в следующих операционных системах: Windows Server 2008, Windows Vista и Windows XP. Вместо этого используйте платформа .NET Framework для реализации функций безопасности. Дополнительные сведения см. в разделе "Альтернативы использованию CAPICOM".]

Эта подпрограмма принимает зашифрованную строку, строку пароля, используемую для создания ключа шифрования, а также имя файла, в котором будет записано зашифрованное сообщение. Все параметры передаются в подпрограмму по значениям. Для расшифровки сообщения необходимо использовать ту же строку пароля. Если пароль потерян, текст не может быть расшифрован. Конфиденциальность сообщения теряется, если непреднамеренный получатель получает доступ к паролю.

Примечание

CAPICOM не поддерживает тип контента PKCS #7 EncryptedData, но использует нестандартную структуру ASN для EncryptedData. В результате только CAPICOM может расшифровать объект CAPICOM EncryptedData.

 

При любой ошибке CAPICOM возвращается отрицательное десятичное значение свойства Err.Number . Дополнительные сведения см. в разделе CAPICOM_ERROR_CODE. Сведения о положительных десятичных значениях Err.Number см. в разделе Winerror.h.

Sub EncryptMessage(ByVal TobeEncrypted As String, ByVal hidden _
    As String, ByVal filename As String)

    On Error GoTo ErrorHandler

    ' Declare and initialize an EncryptedData object.
    ' Algorithm.Name and KeyLength do not need to be set.

    Dim message As New EncryptedData
    message.Content = Tobeencrypted
    message.SetSecret(hidden)
    ' Optionally, the encryption algorithm and key length can be set.
    ' If these properties are not set, the default algorithm and key 
    ' length are used.
    ' Information about the algorithm and key length is saved with 
    ' the encrypted string and the individual decrypting the message
    ' does not need to set these properties.

    message.Algorithm.Name = CAPICOM_ENCRYPTION_ALGORITHM_DES

    ' Declare the string that will hold the encrypted message.

    Dim encryptedmessage As String

    ' Encrypt the message storing the result in the encryptedmessage
    ' string.
    encryptedmessage = message.Encrypt

    ' Optionally, check the length of the encrypted string to 
    ' make sure that the encrypt method worked. 

    If Len(encryptedmessage) < 1 Then
        MsgBox("no message encrypted. ")
    Else
        MsgBox(" Message is " & Len(encryptedmessage) & " characters")

        ' Open an output file and write the encrypted message to the
        ' file. The file is not opened if there is no message
        ' to write.
    Open filename For Output As #1
    Write #1, encryptedmessage
    Close #1
        MsgBox("Encrypted message written to file ")
    End If

    ' Release the EncryptedData object.
    message = 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