Unicode C++ Requires Chilkat v11.8.0+
Unicode C++
Decrypt an S/MIME email and verify its signature
See more Email Object Examples
Register the recipient’s certificate and private key (from a PEM file) before opening signed-encrypted.eml. This workflow expects both encryption and a signature, so it checks both layers before reading the message. Cryptographic signature validity is one input to a signer-trust decision.
Chilkat Unicode C++ Downloads
#include <CkEmailW.h>
#include <CkPemW.h>
#include <CkCertW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkEmailW email;
// The recipient's certificate and private key are in a PEM file. The 2nd argument of LoadPemFile
// is the password of the private key.
CkPemW pem;
success = pem.LoadPemFile(L"recipient.pem",L"RECIPIENT_PASSWORD");
if (success != true) {
wprintf(L"%s\n",pem.lastErrorText());
return;
}
success = email.AddPemSource(pem);
if (success != true) {
wprintf(L"%s\n",email.lastErrorText());
return;
}
success = email.LoadEml(L"signed-encrypted.eml");
if (success != true) {
wprintf(L"%s\n",email.lastErrorText());
return;
}
if ((email.get_ReceivedEncrypted() != true) || (email.get_Decrypted() != true)) {
wprintf(L"Expected a successfully decrypted S/MIME envelope.\n");
return;
}
if ((email.get_ReceivedSigned() != true) || (email.get_SignaturesValid() != true)) {
wprintf(L"Expected a valid signature inside the envelope.\n");
return;
}
// This message has one signer. Examine its certificate after verification.
CkCertW signer;
success = email.LastSignerCert(0,signer);
if (success != true) {
wprintf(L"%s\n",email.lastErrorText());
return;
}
wprintf(L"Signer certificate: %s\n",signer.subjectDN());
wprintf(L"Recovered subject: %s\n",email.subject());
wprintf(L"Recovered attachments: %d\n",email.get_NumAttachments());
}