title: Reading a PFX file PrivateKey and Certificate Chain in java - Getting a Null
body: <p>Here's my code as below:</p>
  
  <pre><code>public static void main(String[] args) throws IOException, GeneralSecurityException, DocumentException {
      String path = "&lt;pfx file&gt;";
      char[] pass = "&lt;password&gt;".toCharArray();
  
      BouncyCastleProvider provider = new BouncyCastleProvider();
      Security.addProvider(provider);
      KeyStore ks = KeyStore.getInstance("pkcs12", provider.getName());
      ks.load(new FileInputStream(path), pass);
      String alias = (String) ks.aliases().nextElement(); /* alias='CCA India 2011\u0000'*/
      PrivateKey pk = (PrivateKey) ks.getKey(alias, pass);/* returns null */
      Certificate[] chain = ks.getCertificateChain(alias);/* returns null */
      X509Certificate last = (X509Certificate) chain[chain.length - 1];
      System.out.println(last.getNotBefore());
      System.out.println(last.getNotAfter());
  }
  </code></pre>
  
  <p>The alias that is returned back has a \u0000 at the end of it. I am not sure what to make of it. Is that the reason why pk and chain is null? I tried to trim the alias to no avail.</p>
  
  <p>I am able to import this certificate into the microsoft keystore. Meaning I am able to see it in the Internet Explorer .. Certificates. I am able to use it to sign documents on Adobe Reader. So there is no issue the pfx file. Just not able to work with it in java.</p>
  
  <p>I have JCE installed as well.</p>