title: How to create a PKCS12 keystore with privatekey encryption PBEWithHmacSHA512AndAES_128
body: <p>I need to build a PKCS-12 file in Scala/Java and I wish to use an AES based encryption of the private-key (e.g. PBEWithHmacSHA512AndAES_128)</p>
  
  <p>I use this code (taken from <a href="https://neilmadden.wordpress.com/2017/11/17/java-keystores-the-gory-details/" rel="nofollow noreferrer">here</a>)</p>
  
  <pre><code>val outputStream = new FileOutputStream(file)
  val salt = new Array[Byte](20)
  new SecureRandom().nextBytes(salt)
  val kspkcs12 = KeyStore.getInstance("PKCS12")
  kspkcs12.load(null, null)
  kspkcs12.setEntry("test", new KeyStore.PrivateKeyEntry(keys.getPrivate, Array(cert)),
    new KeyStore.PasswordProtection("changeMe".toCharArray, "PBEWithHmacSHA512AndAES_128", new PBEParameterSpec(salt,
      100000)))
  kspkcs12.store(outputStream, "changeMe".toArray)
  </code></pre>
  
  <p>now, when checking the result with </p>
  
  <pre><code> openssl pkcs12 -info -in filename.p12  -noout
  </code></pre>
  
  <p>I'm getting: </p>
  
  <blockquote>
    <p>MAC:sha1 Iteration 100000 PKCS7 Data Shrouded Keybag:
    PBES2&lt;unsupported parameters&gt; PKCS7 Encrypted data:
    pbeWithSHA1And40BitRC2-CBC, Iteration 50000 Certificate bag'</p>
  </blockquote>
  
  <p>Why am I getting a "PKCS7 Encrypted data" instead of PKCS12?
  I understand from the </p>
  
  <blockquote>
    <p>"PBES2&lt;unsupported parameters&gt;"</p>
  </blockquote>
  
  <p>message that the provider does not support the requested algorithm. Is there a PKCS12 provider that does?</p>