Skip to content

Latest commit

 

History

History
83 lines (60 loc) · 2.99 KB

decrypting-a-message-in-capicom.md

File metadata and controls

83 lines (60 loc) · 2.99 KB
description ms.assetid title ms.topic ms.date
This subroutine take a password string to be used to generate a session encryption key, and the name of a file from which an encrypted message will be read. All parameters are passed into the subroutine by values.
70f23d93-2bca-419b-9de7-e52ce4fb1350
Decrypting a Message in CAPICOM
article
05/31/2018

Decrypting a Message in CAPICOM

[CAPICOM is a 32-bit only component that is available for use in the following operating systems: Windows Server 2008, Windows Vista, and Windows XP. Instead, use the .NET Framework to implement security features. For more information, see Alternatives to Using CAPICOM.]

This subroutine take a password string to be used to generate a session encryption key, and the name of a file from which an encrypted message will be read. All parameters are passed into the subroutine by values.

Note

CAPICOM does not support the PKCS #7 EncryptedData content type but uses a nonstandard ASN structure for EncryptedData. Therefore, only CAPICOM can decrypt a CAPICOM EncryptedData object.

 

If decryption fails, the Err.Number value is checked to determine whether the error was caused by the use of a password that did not match the password used to encrypt the message. In this case, the NTE_BAD_DATA error is returned.

On any CAPICOM error, a negative decimal value of Err.Number is returned. For more information, see CAPICOM_ERROR_CODE. For information about positive decimal values of Err.Number, see 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