This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

android安全存储,使用AndroidKeyStore的问题

最近在研究android的keystore安全存储,网上就一篇关于这个的博客,但是有些地方没讲清楚,资料非常少。。。

我使用以下方式存储了私钥和证书:

mKeyStore = KeyStore.getInstance("AndroidKeyStore");//获取密钥库

KeyStore.PrivateKeyEntry expected = new KeyStore.PrivateKeyEntry(priKey, expectedChain);

mKeyStore.setEntry("mytestRSAkey", expected, new KeyStoreParameter.Builder(this.getBaseContext()).setEncryptionRequired(true).build());//加密存储

现在我要提取公钥和私钥:

actualEntry = mKeyStore.getEntry("mytestRSAkey",null);//获取密钥

KeyStore.PrivateKeyEntry actual = (KeyStore.PrivateKeyEntry) actualEntry;

pubKey=actual.getCertificate().getPublicKey();

priKey=actual.getPrivateKey();

可能是出于保护私钥的目的,priKey.getEncoded()是获取不到私钥的,只有一个密钥索引,导致我使用使用该私钥解密时出错

public static byte[] decrypt_RSA(byte[] encryptedData, PrivateKey privateKey)

{

   try
   {

  Cipher cipher = Cipher.getInstance("RSA");

  cipher.init(Cipher.DECRYPT_MODE, privateKey);

   return cipher.doFinal(encryptedData);

  } catch (Exception e)

  {

   return null;

  }

}

现在我该怎么使用priKey来解密数据啊