How to add a private key to Windows Cert Store programmatically
<p>I extracted a Key and its certification chain from a JKS, and now I'm trying to add this key to the Windows Keystore using Java.</p>

<p>To load my JKS I did the following:</p>

<pre><code>    String jksPath = "D:\\mykeystore.jks";
    KeyStore keystore = KeyStore.getInstance("JKS");
    FileInputStream fIn = new FileInputStream(jksPath);
    keystore.load(fIn, "12345678".toCharArray());
</code></pre>

<p>Then I get the key and the certification chain:</p>

<pre><code>    Key key = keystore.getKey("res1", "12345678".toCharArray());
    Certificate[] cchain = keystore.getCertificateChain("res1");
</code></pre>

<p>So far so good, then I try to add this key to my Windows Keystore:</p>

<pre><code>    KeyStore ks = KeyStore.getInstance("Windows-MY");
    ks.load(null, null);
    ks.setKeyEntry("myKey", key, "12345678".toCharArray(), cchain);
</code></pre>

<p>And <strong>BOOM</strong>:</p>

<blockquote>
  <p>Exception in thread "main" java.lang.ClassCastException: [Ljava.security.cert.Certificate; cannot be cast to [Ljava.security.cert.X509Certificate;
      at sun.security.mscapi.KeyStore.engineSetKeyEntry(KeyStore.java:402)
      at sun.security.mscapi.KeyStore$MY.engineSetKeyEntry(KeyStore.java:62)
      at java.security.KeyStore.setKeyEntry(KeyStore.java:909)</p>
</blockquote>

<p>Exception thrown due to the <em>setKeyEntry</em> call. </p>

<p><strong>P.S: when I use the same syntaxe on a JKS type of Keystore no exception is thrown.</strong></p>