BouncyCastle:从Certificate中提取公钥导致NullPointerException

时间:2020-12-11 18:26:05
import com.security.crypto.Configuration.Properties;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.x509.Certificate;
import org.bouncycastle.jce.provider.X509CertificateObject;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.PublicKey;
import java.security.cert.X509Certificate;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws Exception {

 String df="MIID/TCCAuWgAwIBAgIBATANBgkqhkiG9w0BAQUFADBjMRQwEgYDVQQDEwtleGFtcGxlLm9yZzELMAkGA1UEBhMCR1IxDzANBgNVBAgTBkF0aGVuczEPMA0GA1UEBxMGQXRoZW5zMQ0wCwYDVQQKEwRUZXN0MQ0wCwYDVQQLEwRUZXN0MB4XDTE2MDkxODE5NDkzMFoXDTE3MDkxODE5NDkzMFowYzEUMBIGA1UEAxMLZXhhbXBsZS5vcmcxCzAJBgNVBAYTAkdSMQ8wDQYDVQQIEwZBdGhlbnMxDzANBgNVBAcTBkF0aGVuczENMAsGA1UEChMEVGVzdDENMAsGA1UECxMEVGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAIFsgF86ymuWPEMCZ5Jlj3pLeq2egQ7aiXD3pwhJQemwoC7wSUoViTt3vnNxVVd7HLQZE4igtCI/ZHJhCf5LKoJqxsAfNjq+KK7/9phznX9MroMpluF8LCKDH6otF7rqknLvI72M+oPYkxTYJv02Wb4+lGWn4aYpUh6XmQPDOW6Yoz7RfrY6t0skJMVGqQhg37n/xHrqcc8KmEy0SBpbsRPi6q8Vwsdbd0MiqWt9NdzfBz6kw8WjMSDAf+H4Nwhydgh4sXxSo2zQHobY+QrEr+RLJI++YsqgBeXYeAWbdKb9C6S16VIe8PNi4EqFYtoOzZcMWDl+1xACiFQemor9FRkCAwEAAaOBuzCBuDAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIC9DA7BgNVHSUENDAyBggrBgEFBQcDAQYIKwYBBQUHAwIGCCsGAQUFBwMDBggrBgEFBQcDBAYIKwYBBQUHAwgwEQYJYIZIAYb4QgEBBAQDAgD3MCwGA1UdEQQlMCOGG2h0dHA6Ly9leGFtcGxlLm9yZy93ZWJpZCNtZYcEfwAAATAdBgNVHQ4EFgQUwOcGIZDvBxPH5FJ2ZKo6YoK1oIswDQYJKoZIhvcNAQEFBQADggEBAARO4d3/JNRmTg33HQ2OigiQ3yh/RCd2u9tF39EmU1tpg/nGMnVql+c+M7TnX51vqGJ2oX5nCY/vM2LgUGCqRcuZLRk2u0SzlaI1QlEPLLnsoCes5rU1tVm8xSUOVYp809F8Eiih0A+NZWbPuT83UgiJVtYOvvEWsnlpErkeP4KblS3z532651pTC/RzKO1saRPx4kBI7QAGogEtjbhvMX8099g0mBHvXcVxrIMTUY4sKntMlYQ4vQ4OxBTEXhKwEW1WJh8orXl3E0EkTFhbjkFE9gbqsS3h4ridMcmahoeIwnwckaU5zxgJ2t3ih35FzZXBmfv3qRgFG81Gdi+NH1U=";
    X509Certificate cert = loadCertificate(df);
    System.out.println(cert.getSigAlgName());//SHA1withRSA
    PublicKey key=cert.getPublicKey();
    System.out.println(key.getAlgorithm());//java.lang.NullPointerException
    }

    public static X509Certificate loadCertificate(String asn1)throws IOException, GeneralSecurityException
    {
        byte []data=Base64.decodeBase64(asn1.getBytes(Properties.CHAR_ENCODING));
        ByteArrayInputStream inStream = new ByteArrayInputStream(data);
        ASN1InputStream derin = new ASN1InputStream(inStream);
        ASN1Primitive certInfo = derin.readObject();
        ASN1Sequence seq = ASN1Sequence.getInstance(certInfo);
        return new X509CertificateObject(Certificate.getInstance(seq));
    }
}

i am using BouncyCastle to generate a X509Certificate from DER-encoded. Everything looks good the certificate loads with succes. But when i try get the public key from certificate i got NullpointerException when i tried to use the key just like the above example .What happens here?

我正在使用BouncyCastle从DER编码生成X509Certificate。一切看起来都很好,证书加载成功。但是当我尝试从证书获取公钥时,当我尝试使用密钥时,我得到了NullpointerException,就像上面的例子一样。这里发生了什么?

SHA1withRSA
Exception in thread "main" java.lang.NullPointerException
    at com.security.crypto.App.main(App.java:28)

1 个解决方案

#1


3  

For reasons that are not entirely clear to me you must add the Bouncycastle JCE provider. So, at the start of main, you need

由于我不完全清楚的原因,您必须添加Bouncycastle JCE提供商。所以,在主要的开始,你需要

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

#1


3  

For reasons that are not entirely clear to me you must add the Bouncycastle JCE provider. So, at the start of main, you need

由于我不完全清楚的原因,您必须添加Bouncycastle JCE提供商。所以,在主要的开始,你需要

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());