Sample code for 30+ languages & platforms
Rust

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

Rust

// 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.
let sb_plain_text = chilkat::StringBuilder::new();
let b_cr_lf = true;
let line = "Live long and prosper.".to_string();
let _ = sb_plain_text.append_line(&line, b_cr_lf);
let _ = sb_plain_text.append_line(&line, b_cr_lf);
let _ = sb_plain_text.append_line(&line, b_cr_lf);
let _ = sb_plain_text.append_line(&line, b_cr_lf);

// The text to be encrypted:
println!("{}", sb_plain_text.get_as_string().unwrap_or_default());

let jwe = chilkat::Jwe::new();

// 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.
let jwe_prot_hdr = chilkat::JsonObject::new();
let _ = jwe_prot_hdr.append_string("alg", "A128KW");
let _ = jwe_prot_hdr.append_string("enc", "A128CBC-HS256");
let _ = jwe_prot_hdr.append_string("zip", "DEF");
let _ = jwe.set_protected_header(&jwe_prot_hdr);

// Set the AES key wrap key:
let aes_wrapping_key = "GawgguFyGrWKav7AX4VKUg".to_string();
let _ = jwe.set_wrapping_key(0, &aes_wrapping_key, "base64url");

// Encrypt and return the JWE in sbJweCompressed:
let sb_jwe_compressed = chilkat::StringBuilder::new();
if jwe.encrypt_sb(&sb_plain_text, "utf-8", &sb_jwe_compressed).is_err() {
    println!("{}", jwe.last_error_text());
    return;
}

// Show the compressed JWE:
println!("{}", sb_jwe_compressed.get_as_string().unwrap_or_default());
println!("size of compressed JWE: {}", sb_jwe_compressed.length());

// Now create a JWE without compression.
let _ = jwe_prot_hdr.delete("zip");
// Make sure to update the shared protected header:
let _ = jwe.set_protected_header(&jwe_prot_hdr);

let sb_jwe_uncompressed = chilkat::StringBuilder::new();
if jwe.encrypt_sb(&sb_plain_text, "utf-8", &sb_jwe_uncompressed).is_err() {
    println!("{}", jwe.last_error_text());
    return;
}

// Show the uncompressed JWE:
println!("{}", sb_jwe_uncompressed.get_as_string().unwrap_or_default());
println!("size of uncompressed JWE: {}", sb_jwe_uncompressed.length());

// 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.
let jwe2 = chilkat::Jwe::new();
if jwe2.load_jwe_sb(&sb_jwe_compressed).is_err() {
    println!("{}", jwe2.last_error_text());
    return;
}

// Set the AES wrap key.
let _ = jwe2.set_wrapping_key(0, &aes_wrapping_key, "base64url");

// Decrypt (also automatically decompresses).
let sb_original_text = chilkat::StringBuilder::new();
if jwe2.decrypt_sb(0, "utf-8", &sb_original_text).is_err() {
    println!("{}", jwe2.last_error_text());
    return;
}

println!("original text from compressed JWE: ");
println!("{}", sb_original_text.get_as_string().unwrap_or_default());

// -----------------------------------------------------------
// Do the same with the uncompressed JWE

if jwe2.load_jwe_sb(&sb_jwe_uncompressed).is_err() {
    println!("{}", jwe2.last_error_text());
    return;
}

// Set the AES wrap key.
let _ = jwe2.set_wrapping_key(0, &aes_wrapping_key, "base64url");

// Decrypt.
sb_original_text.clear();
if jwe2.decrypt_sb(0, "utf-8", &sb_original_text).is_err() {
    println!("{}", jwe2.last_error_text());
    return;
}

println!("original text from uncompressed JWE: ");
println!("{}", sb_original_text.get_as_string().unwrap_or_default());

// ------------------------------------------------
// 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.
//