Sample code for 30+ languages & platforms
Java

Add Private Key to Java Keystore

See more Java KeyStore (JKS) Examples

Adds a private key to an existing Java keystore.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JavaKeyStore;
import com.chilkatsoft.chilkat2.Pfx;
import com.chilkatsoft.chilkat2.PrivateKey;
import com.chilkatsoft.chilkat2.XmlCertVault;

public class ChilkatExample {

    public static void main(String[] args) {
        // This requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        JavaKeyStore jks = new JavaKeyStore();

        String jksPassword = "myJksPassword";
        String jksPath = "/someDir/keyStore.jks";

        // Load the Java keystore from a file.
        try {
            jks.loadFile(jksPassword, jksPath);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // A JKS private key entry consists of both the private key,
        // it's associated certificate (which contains the matching public key
        // within the X.509 of the certificate), and the certificates in the
        // chain of authentication to the root.
        //
        // Therefore, to add a private key entry to a JKS requires
        // a Chilkat certificate object that has a private key and which also
        // has the certificate chain (up to the root) available.

        // There are many ways to get a Chilkat certificate object
        // that contains (within it) the private key and the certificate chain
        // This example will show two possibilities:
        // (1) Where the cert and issuing root are provided in PEM format in .crt files,
        // and the private key is also provided in unencrypted PEM format (.key file).
        // (2) Where the cert, private key, and issuing root are provided in a single PFX.

        // First for the .crt / .key files:
        Cert cert = new Cert();

        // Chilkat will automatically determine the format of the cert file and load it correctly.
        try {
            cert.loadFromFile("/mycerts/alice.crt");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Certificates required for building the chain of authentication can be
        // added to an XML certificate vault object, and then provided as
        // a source for obtaining certs when building the chain.
        XmlCertVault certVault = new XmlCertVault();
        try {
            certVault.addCertFile("/mycerts/ca.crt");

            cert.useCertVault(certVault);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Now provide the associated private key to the certificate object.
        // The Chilkat private key class provides methods for loading from many formats (both
        // encrypted and unencrypted).
        PrivateKey privKey = new PrivateKey();
        try {
            privKey.loadPemFile("/mycerts/alice.key");

            // Provide the certificate object with the private key:
            cert.setPrivateKey(privKey);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Our certificate object now contains all that we need to add it as a private key entry
        // to the Java keystore:
        String alias = "alice";
        try {
            jks.addPrivateKey(cert, alias, jksPassword);

            // Write the updated JKS, which contains the new private key entry w/ certificate chain.
            jks.toFile(jksPassword, jksPath);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Added new private key entry (from .crt and .key files) to the JKS!");

        // Now let's add a new private key entry from a PFX that contains a single
        // private key with associated cert and cert chain.
        Pfx pfx = new Pfx();

        try {
            pfx.loadPfxFile("/myPfxFiles/my.pfx", "pfxPassword");

            // This is easy -- simply add the PFX to the JKS
            alias = "bob";
            jks.addPfx(pfx, alias, jksPassword);

            // Write the updated JKS, which contains the new private key entry w/ certificate chain
            // that came from the PFX.
            jks.toFile(jksPassword, jksPath);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Added new private key entry (from PFX) to the JKS!");
    }
}