2014-08-31 107 views
3

這是我的代碼,用於使用密鑰庫來保存任意文本作爲密鑰庫中的密鑰,我如何獲得「密鑰庫未初始化錯誤」,我如何初始化密鑰庫?如何初始化密鑰庫

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

    } 

回答

4

密鑰庫必須被初始化,因此你必須調用Keystore.load(...)方法。你的情況,你可以調用例如:

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

我曾試圖ks.load(空)之前並沒有工作,但你的ks.load的答案(NULL,NULL)的偉大工程! – user3083447 2014-09-07 13:16:06