Sample code for 30+ languages & platforms
Zig

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

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    // secret key from your ClickBank account
    const secret_key = "MY_SECRET_KEY";

    const json_str = "{\"notification\":\"BASE64_NOTIFICATION\",\"iv\":\"BASE64_IV\"}";

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    try json.load(json_str);

    // Get the encrypted notification (binary) and IV from the JSON.
    const bd_notif = try chilkat.BinData.init();
    defer bd_notif.deinit();
    const bd_iv = try chilkat.BinData.init();
    defer bd_iv.deinit();

    try bd_notif.appendEncoded(try json.stringOf(alloc, "notification"), "base64");
    try bd_iv.appendEncoded(try json.stringOf(alloc, "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.

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    // Because we're using the hex string as the actual AES key, it matters whether the hex is uppercase or lowercase.
    // We want lowercase.
    crypt.setEncodingMode("hex_lower");
    crypt.setHashAlgorithm("sha1");
    const hex_sha1 = try crypt.hashStringENC(alloc, secret_key);
    std.debug.print("{s}\n", .{hex_sha1});

    // Treat the hex string as binary data for the AES key..
    const bd_key = try chilkat.BinData.init();
    defer bd_key.deinit();
    bd_key.appendString(hex_sha1, "us-ascii") catch {};
    bd_key.removeChunk(32, 8) catch {};

    crypt.setKeyLength(256);
    crypt.setCryptAlgorithm("aes");
    crypt.setCipherMode("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..
    crypt.setEncodedIV(try bd_iv.getEncoded(alloc, "base64"), "base64");
    crypt.setEncodedKey(try bd_key.getEncoded(alloc, "base64"), "base64");

    crypt.decryptBd(bd_notif) catch {};
    std.debug.print("Decrypted: {s}\n", .{try bd_notif.getString(alloc, "utf-8")});
}