【问题标题】:CertificateFactory not generating certificate from InputStreamCertificateFactory 未从 InputStream 生成证书
【发布时间】:2015-03-24 13:43:36
【问题描述】:

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

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

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 的值为空。我正在尝试在我的密钥库中使用它。

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

【问题讨论】:

    标签: android ssl x509certificate bouncycastle


    【解决方案1】:

    当我需要上传证书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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 2016-01-29
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多