Sample code for 30+ languages & platforms
DataFlex

Decrypt a .p7m File (using a PFX)

See more Encryption Examples

_LANGUAGE_ sample code showing how to decrypt a .p7m using a .p12 / .pfx certificate w/ private key.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCrypt
    String sPfxPath
    String sPfxPassword
    String sInPath
    String sOutPath
    String sTemp1

    Move False To iSuccess

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

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

    // Use a digital certificate and private key from a PFX file (.pfx or .p12).
    Move "myCertAndKey.pfx" To sPfxPath
    Move "test123" To sPfxPassword

    Get ComAddPfxSourceFile Of hoCrypt sPfxPath sPfxPassword To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCrypt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Set the  outPath to whatever is appropriate.
    // If you are decrypting a PDF, your output might be "out.pdf"...
    Move "smime.p7m" To sInPath
    Move "original.txt" To sOutPath

    // Indicate that we're doing public key decryption (i.e. PKCS7)
    Set ComCryptAlgorithm Of hoCrypt To "pki"

    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