Decrypt method decrypts current encrypted email to new plain POPMSG object.
[Syntax] C++: HRESULT Decrypt( IPOPMSG* *pVal ) Visual Basic: Decrypt() As ANPOPLib.POPMSG C#: ANPOPLib.POPMSGClass Decrypt()
Return Value
If this method decrypts email successfully, return value is a new decrypted POPMSG object, otherwise an exception is thrown out.
Remarks
This method will fail if the certificate for the associated private key is not in either the local computer MY store or the current user MY store.
If current email is signed and encrypted, decryption should be the first step.
ANPOP uses CAPICOM object(a free component of Microsoft) to verify signature and decrypt email, CAPICOM is included in ANPOP installation package. More detail about CAPICOM, please click here.
Usage Example
[Visual Basic]
'Note: current project should add the reference for ANPOP and CAPICOM
Sub DecryptAndVerifyMessage( ByRef oMsg As ANPOPLib.POPMSG )
Dim oSignedData As CAPICOM.SignedData
On Error GoTo ErrorHandler
If oMsg.IsEncrypted() > 0 Then
Set oMsg = oMsg.Decrypt() 'decrypt message
End If
If oMsg.IsSigned() > 0 Then
Set SignedData = oMsg.VerifySignature(0,1)
SignedData.Signers(1).Certificate.Display 'display certificate
'adding this certificate to LOCAL_CURRENT_USER "my" store is recommended
End If
Exit Sub
ErrorHandler:
MsgBox Err.Number & ": " & Err.Description, , "Error:"
End Sub
[C#]
'Note: current project should add the reference for ANPOP and CAPICOM
using System;
using ANPOPLib;
using CAPICOM;
void DecryptAndVerifyMessage( ref POPMSGClass oMsg )
{
try
{
if( oMsg.IsEncrypted() > 0 )
oMsg.RawContent = oMsg.Decrypt().RawContent; //decrypt message
CAPICOM.ISignedData oSignedData = null;
if( oMsg.IsSigned() > 0 )
{
oSignedData = (CAPICOM.ISignedData)oMsg.VerifySignature(0, 1);
//display certificate
CAPICOM.ISigner oSigner = (CAPICOM.ISigner)oSignedData.Signers[1];
oSigner.Certificate.Display();
//adding this certificate to LOCAL_CURRENT_USER "my" store is recommended
}
}
catch( Exception e )
{
Console.WriteLine( e.Message );
}
}
[Visual C++]
#include "stdafx.h"
#include <comdef.h>
#include <iostream>
#import "C:\Program Files\AdminSystem.NET\ANPOP\CAPICOM.dll" \
no_namespace
#import "C:\Program Files\AdminSystem.NET\ANPOP\ANPOP.dll" \
rename_namespace("ANPOPLib")
using namespace ANPOPLib;
using namespace std;
void DecryptAndVerifyMessage( IPOPMSGPtr& oMsg )
{
try
{
if( oMsg->IsEncrypted() > 0 )
oMsg = oMsg->Decrypt(); //decrypt message
ISignedDataPtr oSignedData = NULL;
if( oMsg->IsSigned() > 0 )
{
oSignedData = oMsg->VerifySignature(0, 1);
//display certificate
ISignerPtr oSigner = oSignedData->Signers->Item[1];
oSigner->Certificate->Display();
//adding this certificate to LOCAL_CURRENT_USER "my" store is recommended
}
}
catch( _com_error &e )
{
cout << (const char*)e.Description() << endl;
}
}
See Also
Decrypt Method
VerifySignature Method
IsSigned Method
2001-2007 © Copyright AdminSystem Software Limited. All rights reserved.