Unicode C
Unicode C
Set the Decryption Certificate for an Email
See more Email Object Examples
Demonstrates the Chilkat Email.SetDecryptCert method, which explicitly provides a certificate (with access to its private key) for decrypting a received encrypted email. Set the certificate before loading the encrypted message so Chilkat can decrypt it automatically. This example loads the certificate from a PFX, sets it, and then loads an encrypted email.
Background: Decrypting S/MIME email requires the recipient's private key. On Windows and macOS, Chilkat can find an installed certificate automatically, but when the key lives in a standalone PFX file you provide it explicitly with
SetDecryptCert. A PFX (PKCS#12) bundles the certificate and its private key in one password-protected file. If the certificate and key are in separate files, use SetDecryptCert2 instead.Chilkat Unicode C Downloads
#include <C_CkEmailW.h>
#include <C_CkCertW.h>
void ChilkatSample(void)
{
BOOL success;
HCkEmailW email;
HCkCertW cert;
success = FALSE;
// Demonstrates the SetDecryptCert method, which explicitly provides a certificate (with
// access to its private key) for decrypting a received encrypted email. Set the cert
// before loading the encrypted email so it can be decrypted automatically.
email = CkEmailW_Create();
// Load a certificate together with its private key from a PFX file.
cert = CkCertW_Create();
success = CkCertW_LoadPfxFile(cert,L"qa_data/certs/decryption.pfx",L"pfx_password");
if (success == FALSE) {
wprintf(L"%s\n",CkCertW_lastErrorText(cert));
CkEmailW_Dispose(email);
CkCertW_Dispose(cert);
return true;
}
// Provide the certificate to use for decryption.
success = CkEmailW_SetDecryptCert(email,cert);
if (success == FALSE) {
wprintf(L"%s\n",CkEmailW_lastErrorText(email));
CkEmailW_Dispose(email);
CkCertW_Dispose(cert);
return true;
}
// Load the encrypted email; Chilkat decrypts it using the certificate.
success = CkEmailW_LoadEml(email,L"qa_data/eml/encrypted.eml");
if (success == FALSE) {
wprintf(L"%s\n",CkEmailW_lastErrorText(email));
CkEmailW_Dispose(email);
CkCertW_Dispose(cert);
return true;
}
wprintf(L"ReceivedEncrypted = %d\n",CkEmailW_getReceivedEncrypted(email));
wprintf(L"Decrypted = %d\n",CkEmailW_getDecrypted(email));
// Note: Paths such as "qa_data/..." are relative local filesystem paths,
// relative to the current working directory of the running application.
CkEmailW_Dispose(email);
CkCertW_Dispose(cert);
}