Zig
Zig
JWE with DEFLATE Compression
See more JSON Web Encryption (JWE) Examples
Demonstrates how to DEFLATE ("zip") compress the JWE payload prior to encryption.Note: This example requires Chilkat v9.5.0.66 or greater.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Note: This example requires Chilkat v9.5.0.66 or greater.
// Create some plaintext to be encrypted.
// This example will demonstrate with and without DEFLATE (zip) compression.
const sb_plain_text = try chilkat.StringBuilder.init();
defer sb_plain_text.deinit();
const b_cr_lf = true;
const line = "Live long and prosper.";
sb_plain_text.appendLine(line, b_cr_lf) catch {};
sb_plain_text.appendLine(line, b_cr_lf) catch {};
sb_plain_text.appendLine(line, b_cr_lf) catch {};
sb_plain_text.appendLine(line, b_cr_lf) catch {};
// The text to be encrypted:
std.debug.print("{s}\n", .{try sb_plain_text.getAsString(alloc)});
const jwe = try chilkat.Jwe.init();
defer jwe.deinit();
// Build the JWE Protected Header: {"alg":"A128KW","enc":"A128CBC-HS256","zip":"DEF"}
// The "zip":"DEF" parameter indicates that the plaintext payload should
// be compressed prior to encryption.
const jwe_prot_hdr = try chilkat.JsonObject.init();
defer jwe_prot_hdr.deinit();
jwe_prot_hdr.appendString("alg", "A128KW") catch {};
jwe_prot_hdr.appendString("enc", "A128CBC-HS256") catch {};
jwe_prot_hdr.appendString("zip", "DEF") catch {};
jwe.setProtectedHeader(jwe_prot_hdr) catch {};
// Set the AES key wrap key:
const aes_wrapping_key = "GawgguFyGrWKav7AX4VKUg";
jwe.setWrappingKey(0, aes_wrapping_key, "base64url") catch {};
// Encrypt and return the JWE in sbJweCompressed:
const sb_jwe_compressed = try chilkat.StringBuilder.init();
defer sb_jwe_compressed.deinit();
jwe.encryptSb(sb_plain_text, "utf-8", sb_jwe_compressed) catch {
std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
return;
};
// Show the compressed JWE:
std.debug.print("{s}\n", .{try sb_jwe_compressed.getAsString(alloc)});
std.debug.print("size of compressed JWE: {d}\n", .{sb_jwe_compressed.getLength()});
// Now create a JWE without compression.
jwe_prot_hdr.delete("zip") catch {};
// Make sure to update the shared protected header:
jwe.setProtectedHeader(jwe_prot_hdr) catch {};
const sb_jwe_uncompressed = try chilkat.StringBuilder.init();
defer sb_jwe_uncompressed.deinit();
jwe.encryptSb(sb_plain_text, "utf-8", sb_jwe_uncompressed) catch {
std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
return;
};
// Show the uncompressed JWE:
std.debug.print("{s}\n", .{try sb_jwe_uncompressed.getAsString(alloc)});
std.debug.print("size of uncompressed JWE: {d}\n", .{sb_jwe_uncompressed.getLength()});
// Decrypting is the same whether compression is used or not.
// The "zip" header in the JWE indicates that the payload should be
// automatically decompressed (inflated) after decrypting.
const jwe2 = try chilkat.Jwe.init();
defer jwe2.deinit();
jwe2.loadJweSb(sb_jwe_compressed) catch {
std.debug.print("{s}\n", .{try jwe2.getLastErrorText(alloc)});
return;
};
// Set the AES wrap key.
jwe2.setWrappingKey(0, aes_wrapping_key, "base64url") catch {};
// Decrypt (also automatically decompresses).
const sb_original_text = try chilkat.StringBuilder.init();
defer sb_original_text.deinit();
jwe2.decryptSb(0, "utf-8", sb_original_text) catch {
std.debug.print("{s}\n", .{try jwe2.getLastErrorText(alloc)});
return;
};
std.debug.print("original text from compressed JWE: \n", .{});
std.debug.print("{s}\n", .{try sb_original_text.getAsString(alloc)});
// -----------------------------------------------------------
// Do the same with the uncompressed JWE
jwe2.loadJweSb(sb_jwe_uncompressed) catch {
std.debug.print("{s}\n", .{try jwe2.getLastErrorText(alloc)});
return;
};
// Set the AES wrap key.
jwe2.setWrappingKey(0, aes_wrapping_key, "base64url") catch {};
// Decrypt.
sb_original_text.clear();
jwe2.decryptSb(0, "utf-8", sb_original_text) catch {
std.debug.print("{s}\n", .{try jwe2.getLastErrorText(alloc)});
return;
};
std.debug.print("original text from uncompressed JWE: \n", .{});
std.debug.print("{s}\n", .{try sb_original_text.getAsString(alloc)});
// ------------------------------------------------
// The output of this example is:
// (Note: Your output data will be different because the content encryption key is randomly generated.)
// eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiemlwIjoiREVGIn0.xuW-pEAIdEUFnk10m8ocursvktO8Of9ByCCAt6LgKkkOtCWCUn1kQw.zpGj-9WVni3cQxyOuZbcGA.0hzP1myua3oYpUHwCIY_3edBUREbUpLaX6wYuJduOdI.Ppc6aEO3y3B8BJ1FKMPjlA
// size of compressed JWE: 212
// eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0.N4KeyC7nnSFkieJOyE24_zKeuV_m7v5UKoJb1TgV4Yc_r2RzUPNvyA.6AEdyXSCKx-iMmUJyypSLg.QpixfyrwhGpmwUDp623viik4smPav7vwPLiC2r-V-jwnSfEH3mxWu6DbrIz3mixaqATwynmEBzVPxvS9jTXpSAGCnniib4_0WoPl3r_wF5tlsKOEe--jpNso-DKd1Tp8jJxj3JkFWt3IRnUUKGj17g.sBfDwFc5fzpaI-UW8-SW4g
// size of uncompressed JWE: 303
// original text from compressed JWE:
// Live long and prosper.
// Live long and prosper.
// Live long and prosper.
// Live long and prosper.
//
// original text from uncompressed JWE:
// Live long and prosper.
// Live long and prosper.
// Live long and prosper.
// Live long and prosper.
//
}