title: java.security.UnrecoverableKeyException: Get Key failed: null while reading .p12 file in Java
body: <p>I am trying to extract the private key from a .p12 file downloaded from Google Service Account Console.</p>
  <p>I am reading this <code>.p12</code> from the folder location and creating the <code>KeyStore </code> object.</p>
  <pre class="lang-java prettyprint-override"><code>KeyStore myStore = null;
  FileInputStream in_cert = new FileInputStream(
                  &quot;D://Google Analytics//login//GA//6aa55af54232dfa60b60a908ff0138bba1147d63-privatekey.p12&quot;);
  
  myStore = KeyStore.getInstance(&quot;PKCS12&quot;);
  myStore.load(in_cert, null); // if i give &quot;changeit&quot; as the password(hopefully the default keystore password) gets the exception &quot;java.security.UnrecoverableKeyException: Get Key failed: Given final block not properly padded. If null is passed as an argument the program runs fine with output as &quot;privatekey&quot;&quot;
  
  Enumeration&lt;String&gt; aliases = myStore.aliases();
  String alias = &quot;&quot;;
  System.out.println(myStore.aliases());
  
  while (aliases.hasMoreElements()) {
      System.out.println(aliases.nextElement()); // prints privatekey
  }
  
  java.security.PrivateKey privateKey = (PrivateKey) myStore.getKey(&quot;privatekey&quot;, null); // gives exception &quot;java.security.UnrecoverableKeyException: Get Key failed: null&quot;
  </code></pre>
  <p>What's wrong that I am doing?</p>
  <p>I need the private key as a signer for creating Google JSON Web Token.</p>