getKey(alias, keyPassword) returns 从单独方法调用时密钥的空值

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

我正在尝试检索 java 密钥库中存储的密钥。我写了下面的代码。

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);

    }
}

-如果我调用 generateKeyAndStoreOnKeyStore() 方法,并在同一函数中存储密钥和检索密钥,则检索密钥。

-但是,如果我从另一个方法 getKeyFromKeyStore() 执行相同的操作,其中我只是试图从 keystone 中检索别名处的密钥,我会得到一个 nullPointerException。

-我哪里错了?

不幸的是,javadoc 并不清楚您必须调用 KeyStore.store(...) 方法才能 persist 对密钥库的更改。一旦 generateKeyAndStoreOnKeyStore() 退出,在那里创建的 KeyStore 实例就会超出范围,对密钥库所做的任何未保存的更改都会消失。

修改后调用KeyStore.store(...)方法。