如何初始化密钥库

如何初始化密钥库

本文介绍了如何初始化密钥库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于密钥存储的使用,以节省任意文本作为我如何让密钥库未初始化错误密钥库的关键这是我的code,我怎么能初始化密钥库?

 公共无效secretKeyGeneration(查看视图)抛出KeyStoreException,抛出:NoSuchAlgorithmException,CertificateException,IOException异常{    密钥库KS = KeyStore.getInstance(KeyStore.getDefaultType());
    字节[] SEK =eru9tyighw34ilty348934i34uiq34q34ri.getBytes();
    SecretKey的SK =新SecretKeySpec(SEK,0,sek.length,AES);
    的char []密码=keystorepassword.toCharArray();
    KeyStore.ProtectionParameter的protParam =
    新KeyStore.PasswordProtection(密码);    KeyStore.SecretKeyEntry skEntry =新KeyStore.SecretKeyEntry(SK);
    ks.setEntry(secretKeyAlias​​,skEntry,的protParam);    }


解决方案

密钥库的有无以的初始化,因此你的有无以的调用密钥库.load(...)方法。你的情况,你可以调用例如:

 密钥库KS = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(NULL,NULL);
字节[] SEK =eru9tyighw34ilty348934i34uiq34q34ri.getBytes();
...

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

    }
解决方案

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

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

这篇关于如何初始化密钥库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:54