这个我的代码用于使用密钥库将任意文本保存为密钥库中的密钥我如何得到"密钥库未初始化错误",如何初始化密钥库?
public void secretKeyGeneration(View view) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
byte[] sek = "eru9tyighw34ilty348934i34uiq34q34ri".getBytes();
SecretKey sk = new SecretKeySpec(sek, 0, sek.length, "AES");
char[] password = "keystorepassword".toCharArray();
KeyStore.ProtectionParameter protParam =
new KeyStore.PasswordProtection(password);
KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(sk);
ks.setEntry("secretKeyAlias", skEntry, protParam);
}
答案 0 :(得分:12)
密钥库必须进行初始化,因此必须调用Keystore.load(...)
方法。在您的情况下,您可以例如调用:
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
byte[] sek = "eru9tyighw34ilty348934i34uiq34q34ri".getBytes();
...