AES密钥存储

时间:2016-04-23 18:16:50

标签: java exception cryptography aes keystore

大家好,我在将AES密钥存储到keyStore时遇到问题。

这是我的代码:

public Key getKey(String alias) {
    try {
        FileInputStream fStream = new FileInputStream(path);
        keyStore.load(fStream, this.password.toCharArray());
        Key key = keyStore.getKey(alias, null);
        fStream.close();

        return key;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

public void storeKey(Key key, String alias) {
    try {
        keyStore.setKeyEntry(alias, key.getEncoded(), null);
        OutputStream writeStream = new FileOutputStream(path);
        keyStore.store(writeStream, this.password.toCharArray());
        writeStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

所以storeKey工作正常,但是当我调用getKey()时,我得到了这个异常:

java.security.UnrecoverableKeyException: Get Key failed: AES SecretKeyFactory not available
at sun.security.pkcs12.PKCS12KeyStore.engineGetKey(Unknown Source)
at java.security.KeyStore.getKey(Unknown Source)
at sm130075.vl130298.crypto.KeyStoreManager.getKey(KeyStoreManager.java:51)
at sm130075.vl130298.crypto.AES.<init>(AES.java:36)
at sm130075.vl130298.crypto.AES.main(AES.java:84)

编辑: 所以我发现问题很可能是SecretFactory不知道为AES生成密钥。任何人都知道如何解决这个问题?

0 个答案:

没有答案