title: X509Certificate2.Export equivalent in java
body: <p>I'm trying to port some existing code from C# to Java, this specific part uses the <a href="https://msdn.microsoft.com/en-us/library/24ww6yzk(v=vs.110).aspx" rel="nofollow">X509Certificate2.Export</a> method to get the certificate exported as <code>byte[]</code> for later encoding as Base64.  </p>
  
  <pre><code>var pfxPassword = "passw0rd";
  var appCert = new X509Certificate2("c:\\mycert.pfx", pfxPassword, X509KeyStorageFlags.Exportable);
  byte[] certificateBytes = certificate.Export(X509ContentType.Pfx, pfxPassword);
  string certString = Convert.ToBase64String(certificateBytes);
  System.Console.WriteLine(certString);
  </code></pre>
  
  <p>So i tried by using the <a href="https://docs.oracle.com/javase/7/docs/api/java/security/KeyStore.html#store(java.io.OutputStream,%20char[])" rel="nofollow">KeyStore.store()</a> java method</p>
  
  <pre><code>String caPassword = "passw0rd";
  KeyStore keyStore = KeyStore.getInstance("pkcs12");
  keyStore.load(new FileInputStream(new File("c:\\mycert.pfx")), caPassword.toCharArray());
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  keyStore.store(outputStream, caPassword.toCharArray());
  byte[] encodedBytes = Base64.encodeBase64(outputStream.toByteArray());
  String result = new String(encodedBytes, "UTF-8");
  System.out.println(result);
  </code></pre>
  
  <p>Accordingly with the documentation for Export and store:</p>
  
  <blockquote>
    <p>Export: Exports the current X509Certificate object to a byte array in a format described by one of the X509ContentType values, and using the specified password.</p>
  </blockquote>
  
  <p>And </p>
  
  <blockquote>
    <p>store: Stores this keystore to the given output stream, and protects its integrity with the given password.</p>
  </blockquote>
  
  <p>But these code snippets are giving me different results in size and content by using the same input file and passphrase, so why is this happen? </p>
  
  <p>How can I obtain the same results between these languages/frameworks?</p>