Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
Sign an email with a key in the Windows Current User certificate store
See more DKIM Examples
Run on Windows as the account that owns the signing certificate. Copy its thumbprint from Current User > Personal and supply it as hexadecimal digits. The thumbprint selects a certificate. The DKIM signature itself uses RSA-SHA256.
The certificate needs an accessible, exportable RSA private key for GetPrivateKey. Publish its matching public key under the selector in DNS. DKIM recipients use that DNS key to verify the message. An X.509 chain is unnecessary for this step.
Chilkat Unicode C++ Downloads
#include <CkCertStoreW.h>
#include <CkJsonObjectW.h>
#include <CkCertW.h>
#include <CkPrivateKeyW.h>
#include <CkDkimW.h>
#include <CkBinDataW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkCertStoreW store;
success = store.OpenCurrentUserStore(true);
if (success != true) {
wprintf(L"%s\n",store.lastErrorText());
return;
}
CkJsonObjectW search;
search.UpdateString(L"thumbprint",L"YOUR_CERTIFICATE_THUMBPRINT");
CkCertW cert;
success = store.FindCert(search,cert);
if (success != true) {
wprintf(L"%s\n",store.lastErrorText());
return;
}
if (cert.HasPrivateKey() != true) {
wprintf(L"The selected certificate has no accessible private key.\n");
return;
}
CkPrivateKeyW privateKey;
success = cert.GetPrivateKey(privateKey);
if (success != true) {
wprintf(L"%s\n",cert.lastErrorText());
return;
}
CkDkimW signer;
signer.put_DkimDomain(L"example.com");
signer.put_DkimSelector(L"mail2026");
signer.put_DkimAlg(L"rsa-sha256");
success = signer.SetDkimPrivateKey(privateKey);
if (success != true) {
wprintf(L"%s\n",signer.lastErrorText());
return;
}
CkBinDataW message;
success = message.LoadFile(L"message.eml");
if (success != true) {
wprintf(L"Could not read message.eml.\n");
return;
}
success = signer.DkimSign(message);
if (success != true) {
wprintf(L"%s\n",signer.lastErrorText());
return;
}
success = message.WriteFile(L"signed.eml");
if (success != true) {
wprintf(L"Could not write signed.eml.\n");
return;
}
wprintf(L"Saved signed.eml.\n");
}