org.bouncycastle.util.encoders.Base64.decode 解码去换行问题

org.bouncycastle.util.encoders.Base64.decode解码去换行问题

byte[] b1 = Base64.decode("MIIDFjCCAn+gAwIBAgIJAK2rvBRSfeaoMA0GCSqGSIb3DQEBBQUAMGYxCzAJBgNV\nBAYTAkNOMREwDwYDVQQIEwhaaGVqaWFuZzERMA8GA1UEBxMISGFuZ3pob3UxGjAY");
byte[] b2 = Base64.decode("MIIDFjCCAn+gAwIBAgIJAK2rvBRSfeaoMA0GCSqGSIb3DQEBBQUAMGYxCzAJBgNVBAYTAkNOMREwDwYDVQQIEwhaaGVqaWFuZzERMA8GA1UEBxMISGFuZ3pob3UxGjAY");
        

在低版本bc(bcprov-jdk14-119.jar)包中运行上面代码得到的b1和b2结果是不一样的。
在更高版本bc(bcprov-jdk16-1.45.jar)包中运行上面代码得到的b1和b2结果是一样的。

public static  PublicKey getPubKeyFromStr(String certMsgStr){
        InputStream tIn = null;
        Certificate tCer = null;
        PublicKey key = null;
        try {
            CertificateFactory tCertFactory = CertificateFactory.getInstance("X.509");
            tIn = new ByteArrayInputStream(Base64.decode(certMsgStr));
            tCer = (X509Certificate) tCertFactory.generateCertificate(tIn);
            key = tCer.getPublicKey();
        } catch (CertificateException e) {
            e.printStackTrace();
        } finally {
            try {
                if(tIn != null)
                    tIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return key;
    }

在公钥读取时,如果用低版本bc包解码要注意传进来的公钥一定不能用换行,否则读取公钥会失败。高版本可以不用考虑换行问题。

你可能感兴趣的:(原创技术与感悟)