Search code examples
javaencryptioncryptographykeystoresecret-key

getKey(alias, keyPassword) returns null value for secret key when calling from separate method


I am trying to retrieve a stored key in java key store. I have written the following code.

public class clientEncryptionUtility
{

    public static void generateKeyAndStoreOnKeyStore(String _keyStorePassword, String _keyStorePath, String _keyPassword, String keyAlias) throws Exception // take the keystore path, alias, password
    {
        KeyStore keyStore = KeyStore.getInstance("JCEKS");
        char[] keyStorePassword = _keyStorePassword.toCharArray();
        String path = _keyStorePath;
        FileInputStream fis = new FileInputStream(path);
        //load keystore
        keyStore.load(fis, keyStorePassword);
        //Loading the KeyStore object
        KeyStore.ProtectionParameter protectionParam = new KeyStore.PasswordProtection(keyStorePassword);

        //Generate the symmetric key for encryption
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");

        SecureRandom secureRandom = new SecureRandom();

        int keyBitSize = 128;

        keyGenerator.init(keyBitSize, secureRandom);

        SecretKey secretKey = keyGenerator.generateKey();   //Secret encryption key is genereated

        //setting the password for the key stored in keystore
        System.out.println("Algorithm used to generate key : "+secretKey.getAlgorithm()); 

        char[] keyPassword = _keyPassword.toCharArray();

        KeyStore.ProtectionParameter entryPassword = new KeyStore.PasswordProtection(keyPassword);

        KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(secretKey);

        keyStore.setEntry(keyAlias, secretKeyEntry, entryPassword);
        SecretKey newSecretKey = (SecretKey) keyStore.getKey(keyAlias, keyPassword);
        String stringKey = newSecretKey.toString();
        System.out.println("The encryption key at the alias is: " + stringKey);
    }
    public static void getKeyFromKeyStore(String _keyStorePassword, String _keyStorePath, String keyAlias, String _keyPassword) throws Exception
    {
        KeyStore keyStore = KeyStore.getInstance("JCEKS");
        char[] keyStorePassword = _keyStorePassword.toCharArray();
        String path = _keyStorePath;
        FileInputStream fis = new FileInputStream(path);

        //load keystore
        keyStore.load(fis, keyStorePassword);
        char[] keyPassword = _keyPassword.toCharArray();
        SecretKey secretKey = (SecretKey) keyStore.getKey(keyAlias, keyPassword);
        // Key key = keyStore.getKey(keyAlias, keyPassword);
        String stringKey = secretKey.toString();
        System.out.println("The encryption key at the alias is: " + stringKey);

    }
}

-If I call the generateKeyAndStoreOnKeyStore() method, and store the key and retrieve the key in the same function, the key is retrieved.

-However if I do the same from another method getKeyFromKeyStore() wherein I am just trying to retrieve the key at the alias from the keystone, I get a nullPointerException.

-Where am I going wrong?


Solution

  • Unfortunately it's not clear from the javadocs that you must call the KeyStore.store(...) method to persist changes to the keystore. Once generateKeyAndStoreOnKeyStore() exits, the KeyStore instance created there goes out of scope and any unsaved changes made to the keystore disappear.

    Call the KeyStore.store(...) method after making changes.