Sample code for 30+ languages & platforms
PureBasic

ClickBank Decrypt Instant Notification

See more ClickBank Examples

Demonstrates how to decrypt a ClickBank instant notification. See Instant Notification Service for more information and alternative code snippets.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkCrypt2.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; secret key from your ClickBank account
    secretKey.s = "MY_SECRET_KEY"

    jsonStr.s = "{" + Chr(34) + "notification" + Chr(34) + ":" + Chr(34) + "BASE64_NOTIFICATION" + Chr(34) + "," + Chr(34) + "iv" + Chr(34) + ":" + Chr(34) + "BASE64_IV" + Chr(34) + "}"

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

    success = CkJsonObject::ckLoad(json,jsonStr)

    ; Get the encrypted notification (binary) and IV from the JSON.
    bdNotif.i = CkBinData::ckCreate()
    If bdNotif.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    success = CkBinData::ckAppendEncoded(bdNotif,CkJsonObject::ckStringOf(json,"notification"),"base64")
    success = CkBinData::ckAppendEncoded(bdIv,CkJsonObject::ckStringOf(json,"iv"),"base64")

    ; Get an SHA1 digest (as a hex string) of the secret key.
    ; A SHA1 digest is 20 bytes.  Therefore the hex string is 40 chars.
    ; Treat each us-ascii char as a binary byte of the secret key.
    ; 256-bit AES needs a 256-bit key, which is 32-bytes.  Therefore
    ; use the 1st 32 us-ascii chars of the hex SHA1 as the AES secret key.

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

    ; Because we're using the hex string as the actual AES key, it matters whether the hex is uppercase or lowercase.
    ; We want lowercase.
    CkCrypt2::setCkEncodingMode(crypt, "hex_lower")
    CkCrypt2::setCkHashAlgorithm(crypt, "sha1")
    hexSha1.s = CkCrypt2::ckHashStringENC(crypt,secretKey)
    Debug hexSha1

    ; Treat the hex string as binary data for the AES key..
    bdKey.i = CkBinData::ckCreate()
    If bdKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkBinData::ckAppendString(bdKey,hexSha1,"us-ascii")
    CkBinData::ckRemoveChunk(bdKey,32,8)

    CkCrypt2::setCkKeyLength(crypt, 256)
    CkCrypt2::setCkCryptAlgorithm(crypt, "aes")
    CkCrypt2::setCkCipherMode(crypt, "cbc")

    ; We can use any encoding because were just getting the binary bytes in an encoding, and then setting from the same encoding.
    ; We'll just use base64..
    CkCrypt2::ckSetEncodedIV(crypt,CkBinData::ckGetEncoded(bdIv,"base64"),"base64")
    CkCrypt2::ckSetEncodedKey(crypt,CkBinData::ckGetEncoded(bdKey,"base64"),"base64")

    CkCrypt2::ckDecryptBd(crypt,bdNotif)
    Debug "Decrypted: " + CkBinData::ckGetString(bdNotif,"utf-8")


    CkJsonObject::ckDispose(json)
    CkBinData::ckDispose(bdNotif)
    CkBinData::ckDispose(bdIv)
    CkCrypt2::ckDispose(crypt)
    CkBinData::ckDispose(bdKey)


    ProcedureReturn
EndProcedure