Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMime
    Variant vCertInfo
    Handle hoCertInfo
    String sJson
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End
    Get ComLoadMimeFile Of hoMime "qa_data/encrypted.eml" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Get Create (RefClass(cComChilkatJsonObject)) To hoCertInfo
    If (Not(IsComObjectCreated(hoCertInfo))) Begin
        Send CreateComObject of hoCertInfo
    End
    Get pvComObject of hoCertInfo to vCertInfo
    Get ComGetDecryptCertInfo Of hoMime vCertInfo To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Set ComEmitCompact Of hoCertInfo To False
    Get ComEmit Of hoCertInfo To sJson
    Showln sJson
    //  JSON parsing code for this result can be generated at Chilkat's online tool:
    //  https://tools.chilkat.io/jsonParse


End_Procedure