java 如何初始化密钥库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25591236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:18:02  来源:igfitidea点击:

How to initialize the Keystore

javaandroidandroid-keystoresecret-key

提问by user3083447

This my code used for usage of key store to save an arbitrary text as a key in the keystore how I am getting the "Keystore is not initialized error", how can I initialise the Keystore?

这是我的代码用于使用密钥库将任意文本保存为密钥库中的密钥我如何得到“密钥库未初始化错误”,我如何初始化密钥库?

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

    }   

回答by Jcs

Keystores have tobe initialized and hence you have tocall the Keystore.load(...)method. In your case you can for instance invoke:

密钥库必须进行初始化,因此你必须调用该Keystore.load(...)方法。在您的情况下,您可以例如调用:

KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
byte[]  sek = "eru9tyighw34ilty348934i34uiq34q34ri".getBytes();
...