次の方法で共有


CAPICOM でのメッセージの暗号化解除

[CAPICOM は、次のオペレーティング システムで使用できる 32 ビットのみのコンポーネントです: Windows Server 2008、Windows Vista、および Windows XP。 代わりに、.NET Frameworkを使用してセキュリティ機能を実装します。 詳細については、「 CAPICOM を使用する代替手段」を参照してください。

このサブルーチンは、セッション暗号化キーの生成に使用されるパスワード文字列と、暗号化されたメッセージの読み取り元となるファイルの名前を受け取ります。 すべてのパラメーターは、値によってサブルーチンに渡されます。

Note

CAPICOM は PKCS #7 EncryptedData コンテンツ タイプをサポートしていませんが、EncryptedData には標準以外の ASN 構造体を使用します。 そのため、CAPICOM EncryptedData オブジェクトの暗号化を解除できるのは CAPICOM だけです。

 

暗号化解除に失敗した場合は、 Err.Number 値がチェックされ、メッセージの暗号化に使用されたパスワードと一致しないパスワードの使用によってエラーが発生したかどうかを確認します。 この場合、NTE_BAD_DATA エラーが返されます。

CAPICOM エラーでは、 Err.Number の負の 10 進値が返されます。 詳細については、「 CAPICOM_ERROR_CODE」を参照してください。 Err.Number の正の 10 進値については、「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