title: Get public Key from imported certificate in Keystore with Java
body: <p>I have created and downloaded a certificate from <a href="https://ap1.salesforce.com/" rel="nofollow">sales force</a>, as per the instructions in <a href="https://docs.jboss.org/author/display/PLINK/Picketlink+as+SP,+Salesforce+as+IDP" rel="nofollow">PicketLink document</a>.</p>
  
  <p>I downloaded the certificate and its name is <strong><em>mysample.crt</em></strong> and 
  I imported the certificate into a keysotre.</p>
  
  <pre><code>keytool -import -file mysample.crt -keystore keystore.jks -alias salesforce-idp
  </code></pre>
  
  <p>To check, I exported the public key also</p>
  
  <pre><code>keytool -export -alias salesforce-idp -keystore keystore.jks -rfc -file public.cert
  </code></pre>
  
  <p>I have a Java code to get the Public Key, but it is not working. This is my Code</p>
  
  <pre><code>package com.sample.keystore;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.security.Key;
  import java.security.KeyPair;
  import java.security.KeyStore;
  import java.security.PrivateKey;
  import java.security.PublicKey;
  
  import org.apache.commons.codec.binary.Base64;
  
  public class ExtractPublicKey {
  
      public static void main(String[] args) {
  
          try {
              // Load the keystore
              File file = new File("/home/user/salesforce-cert/keystore.jks");
              FileInputStream is = new FileInputStream(file);
              KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
              String alias = "salesforce-idp";
              String password = "user";
              char[] passwd = password.toCharArray();
              keystore.load(is, passwd);
              KeyPair kp = getKeyPair(keystore, alias, passwd);
              Base64 base64 = new Base64();
              PublicKey pubKey = kp.getPublic();
  
              String publicKeyString = base64.encodeBase64String(pubKey
                      .getEncoded());
  
              System.out.println(publicKeyString);
              is.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public static KeyPair getKeyPair(KeyStore keystore, String alias, char[] password) throws Exception {
              // Get private key
              Key key = keystore.getKey(alias, password);
              if (key instanceof PrivateKey) {
                  // Get certificate of public key
                  java.security.cert.Certificate cert = keystore.getCertificate(alias);
  
                  // Get public key
                  PublicKey publicKey = cert.getPublicKey();
  
                  // Return a key pair
                  return new KeyPair(publicKey, (PrivateKey)key);
              }
          return null;
      }
  
  }
  </code></pre>
  
  <p>But when I run the code, I get the following exception</p>
  
  <pre><code>java.lang.NullPointerException
      at com.sample.keystore.ExtractPublicKey.main(ExtractPublicKey.java:28)
  </code></pre>
  
  <p>Line 28 refers to <code>PublicKey pubKey = kp.getPublic();</code>. Because the method returns <strong><em>null</em></strong> instead of the Key Pair. Why is that? And how to get the Public Key?</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>I updated the code to</p>
  
  <pre><code>keystore.load(is, passwd);
  PublicKey pubKey = keystore.getCertificate(alias).getPublicKey();
  String publicKeyString = Base64.encodeBase64String(pubKey.getEncoded());
  System.out.println(publicKeyString);
  </code></pre>
  
  <p>Then I am getting folowing Key</p>
  
  <pre><code>MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlKJTbmfEumDR7nAfBbfAstuUvsgKxizZ1mwGc990dSsmgldIhsrLqpAECdf7vl2q2F8DyXciOopZbJPt/UBmpl6M1TJCQ34UyZaYGI2qid8jSNxFYGApfYPxIBJAk9YOAATqqyAREL+i1mUaFfN8WULFDvz6WsuXOjuxBobqjkg4TUumyyVgZda9ksl3aJmft02AfDMw/GCT8gKPTQb3nZP9BwTo5AQkV5fy0cKZ80G4qD+fiuZJ+8IecgFgXl5agZ0y2Wri8i1OGTGw34SUP2gOO+NUd17YA5AO+ocHlH8yzlXHNH7DPQsLo+Uz8CcXV+eLyzxGTGfuiTw8qsPCCwIDAQAB
  </code></pre>
  
  <p>Nut actual key is different. In <em>public,cert</em>, the key is different from what I am getting through Java code.</p>