Sample code for 30+ languages & platforms
PowerBuilder

Get Recipient Certificate Info from Encrypted S/MIME

See more MIME Examples

Demonstrates GetDecryptCertInfo, which examines an encrypted S/MIME entity and writes the recipient-certificate identifiers into a JsonObject. This reveals which certificate(s) the message was encrypted for, so the correct private key can be located before decrypting.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Tip: Code to parse the returned JSON can be generated with Chilkat's online tool at https://tools.chilkat.io/jsonParse.

Background. A CMS/PKCS #7 encrypted message carries recipient identifiers (such as issuer and serial number). Inspecting them lets an application determine, without decrypting, which key it needs.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mime
oleobject loo_CertInfo
string ls_Json

li_Success = 0

loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.Mime")
if li_rc < 0 then
    destroy loo_Mime
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Mime.LoadMimeFile("qa_data/encrypted.eml")
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

//  Examine the encrypted entity and write the recipient-certificate identifiers to a JsonObject.
//  This tells you which certificate(s) the message was encrypted for, so you can locate the right
//  private key before attempting to decrypt.
loo_CertInfo = create oleobject
li_rc = loo_CertInfo.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Mime.GetDecryptCertInfo(loo_CertInfo)
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    destroy loo_CertInfo
    return
end if

loo_CertInfo.EmitCompact = 0
ls_Json = loo_CertInfo.Emit()
Write-Debug ls_Json
//  JSON parsing code for this result can be generated at Chilkat's online tool:
//  https://tools.chilkat.io/jsonParse


destroy loo_Mime
destroy loo_CertInfo