CkJava Requires Chilkat v11.0.0+
CkJava
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 CkJava Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkCertStore store = new CkCertStore();
success = store.OpenCurrentUserStore(true);
if (success != true) {
System.out.println(store.lastErrorText());
return;
}
CkJsonObject search = new CkJsonObject();
search.UpdateString("thumbprint","YOUR_CERTIFICATE_THUMBPRINT");
CkCert cert = new CkCert();
success = store.FindCert(search,cert);
if (success != true) {
System.out.println(store.lastErrorText());
return;
}
if (cert.HasPrivateKey() != true) {
System.out.println("The selected certificate has no accessible private key.");
return;
}
CkPrivateKey privateKey = new CkPrivateKey();
success = cert.GetPrivateKey(privateKey);
if (success != true) {
System.out.println(cert.lastErrorText());
return;
}
CkDkim signer = new CkDkim();
signer.put_DkimDomain("example.com");
signer.put_DkimSelector("mail2026");
signer.put_DkimAlg("rsa-sha256");
success = signer.SetDkimPrivateKey(privateKey);
if (success != true) {
System.out.println(signer.lastErrorText());
return;
}
CkBinData message = new CkBinData();
success = message.LoadFile("message.eml");
if (success != true) {
System.out.println("Could not read message.eml.");
return;
}
success = signer.DkimSign(message);
if (success != true) {
System.out.println(signer.lastErrorText());
return;
}
success = message.WriteFile("signed.eml");
if (success != true) {
System.out.println("Could not write signed.eml.");
return;
}
System.out.println("Saved signed.eml.");
}
}