title: Storing AES Secret key using keystore in java
body: <p>I am using Java keystore to store the secret key for AES encryption.</p>
  
  <pre><code>final String strToEncrypt = "Hello World";
  KeyGenerator kg = KeyGenerator.getInstance("AES");
  kg.init(128);
  SecretKey sk = kg.generateKey();
  String secretKey = String.valueOf(Hex.encodeHex(sk.getEncoded()));   
  //Storing AES Secret key in keystore
  KeyStore ks = KeyStore.getInstance("JCEKS");
  char[] password = "keystorepassword".toCharArray();
  java.io.FileInputStream fis = null;
  try {
    fis = new java.io.FileInputStream("keyStoreName");
    ks.load(fis, password);
  } finally {
    if (fis != null) {
      fis.close();
    }
  
    KeyStore.ProtectionParameter protParam = 
      new KeyStore.PasswordProtection(password);
  
    KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(sk);
    ks.setEntry("secretKeyAlias", skEntry, protParam);
  </code></pre>
  
  <p>But i am getting following Exception.</p>
  
  <blockquote>
    <p>Exception in thread "main" java.security.KeyStoreException: Uninitialized keystore<br>
    at java.security.KeyStore.setEntry(Unknown Source) </p>
  </blockquote>
  
  <p>How to fix this error? Thanks in advance</p>