Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkCrypt2.pb"
IncludeFile "CkMime.pb"

Procedure ChilkatExample()

    success.i = 0

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

    mime.i = CkMime::ckCreate()
    If mime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMime::ckLoadMimeFile(mime,"c:/aaworkarea/EmailInBytes.txt")
    If success <> 1
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    success = CkMime::ckSaveBody(mime,"c:/aaworkarea/smime.p7m")
    If success <> 1
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    crypt.i = CkCrypt2::ckCreate()
    If crypt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCrypt2::ckAddPfxSourceFile(crypt,"c:/aaworkarea/my.pfx","pfxPassword")
    If success = 0
        Debug CkCrypt2::ckLastErrorText(crypt)
        CkMime::ckDispose(mime)
        CkCrypt2::ckDispose(crypt)
        ProcedureReturn
    EndIf

    ; Indicate the public-key (PKCS7) encryption/decryption should be used:
    CkCrypt2::setCkCryptAlgorithm(crypt, "pki")

    inPath.s = "c:/aaworkarea/smime.p7m"
    outPath.s = "c:/aaworkarea/decrypted.dat"

    success = CkCrypt2::ckCkDecryptFile(crypt,inPath,outPath)
    If success = 0
        Debug CkCrypt2::ckLastErrorText(crypt)
        CkMime::ckDispose(mime)
        CkCrypt2::ckDispose(crypt)
        ProcedureReturn
    EndIf

    Debug "Success."


    CkMime::ckDispose(mime)
    CkCrypt2::ckDispose(crypt)


    ProcedureReturn
EndProcedure