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

Verify a signed message with a trusted public key file

See more DKIM Examples

Use the saved signed message and its matching public PEM file to test a signing configuration before publishing DNS. This verifier caches the key for the exact selector and domain. A different pair can trigger normal DNS lookup, so the example also checks which signature actually verified.

Load the key from a trusted source independent of the received message. A pass with a locally supplied key confirms the cryptographic match. Use the published-key check to test what recipients can retrieve.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Message
oleobject loo_Verifier
string ls_Domain
string ls_Selector
oleobject loo_Info

li_Success = 0

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

loo_Message = create oleobject
li_rc = loo_Message.ConnectToNewObject("Chilkat.BinData")
if li_rc < 0 then
    destroy loo_Message
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Message.LoadFile("signed.eml")
if li_Success <> 1 then
    Write-Debug "Could not read signed.eml."
    destroy loo_Message
    return
end if

loo_Verifier = create oleobject
li_rc = loo_Verifier.ConnectToNewObject("Chilkat.Dkim")

ls_Domain = "example.com"
ls_Selector = "mail2026"
li_Success = loo_Verifier.LoadPublicKeyFile(ls_Selector,ls_Domain,"dkim-public.pem")
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    destroy loo_Message
    destroy loo_Verifier
    return
end if

//  signed.eml from the signing example has our newly added signature at index zero.
if loo_Verifier.NumDkimSigs(loo_Message) = 0 then
    Write-Debug "No DKIM signature in signed.eml."
    destroy loo_Message
    destroy loo_Verifier
    return
end if

li_Success = loo_Verifier.DkimVerify(0,loo_Message)
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    Write-Debug loo_Verifier.VerifyInfo
    destroy loo_Message
    destroy loo_Verifier
    return
end if

loo_Info = create oleobject
li_rc = loo_Info.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Info.Load(loo_Verifier.VerifyInfo)
if (li_Success <> 1) OR (loo_Info.StringOfEquals("domain",ls_Domain,1) <> 1) OR (loo_Info.StringOfEquals("selector",ls_Selector,1) <> 1) then
    Write-Debug "The first signature is not from the selected domain and selector."
    destroy loo_Message
    destroy loo_Verifier
    destroy loo_Info
    return
end if

Write-Debug "Signature verified with the saved key for " + ls_Selector + "._domainkey." + ls_Domain


destroy loo_Message
destroy loo_Verifier
destroy loo_Info