|
|
@@ -0,0 +1,77 @@
|
|
|
+package com.zhongshu.payment.server.core.utils;
|
|
|
+
|
|
|
+import cn.hutool.crypto.SmUtil;
|
|
|
+import cn.hutool.crypto.asymmetric.SM2;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.bouncycastle.asn1.gm.GMNamedCurves;
|
|
|
+import org.bouncycastle.asn1.x9.X9ECParameters;
|
|
|
+import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
|
|
+import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
|
|
|
+import org.bouncycastle.crypto.params.ECDomainParameters;
|
|
|
+import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
|
|
|
+import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
|
|
+import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
|
|
+import org.bouncycastle.jcajce.provider.digest.SM3;
|
|
|
+import org.bouncycastle.jce.ECNamedCurveTable;
|
|
|
+import org.bouncycastle.jce.interfaces.ECPrivateKey;
|
|
|
+import org.bouncycastle.jce.interfaces.ECPublicKey;
|
|
|
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
+import org.bouncycastle.jce.spec.ECParameterSpec;
|
|
|
+import org.bouncycastle.math.ec.ECPoint;
|
|
|
+import org.bouncycastle.util.encoders.Hex;
|
|
|
+
|
|
|
+import javax.crypto.spec.DESKeySpec;
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.security.*;
|
|
|
+import java.security.spec.X509EncodedKeySpec;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author TRX
|
|
|
+ * @date 2024/9/2
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class Test1 {
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public static void main(String[] args) {
|
|
|
+ Security.addProvider(new BouncyCastleProvider());
|
|
|
+ String publicKeyString = "GAhPWQ8D4hXanneneaydaHYHiwn64p7y3A46Jpss87aKWsy5";
|
|
|
+ String data = "hello word";
|
|
|
+
|
|
|
+ Security.addProvider(new BouncyCastleProvider());
|
|
|
+ KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
|
|
|
+ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("sm2p256v1");
|
|
|
+ keyPairGenerator.initialize(ecSpec, new SecureRandom());
|
|
|
+ KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
|
|
+ ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
|
|
|
+ ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
|
|
|
+ String base64Pub = Base64.getEncoder().encodeToString(publicKey.getEncoded());
|
|
|
+
|
|
|
+ log.info("pub: {} {}",base64Pub, new String(publicKey.getEncoded()));
|
|
|
+ log.info("pub: {}", Base64.getEncoder().encodeToString(privateKey.getEncoded()));
|
|
|
+
|
|
|
+ Signature sig = Signature.getInstance("SM3WithSM2", BouncyCastleProvider.PROVIDER_NAME);
|
|
|
+ sig.initSign(privateKey);
|
|
|
+ sig.update(data.getBytes());
|
|
|
+ byte[] signatureValue = sig.sign();
|
|
|
+
|
|
|
+ // 解码公钥字符串
|
|
|
+ byte[] publicKeyBytes = Base64.getDecoder().decode(base64Pub);
|
|
|
+ // 构造公钥对象
|
|
|
+ X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
|
|
|
+ BouncyCastleProvider BC = new BouncyCastleProvider();
|
|
|
+ KeyFactory keyFactory = KeyFactory.getInstance("EC", BC);
|
|
|
+ PublicKey publicKey1 = keyFactory.generatePublic(keySpec);
|
|
|
+
|
|
|
+
|
|
|
+ Signature signature = Signature.getInstance("SM3withSM2", "BC");
|
|
|
+ signature.initVerify(publicKey1);
|
|
|
+ signature.update(data.getBytes());
|
|
|
+ boolean isValid = signature.verify(signatureValue);
|
|
|
+ log.info("isValid: {}", isValid);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|