将签名的X509证书和私钥保存在Android中

时间:2019-02-20 08:45:50

标签: java android bouncycastle x509certificate

我想将X509Certificate及其私钥保存到Android KeyStore中,我想将X509Certificate(包含公钥)及其私钥“合并”。私钥用于创建CSR,然后服务器端对证书签名并返回到应用程序,我可以将证书和私钥合并为一个唯一的证书吗?另外,我正在使用海绵宝宝(又名bouncycastle的android包装器)。

1 个答案:

答案 0 :(得分:0)

我对Android KeyStore一无所知,但也许您可以尝试以下方法:

PrivateKey privateKey = ...         //this is what you already have
X509Certificate certificate = ...   //this is what you already have

KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);

Certificate[] certChain = new Certificate[1];  
certChain[0] = certificate;  

char[] myKeyPassword = "myKeyPassword".toCharArray();
keyStore.setKeyEntry("mykeyalias", (Key)privateKey, myKeyPassword, certChain);  

有关KeyStore.setKeyEntry的更多信息,请参见https://docs.oracle.com/javase/9/docs/api/java/security/KeyStore.html#setKeyEntry-java.lang.String-java.security.Key-char:A-java.security.cert.Certificate:A-