Sample code for 30+ languages & platforms
Rust

Combine Multiple PKCS12's into a Single Java KeyStore

See more Java KeyStore (JKS) Examples

Combines multiple PKCS12's into a single Java KeyStore (JKS) file. To combine multiple PKCS12 files into a single JKS, simply load each PKCS12 into a PFX object, add it to the Java keystore object via the AddPfx method, and then finally write the Java keystore at the very end.

Chilkat Rust Downloads

Rust

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

let jks = chilkat::JavaKeyStore::new();

let pfx = chilkat::Pfx::new();

// Combines several PKCS12's into a single JKS.
// Simply load each, add it to the keystore, and then 
// save at the very end.

let mut alias = String::new();
let mut pfx_password = String::new();
let jks_password = "jksSecret".to_string();

// Add the 1st PFX...
pfx_password = "secret1".to_string();
if pfx.load_pfx_file("/someDir/file1.p12", &pfx_password).is_err() {
    println!("{}", pfx.last_error_text());
    return;
}

alias = "alias1".to_string();
if jks.add_pfx(&pfx, &alias, &jks_password).is_err() {
    println!("{}", jks.last_error_text());
    return;
}

// Add the 2nd PFX...
pfx_password = "secret2".to_string();
if pfx.load_pfx_file("/someDir/file2.p12", &pfx_password).is_err() {
    println!("{}", pfx.last_error_text());
    return;
}

alias = "alias2".to_string();
if jks.add_pfx(&pfx, &alias, &jks_password).is_err() {
    println!("{}", jks.last_error_text());
    return;
}

// We can continue adding as many PFX's as desired...

// Write the Java keystore to a file:
if jks.to_file(&jks_password, "/jksFiles/my.jks").is_err() {
    println!("{}", jks.last_error_text());
} else {
    println!("Successfully combined multiple PKCS12's into a single JKS");
}