【问题标题】:Public Key Generation from .cer file not working on Android 28从 .cer 文件生成公钥不适用于 Android 28
【发布时间】:2019-01-24 00:21:41
【问题描述】:

我有一个从 .cer 文件生成公钥的方法。我将 .cer 文件内容转换为输入流,一旦获得流,我就会调用此方法来生成公钥

public static void generatePublicKey(InputStream inputStream) {
        try {
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", "BC");
            Certificate certificate = certificateFactory.generateCertificate(inputStream);
            publicKey = certificate.getPublicKey();
            inputStream.close();
        } catch (CertificateException | IOException e) {
            e.printStackTrace();
        }
    }

在我们将项目更新为以 Android Pie 为目标之前,它一直有效。看起来 google 不推荐使用 BC 提供程序,这就是导致问题的原因。如果我在 getInstance() 中使用“BC”,我会得到 NoSuchAlgorithmException。如果我删除“BC”并通过CertificateFactory.getInstance("X.509"),这是谷歌在https://android-developers.googleblog.com/2018/03/cryptography-changes-in-android-p.html 建议的方法 我明白了

com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: java.lang.RuntimeException: error:0c0000be:ASN.1 encoding routines:OPENSSL_internal:WRONG_TAG

【问题讨论】:

  • 您找到解决方案了吗?我也遇到了同样的问题。

标签: java android ssl-certificate x509certificate public-key-encryption


【解决方案1】:

我有同样的错误。问题是如何创建输入流。 试试这个:

InputStream is = getAssets().open("certbase64.cer");
BufferedInputStream bis = new BufferedInputStream(is);    
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(bis);

文件必须在“assets”文件夹中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 2021-02-04
    • 2020-09-24
    • 2023-03-12
    • 2019-06-10
    • 2021-02-14
    • 2013-10-11
    相关资源
    最近更新 更多