title: How to add KeyStore file protected with password to zip in java
body: <p>I created KeyStore file protected with the password</p>
  
  <pre><code>CertificateFactorycertFactory = CertificateFactory.getInstance("X.509");
  InputStream in = new ByteArrayInputStream(certyfikat);
  X509Certificate cert = (X509Certificate)  
  certFactory.generateCertificate(in);
  KeyStore ks = KeyStore.getInstance("PKCS12");
  ks.load(null,null);
  ks.setCertificateEntry("alias", cert);
  FileOutputStream fos = new FileOutputStream("test.pfx");
  ks.store(fos, "yourPassword".toCharArray());
  </code></pre>
  
  <p>Is this right way to add password protection to KeyStore?</p>
  
  <p>I want to return this in the zip file. I tried ZipOutputStream and ZipEntry, but using ZipOutputStream.write() can handle only byte[]. </p>
  
  <pre><code>ByteArrayOutputStream baos = new ByteArrayOutputStream();
  ZipOutputStream zos = new ZipOutputStream(baos);
  ZipEntry entry = new ZipEntry("cert.pfx");
  zos.putNextEntry(entry);
  zos.write(ks???);
  </code></pre>
  
  <p>How can I convert KeyStore to byte[]?</p>
  
  <p>Is there any other way to create a zip with KeyStore? </p>
  
  <p>Maybe .pfx file should be created in another way rather than with KeyStore class?</p>