Rust
Rust
Encrypt a JWE with a Password (PBES2)
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetPassword, which sets the PBES2 password for a recipient. The example uses PBES2-HS256+A128KW key management with AES-128-GCM content encryption.
Background. PBES2 derives a key-wrapping key from a password using a salt and iteration count, allowing password-based JWE encryption. The password should come from a secure source.
Chilkat Rust Downloads
let jwe = chilkat::Jwe::new();
// Protected header: PBES2 password-based key management with AES-128-GCM content encryption.
let header = chilkat::JsonObject::new();
let _ = header.update_string("alg", "PBES2-HS256+A128KW");
let _ = header.update_string("enc", "A128GCM");
if jwe.set_protected_header(&header).is_err() {
println!("{}", jwe.last_error_text());
return;
}
// Set the PBES2 password for recipient 0. The password should come from a secure source rather than
// being hard-coded.
let password = "myPassword".to_string();
if jwe.set_password(0, &password).is_err() {
println!("{}", jwe.last_error_text());
return;
}
let Ok(jwe_compact) = jwe.encrypt("This is the secret content.", "utf-8") else {
println!("{}", jwe.last_error_text());
return;
};
println!("{}", jwe_compact);