首先说明用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;
}
所需要的jar包:http://download.csdn.net/detail/a956959099/7631425
java 解析国密SM2算法证书
时间: 2024-10-27 18:09:39