Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.8.0+

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
    Variant vPem
    Handle hoPem
    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

    //  The certificate and its private key are in a PEM file.  The 2nd argument of LoadPemFile is the
    //  password of the private key.  (Pass an empty string if the private key is not encrypted.)
    Get Create (RefClass(cComChilkatPem)) To hoPem
    If (Not(IsComObjectCreated(hoPem))) Begin
        Send CreateComObject of hoPem
    End
    Get ComLoadPemFile Of hoPem "c:/aaworkarea/my.pem" "pemPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPem To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoPem to vPem
    Get ComAddPemSource Of hoCrypt vPem 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