CFP*_*CFP 3 android x509certificate android-keystore
用户在 SD 卡上保存了一个 .p12 文件(例如他的 S/MIME 证书)。我想将此证书(或提取的私钥和公钥)加载到 AndroidKeyStore 中。
File file = new File(pathToP12File);
Certificate c = null; // TODO: convert file into something I can load into the keystore
KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
ks.setCertificateEntry("myCertAlias", c);
Run Code Online (Sandbox Code Playgroud)
将文件转换为可以在密钥库中设置为证书条目的最佳方法是什么?
可以将 p12 文件解释为密钥库,提取证书并将其加载到 AndroidKeyStore 中。
private void getCertsFromP12(String pathToFile, String passphrase){
try {
KeyStore p12 = KeyStore.getInstance("pkcs12");
p12.load(new FileInputStream(pathToFile), passphrase.toCharArray());
Enumeration e = p12.aliases();
while (e.hasMoreElements()) {
String alias = (String) e.nextElement();
X509Certificate c = (X509Certificate) p12.getCertificate(alias);
addCertificateToKeyStore(c);
}
} catch (Exception e) {}
}
private void addCertificateToKeyStore(X509Certificate c) {
try {
KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
ks.setCertificateEntry("myCertAlias", c);
} catch (Exception e){}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4170 次 |
| 最近记录: |