java 解析国密SM2算法证书

首先说明用Java自带的解析x509证书类,是不能解析sm2算法的证书,执行会抛出异常。

用开源库bouncycastle能够解析、详细代码



private byte[] getCSPK(byte[] csCert)
    {
        InputStream inStream = new ByteArrayInputStream(csCert);
        ASN1Sequence seq = null;
        ASN1InputStream aIn;
        try
        {
            aIn = new ASN1InputStream(inStream);
            seq = (ASN1Sequence)aIn.readObject();
            X509CertificateStructure cert = new X509CertificateStructure(seq);
            cert.getVersion();
            ASN1Primitive publicKey = cert.getSubjectPublicKeyInfo().getPublicKey();
            byte[] encodedPublicKey = publicKey.getEncoded();
            byte[] eP = new byte[64];
            System.arraycopy(encodedPublicKey, 5, eP, 0, eP.length);
            return eP;
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
}

你可能感兴趣的:(java)