Sample code for 30+ languages & platforms
DataFlex

Decrypt S/MIME using Added Credential Sources

See more MIME Examples

Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.

The recipient's certificate and private key are supplied from a PFX added with AddPfxSourceFile, and Decrypt automatically selects the matching private key based on the CMS recipient information.

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.

Background. Decrypt requires access to the recipient's private key. Chilkat matches the encrypted message's recipient identifiers against the certificates and keys made available through sources such as AddPfxSourceFile, AddDecryptCert, or a certificate vault.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMime
    String sPfxPassword
    String sBody
    String sTemp1
    Boolean bTemp1

    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

    //  Load the recipient's certificate and private key (from a PFX) so decryption can find them.  The
    //  PFX password should come from a secure source rather than being hard-coded.
    Move "myPfxPassword" To sPfxPassword
    Get ComAddPfxSourceFile Of hoMime "qa_data/recipient.pfx" sPfxPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Decrypt.  Chilkat identifies the correct recipient from the CMS recipient info and uses the
    //  matching private key from the added sources.
    Get ComDecrypt Of hoMime To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetBodyDecoded Of hoMime To sBody
    Get ComLastMethodSuccess Of hoMime To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sBody


End_Procedure