Расшифровка сообщения в CAPICOM
[CAPICOM — это 32-разрядный компонент, доступный для использования в следующих операционных системах: Windows Server 2008, Windows Vista и Windows XP. Вместо этого используйте платформа .NET Framework для реализации функций безопасности. Дополнительные сведения см. в разделе Альтернативы использованию CAPICOM.]
Эта подпрограмма принимает строку пароля, используемую для создания ключа шифрования сеанса, и имя файла, из которого будет считываться зашифрованное сообщение. Все параметры передаются в подпрограмму по значениям.
Примечание
CAPICOM не поддерживает тип контента PKCS #7 EncryptedData, но использует для EncryptedData нестандартную структуру ASN. Таким образом, только CAPICOM может расшифровать объект CAPICOM EncryptedData.
Если расшифровка завершается сбоем, проверяется значение Err.Number , чтобы определить, была ли ошибка вызвана использованием пароля, который не совпадал с паролем, используемым для шифрования сообщения. В этом случае возвращается ошибка NTE_BAD_DATA.
При любой ошибке CAPICOM возвращается отрицательное десятичное значение Err.Number . Дополнительные сведения см. в разделе CAPICOM_ERROR_CODE. Сведения о положительных десятичных значениях Err.Number см. в разделе Winerror.h.
Sub DecryptMessage(ByVal hidden As String, ByVal filename As String)
On Error goto ErrorHandler
'Declare an EncryptedData object
Dim message As New EncryptedData
'Declare a string variable to hold the encrypted message.
Dim encrypted As String
' Open an input file and read in the encrypted message
Open filename For Input As #1
Input #1, encrypted
Close #1
' If a message was read in (the length of the input string is
' greater than 0), set the password and decrypt the message.
If Len(encrypted) > 0 then
' Set the password used to generate the key
' used to decrypt the message. If the password
' not the same as the password used when the message
' was encrypted, decryption will fail.
' The algorithm name and key length set in the encrypting
' process are automatically used to decrypt the message.
message.SetSecret hidden
message.Decrypt encrypted
' Display the decrypted message.
MsgBox message.Content
else
msgbox "No encrypted message was read in.
endif
' Release the EncryptedData object and exit the subroutine.
Set message = Nothing
Exit Sub
ErrorHandler:
If Err.Number > 0 Then
MsgBox "Visual Basic error found:" & Err.Description
Else
' Check for a bad password error.
If Err.Number = -2146893819 Then
MsgBox "Error. The password may not be correct."
Else
MsgBox "CAPICOM error found : " & Err.Number
End If
End If
End Sub