2017-03-03 82 views
1

我正在使用android密鑰庫api來存儲RSA密鑰對。Android KeyStore未保存RSA密鑰

當我在調試模式下運行我的android時,它顯示密鑰對和證書字節已經存儲在密鑰庫中,但是當我重新加載調試器並嘗試檢索密鑰時,密鑰庫看起來是空的。

我希望能夠獲取由別名的關鍵。下面的代碼顯示了密鑰對的創建和存儲過程。

public RSA(char[] password) throws Exception { 
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 
    ks.load(null); 
    Enumeration<String> aliases = ks.aliases(); 
    if(!aliases.hasMoreElements()) 
    { 
     mCurrentCertificate = generateCert(); 
     //Store the new keypair 
     FileInputStream fs = null; 
     ks.load(fs, password); 

     KeyStore.ProtectionParameter protParam = 
       new KeyStore.PasswordProtection(password); 

     java.security.cert.Certificate[] myCert = 
       new java.security.cert.Certificate[] { (java.security.cert.Certificate) mCurrentCertificate}; 

     KeyStore.PrivateKeyEntry pkEntry = 
       new KeyStore.PrivateKeyEntry(mCurrentRSAKeyPair.getPrivate(), 
         myCert); 

     ks.setEntry("MyKey2", pkEntry, protParam); 

     for(int i = 0; i < ks.size(); i++) 
     { 
      //MyKey is the previous key stored, MyKey2 is the next one 
      System.out.println(ks.getCertificate("MyKey")); 
     } 
    } 
} 

回答

0

與您的代碼,你可以創建文件系統密鑰庫(它缺少ks.store(fileOutputStream, password);,但Android Keystore System有一個特定的API生成和使用按鍵

在這個answer你有一個例子產生並加載RSA密鑰