Rust Requires Chilkat v11.7.0+
Rust
Choose the PDF Encryption Algorithm: AES-256, AES-128 or RC4
See more PDF Protection Examples
Demonstrates the algorithmChoice option of the encrypt operation. aes-256 is the default and the one to use unless an older reader must open the file. aes-128 is for readers that predate AES-256. rc4-128 and rc4-40 are broken ciphers kept only for very old readers. Each algorithm needs a minimum PDF version, and the file header is raised to it when the PDF is saved, never lowered. A PDF saved with RC4 is written with a classic cross-reference table so that those old readers can parse it.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at two_page_memo.pdf
let json = chilkat::JsonObject::new();
let json_out = chilkat::JsonObject::new();
let info = chilkat::JsonObject::new();
// AES-256, the default. Saved as PDF 1.7 with Adobe extension level 8.
let pdf1 = chilkat::Pdf::new();
if pdf1.load_file("qa_data/pdf/two_page_memo.pdf").is_err() {
println!("{}", pdf1.last_error_text());
return;
}
pdf1.set_user_password("user-secret");
pdf1.set_owner_password("owner-secret");
let _ = json.update_string("algorithmChoice", "aes-256");
if pdf1.protection("encrypt", &json, &json_out).is_err() {
println!("{}", pdf1.last_error_text());
return;
}
println!("aes-256 will be saved as PDF {}, Adobe extension level {}", json_out.string_of("pending.pdfVersion").unwrap_or_default(), json_out.int_of("pending.adobeExtensionLevel"));
if pdf1.write_file("qa_output/memo_aes256.pdf").is_err() {
println!("{}", pdf1.last_error_text());
return;
}
if pdf1.pre_load_check("qa_output/memo_aes256.pdf", &info).is_err() {
println!("{}", pdf1.last_error_text());
return;
}
println!(" saved file: {}, PDF {}", info.string_of("algorithm").unwrap_or_default(), info.string_of("pdfVersion").unwrap_or_default());
// AES-128, for readers that predate AES-256. Saved as PDF 1.6.
let pdf2 = chilkat::Pdf::new();
if pdf2.load_file("qa_data/pdf/two_page_memo.pdf").is_err() {
println!("{}", pdf2.last_error_text());
return;
}
pdf2.set_user_password("user-secret");
pdf2.set_owner_password("owner-secret");
let _ = json.update_string("algorithmChoice", "aes-128");
if pdf2.protection("encrypt", &json, &json_out).is_err() {
println!("{}", pdf2.last_error_text());
return;
}
println!("aes-128 will be saved as PDF {}", json_out.string_of("pending.pdfVersion").unwrap_or_default());
if pdf2.write_file("qa_output/memo_aes128.pdf").is_err() {
println!("{}", pdf2.last_error_text());
return;
}
if pdf2.pre_load_check("qa_output/memo_aes128.pdf", &info).is_err() {
println!("{}", pdf2.last_error_text());
return;
}
println!(" saved file: {}, PDF {}", info.string_of("algorithm").unwrap_or_default(), info.string_of("pdfVersion").unwrap_or_default());
// RC4-128, a broken cipher, only for readers too old for AES. Saved as PDF 1.4.
let pdf3 = chilkat::Pdf::new();
if pdf3.load_file("qa_data/pdf/two_page_memo.pdf").is_err() {
println!("{}", pdf3.last_error_text());
return;
}
pdf3.set_user_password("user-secret");
pdf3.set_owner_password("owner-secret");
let _ = json.update_string("algorithmChoice", "rc4-128");
if pdf3.protection("encrypt", &json, &json_out).is_err() {
println!("{}", pdf3.last_error_text());
return;
}
println!("rc4-128 will be saved as PDF {}", json_out.string_of("pending.pdfVersion").unwrap_or_default());
if pdf3.write_file("qa_output/memo_rc4_128.pdf").is_err() {
println!("{}", pdf3.last_error_text());
return;
}
if pdf3.pre_load_check("qa_output/memo_rc4_128.pdf", &info).is_err() {
println!("{}", pdf3.last_error_text());
return;
}
println!(" saved file: {}, PDF {}", info.string_of("algorithm").unwrap_or_default(), info.string_of("pdfVersion").unwrap_or_default());
// Sample output:
// aes-256 will be saved as PDF 1.7, Adobe extension level 8
// saved file: aes-256, PDF 1.7
// aes-128 will be saved as PDF 1.6
// saved file: aes-128, PDF 1.6
// rc4-128 will be saved as PDF 1.4
// saved file: rc4-128, PDF 1.4