gpt4 book ai didi

java - Android - 如何从系统加载用户证书

转载 作者:行者123 更新时间:2023-12-01 11:19:59 27 4
gpt4 key购买 nike

我正在为 Android 5 或更高版本开发应用程序。

我需要加载 SSL 客户端证书以向 API 发出请求。

我可以实现从下载文件夹加载证书并提出请求:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "certificate.pfx");
KeyStore clientStore = KeyStore.getInstance("PKCS12");
clientStore.load(new FileInputStream(file), "password".toCharArray());

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(clientStore, "password".toCharArray());
KeyManager[] kms = kmf.getKeyManagers();

SSLContext sslContext = null;
sslContext = SSLContext.getInstance("TLS");
sslContext.init(kms, null, new SecureRandom());

HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

但是我想知道是否有可能从Android系统加载证书(当然如果我以前在Android中安装过这样的 post)。

可能吗?

最佳答案

将您的证书文件添加到 Assets 目录并替换:

File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "certificate.pfx");
KeyStore clientStore = KeyStore.getInstance("PKCS12");
clientStore.load(new FileInputStream(file), "password".toCharArray());


KeyStore clientStore = KeyStore.getInstance("PKCS12");
InputStream is = context.getAssets().open("certificate.pfx");
clientStore.load(is, "password".toCharArray());

关于java - Android - 如何从系统加载用户证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46736191/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com