Sample code for 30+ languages & platforms
PureBasic

Example: Mime.GetDecryptCertInfo method

Demonstrates the GetDecryptCertInfo method.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJsonObject.pb"
IncludeFile "CkMime.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; Load MIME that is has Content-Type like this:
    ; Content-Type: application/pkcs7-mime; smime-type="enveloped-data"; name="smime.p7m"; smime-type="enveloped-data"
    success = CkMime::ckLoadMimeFile(mime,"qa_data/mime/enveloped_data.eml")
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    ; Get information about the certificate that would be needed to decrypt.
    ; An enveloped-data can potentially be decrypted by multiple certificates if it was encrypted in a way that allows it,
    ; but in most cases, only a single certificate with associated private key (that of the message recipient) is possible.
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMime::ckGetDecryptCertInfo(mime,json)
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)

    ; Sample output:

    ; {
    ;   "recipientInfo": [
    ;     {
    ;       "serial": "****",
    ;       "issuerCN": "****"
    ;     }
    ;   ]
    ; }

    ; Get each certificate's information like this:
    serial.s
    issuerCN.s

    i.i = 0
    count.i = CkJsonObject::ckSizeOfArray(json,"recipientInfo")
    While i < count
        CkJsonObject::setCkI(json, i)
        serial = CkJsonObject::ckStringOf(json,"recipientInfo[i].serial")
        issuerCN = CkJsonObject::ckStringOf(json,"recipientInfo[i].issuerCN")
        i = i + 1
    Wend


    CkMime::ckDispose(mime)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure