title: How to store key using Android Key Store Provider
body: <p>I am trying to use the Android Key Store Provider that became available in Android 4.3 to securely save a Private Key and to then use this private key to encrypt and decode data.</p>
  
  <p>I think I have implemented the correct approach and code for this so far however I am currently facing an odd issue that I cannot figure out.</p>
  
  <p>I have a class called KeyStorage that I use to create the Key pair, load the KeyStore and retrieve the private key, the code for this class is as follows:</p>
  
  <pre><code>public class KeyStorage {
  
  public static final String ANDROID_KEYSTORE = "AndroidKeyStore";
  private KeyStore keyStore;
  
  public void loadKeyStore() {
      try {
          keyStore = KeyStore.getInstance(ANDROID_KEYSTORE);
          keyStore.load(null);
          Enumeration&lt;String&gt; aliases = keyStore.aliases();
          while(aliases.hasMoreElements())
              Log.e("E", "Aliases = " + aliases.nextElement());
      } catch (Exception e) {
          // TODO: Handle this appropriately in your app
          e.printStackTrace();
      }
  }
  
  public void generateNewKeyPair(String alias, Context context)
          throws Exception {
  
      Calendar start = Calendar.getInstance();
      Calendar end = Calendar.getInstance();
      // expires 1 year from today
      end.add(1, Calendar.YEAR);
  
      KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
              .setAlias(alias)
              .setSubject(new X500Principal("CN=" + alias))
              .setSerialNumber(BigInteger.TEN)
              .setStartDate(start.getTime())
              .setEndDate(end.getTime())
              .build();
  
      // use the Android keystore
      KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", ANDROID_KEYSTORE);
      gen.initialize(spec);
  
      // generates the keypair
      gen.generateKeyPair();
  }
  
  public PrivateKey loadPrivteKey(String alias) throws Exception {
  
      if (keyStore.isKeyEntry(alias)) {
          Log.e("E", "Could not find key alias: " + alias);
          return null;
      }
  
      KeyStore.Entry entry = keyStore.getEntry(EnhancedCredentialStore.KEY_ALIAS, null);
  
      if (!(entry instanceof KeyStore.PrivateKeyEntry)) {
          Log.e("E", " alias: " + alias + " is not a PrivateKey");
          return null;
      }
  
      return ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();
  }
  </code></pre>
  
  <p>I then have a class called CredentialStore that I try to use this in. I create an alias called "MySecureKey" and try to create and store a private key based on this. So as you can see I try to create the new Key pair based on the Alias load the keystore and then finally retrieve the Private key. However it doesn't work.</p>
  
  <pre><code>public class CredentialStore {
  
  public static final String KEY_ALIAS  = "MySecureKey";
  
  private KeyStorage KeyStorage;
  
  public CredentialStore(Activity context){
  
      keyStorage = new KeyStorage();
      try {
          keyStorage.generateNewKeyPair(KEY_ALIAS, context);
      } catch (Exception e) {
          e.printStackTrace();
      }
      blueBirdKeyStorage.loadKeyStore();
  
      try {
          keyStorage.loadPrivteKey(KEY_ALIAS);
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>I get the logs as follow:</p>
  
  <pre><code>Aliases = MySecureKey
  Could not find key alias: MySecureKey
  </code></pre>
  
  <p>So when I check the aliases in the loadKeyStore method it looks like it is there as it is coming back in the log. However when I try to call it using the loadKeyStore method I get the log saying it can not find the key "MySecureKey".</p>
  
  <p>I cannot find any reason for this and researching online has proven unfruitful so I was wondering if anyone has any idea what might be going wrong?</p>