java sm2证书签名验证
时间: 2023-10-29 07:05:23 浏览: 174
SM2加密工具类前后端代码
以下是Java代码示例,用于使用SM2证书对数据进行签名和验证:
/**
* SM2证书签名和验证示例
*/
import java.security.Security;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.asn1.x509.X509CertificateStructure;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.signers.SM2Signer;
public class SM2CertSignDemo {
public static void main(String[] args) {
Security.addProvider(new BouncyCastleProvider());
try {
// 从证书获取SM2公钥
X509Certificate cert = loadCertificate("test-cert.pem");
PublicKey publicKey = cert.getPublicKey();
byte[] encodedPublicKey = publicKey.getEncoded();
SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(encodedPublicKey);
ECPublicKeyParameters ecPublicKeyParameters = new ECPublicKeyParameters(subjectPublicKeyInfo.getPublicKeyData().getBytes(), SM2CurveUtils.SM2_CURVE);
// 从密钥文件获取SM2私钥
byte[] privateKeyBytes = loadPrivateKeyBytes("test-key.pem");
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
byte[] encodedPrivateKey = privateKey.getEncoded();
ECPrivateKeyParameters ecPrivateKeyParameters = new ECPrivateKeyParameters(SM2CurveUtils.SM2_CURVE.decodeScalar(encodedPrivateKey), SM2CurveUtils.SM2_CURVE);
// 签名示例
byte[] data = "Hello, world!".getBytes("UTF-8");
SM2Signer sm2Signer = new SM2Signer();
sm2Signer.init(true, ecPrivateKeyParameters);
sm2Signer.update(data, 0, data.length);
byte[] signature = sm2Signer.generateSignature();
System.out.println("Signature: " + bytesToHexString(signature));
// 验证签名示例
sm2Signer.init(false, ecPublicKeyParameters);
sm2Signer.update(data, 0, data.length);
boolean valid = sm2Signer.verifySignature(signature);
System.out.println("Valid: " + valid);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 从证书文件加载X.509证书
*/
private static X509Certificate loadCertificate(String certFile) throws Exception {
byte[] certBytes = Files.readAllBytes(Paths.get(certFile));
X509CertificateHolder certHolder = new X509CertificateHolder(certBytes);
X509CertificateStructure certStructure = certHolder.toASN1Structure();
X509Certificate cert = new JcaX509CertificateConverter().getCertificate(certStructure);
return cert;
}
/**
* 从密钥文件加载PKCS#8格式私钥
*/
private static byte[] loadPrivateKeyBytes(String keyFile) throws Exception {
byte[] keyBytes = Files.readAllBytes(Paths.get(keyFile));
String keyString = new String(keyBytes, "UTF-8");
keyString = keyString.replace("-----BEGIN PRIVATE KEY-----\n", "");
keyString = keyString.replace("-----END PRIVATE KEY-----\n", "");
keyString = keyString.replaceAll("\\s+", "");
return Base64.decode(keyString);
}
/**
* 将字节数组转换为16进制字符串
*/
private static String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(String.format("%02X", bytes[i]));
}
return sb.toString();
}
}
阅读全文