Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.

local store = chilkat.CertStore.new()
local success = store:open_current_user_store(true)
if not success then
    print(store.last_error_text)
    return
end

local search = chilkat.JsonObject.new()
search:update_string("thumbprint", "YOUR_CERTIFICATE_THUMBPRINT")
local cert = chilkat.Cert.new()
success = store:find_cert(search, cert)
if not success then
    print(store.last_error_text)
    return
end

if not cert:has_private_key() then
    print("The selected certificate has no accessible private key.")
    return
end

local privateKey = chilkat.PrivateKey.new()
success = cert:get_private_key(privateKey)
if not success then
    print(cert.last_error_text)
    return
end

local signer = chilkat.Dkim.new()
signer.dkim_domain = "example.com"
signer.dkim_selector = "mail2026"
signer.dkim_alg = "rsa-sha256"
success = signer:set_dkim_private_key(privateKey)
if not success then
    print(signer.last_error_text)
    return
end

local message = chilkat.BinData.new()
success = message:load_file("message.eml")
if not success then
    print("Could not read message.eml.")
    return
end

success = signer:dkim_sign(message)
if not success then
    print(signer.last_error_text)
    return
end

success = message:write_file("signed.eml")
if not success then
    print("Could not write signed.eml.")
    return
end

print("Saved signed.eml.")