Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Convert PKCS12 / PFX to Java Keystore (JKS)

See more PFX/P12 Examples

Loads a PKCS12 / PFX file and saves it to a Java keystore (JKS) file.

Chilkat Rust Downloads

Rust

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

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

// Load the PKCS12 from a file
if pfx.load_pfx_file("/someDir/my.p12", "myPfxPassword").is_err() {
    println!("{}", pfx.last_error_text());
    return;
}

let jks_password = "myJksPassword".to_string();
let alias = "firstPrivateKeyAlias".to_string();

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

// Convert to a Java keystore object.
// The jksPassword is the password to be used for the JKS private key entries. 
// It may be the same as the PFX password, but can also be different if desired.
if pfx.to_jks_obj(&alias, &jks_password, &jks).is_err() {
    println!("{}", pfx.last_error_text());
    return;
}

// Save the Java keystore to a file.
if jks.to_file(&jks_password, "/myKeystores/my.jks").is_err() {
    println!("{}", jks.last_error_text());

    return;
}

println!("Successfully converted PFX to JKS.");