title: Convert private key to PKCS#8 format in java
body: <p>I'm trying to get certificates and private keys from windows certificate store using MSCAPI provider, then i need to store them in a Java Keystore object, but i'm facing a problem of private keys format, the error says:
  <code>java.security.KeyStoreException: Cannot get key bytes, not PKCS#8 encoded</code>
  Here's my code:</p>
  
  <pre><code>SunMSCAPI providerMSCAPI = new SunMSCAPI();
  Security.addProvider(providerMSCAPI);
  KeyStore ks = KeyStore.getInstance("Windows-MY");
  ks.load(null, null);
  PrivateKey privateKey = null;
  X509Certificate cert = null;
  cert = (X509Certificate) ks.getCertificate("ALIAS");
  if (ks.isKeyEntry("ALIAS")) {
      privateKey = (PrivateKey) ks.getKey("ALIAS", null);
  }
  KeyStore newKs = null;
  ...
  ...
  newKs .setKeyEntry("pvKey", privateKey , "pwd".toCharArray(), certifChain);
  </code></pre>
  
  <p>Also, the <code>privateKey.getEncoded()</code> returns null.</p>