title: Load certificate and private key into Java KeyStore
body: <p>I'm trying to fetch a certificate and its private key from Azure Key Vault then call a remote server and do client certificate authentication.</p>
  <p>The first part works well (fetching from Key Vault), however i'm completely stuck at importing the public and private material into KeyStore.</p>
  <p>I've tried</p>
  <pre><code>keyStore.load(publicKey, null);
  keyStore.load(new ByteArrayInputStream(privateKey.getBytes()),
      &quot;thePassphrase&quot;.toCharArray());
  </code></pre>
  <p>but this leads to</p>
  <pre><code>java.io.IOException: DER input, Integer tag error
          at java.base/sun.security.util.DerInputStream.getInteger(DerInputStream.java:192)
          at java.base/sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1995)
          at java.base/sun.security.util.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:222)
          at java.base/java.security.KeyStore.load(KeyStore.java:1479)
  </code></pre>
  <p>Here's the full thing minus what i don't know how to implement -</p>
  <pre class="lang-java prettyprint-override"><code>DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
  
  SecretClient secretClient = new SecretClientBuilder()
      .vaultUrl(&quot;https://&lt;myvault&gt;.vault.azure.net&quot;)
      .credential(credential)
      .buildClient();
  
  CertificateClient certClient = new CertificateClientBuilder()
      .vaultUrl(&quot;https://&lt;myvault&gt;.vault.azure.net&quot;)
      .credential(credential)
      .buildClient();
  
  // Get the public part of the cert
  KeyVaultCertificateWithPolicy certificate = certClient.getCertificate(&quot;Joes-Crab-Shack&quot;);
  byte[] publicKey = certificate.getCer();
  
  // Get the private key
  KeyVaultSecret secret = secretClient.getSecret(
      certificate.getName(),
      certificate.getProperties().getVersion());
  String privateKey = secret.getValue();
  
  // ***************
  // How do i get the cert and its private key into KeyStore?
  KeyStore keyStore = KeyStore.getInstance(&quot;PKCS12&quot;);
  // I've also tried &quot;JKS&quot; but that leads to
  //    java.io.IOException: Invalid keystore format
  keyStore.load(...)
  // ***************
  
  // Do client certificate authentication
  SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, null).build();
  
  CloseableHttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();
  response = httpClient.execute(new HttpGet(&quot;https://remote.that.asks.for.client.cert/&quot;));
  
  InputStream inputStream = response.getEntity().getContent();
  
  body = IOUtils.readInputStreamToString(inputStream, Charsets.UTF_8);
  </code></pre>
  <p>How do i get the certificate and its private key into KeyStore so i can then use it in my HTTP client?</p>