Sample code for 30+ languages & platforms
DataFlex

Extract PKCS7 from MIME and Decrypt

See more MIME Examples

Extracts the base64-encoded PKCS7 body of a MIME message to a file, and then decrypts using Chilkat Crypt2.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMime
    Handle hoCrypt
    String sInPath
    String sOutPath
    String sTemp1

    Move False To iSuccess

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End

    Get ComLoadMimeFile Of hoMime "c:/aaworkarea/EmailInBytes.txt" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSaveBody Of hoMime "c:/aaworkarea/smime.p7m" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End

    Get ComAddPfxSourceFile Of hoCrypt "c:/aaworkarea/my.pfx" "pfxPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCrypt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Indicate the public-key (PKCS7) encryption/decryption should be used:
    Set ComCryptAlgorithm Of hoCrypt To "pki"

    Move "c:/aaworkarea/smime.p7m" To sInPath
    Move "c:/aaworkarea/decrypted.dat" To sOutPath

    Get ComCkDecryptFile Of hoCrypt sInPath sOutPath To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCrypt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Success."


End_Procedure