gpt4 book ai didi

android - CertificateFactory 不从 InputStream 生成证书

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:02 25 4
gpt4 key购买 nike

我正在尝试将自签名证书导入 Keystore。我将证书放在 Assets 文件夹中并尝试读取 CertificateFactory 中的输入流。

但 CertificateFactory 对象为空。它无法读取输入流。 Inputstream 具有值(value),我可以检查一下。请在此处查看我的代码:

CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
AssetManager assetManager = getAssets();
InputStream caInput = assetManager.open("somecert.pem");
X509Certificate caCertificate = (X509Certificate)cf.generateCertificate(caInput);
// Certificate ca = cf.generateCertificate(caInput); //Also did this.

caCertificate 具有空值。我正在尝试在我的 keystore 中使用它。

        String keyStoreType = KeyStore.getDefaultType();
keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", caCertificate);

最佳答案

当我需要上传证书crt时,使用这段代码(这段代码可以为你服务,我认为是一样的):

// Loading the certificate in asserts
CertificateFactory cf = CertificateFactory.getInstance("X.509");
AssetManager assManager = context.getAssets();
caInput = assManager.open("certif.crt");
Certificate ca = cf.generateCertificate(caInput);
// Create a KeyStore containing different CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);

// Create a TrustManager to store certificates
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);

// Creates a context for use ssl certificate
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);

但在这个问题中可能会找到解决方案 How to Load RSA Private Key From File

关于android - CertificateFactory 不从 InputStream 生成证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29234245/

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