引入pom依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
java SM2加解密工具类:
package com.tool; import com.google.common.base.Preconditions; import org.apache.commons.lang3.StringUtils; import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.engines.SM2Engine; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.params.ParametersWithRandom; import org.bouncycastle.crypto.signers.SM2Signer; import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.springframework.util.Base64Utils; import java.security.*; import java.security.spec.*; public class SM2Util { private static final SM2Engine.Mode DIGEST = SM2Engine.Mode.C1C3C2; private static final String ALGORITHM = "SM2"; /** * 私钥转换为 {@link ECPrivateKeyParameters} * @param key key * @return * @throws InvalidKeyException */ public static ECPrivateKeyParameters privateKeyToParams(String algorithm, byte[] key) throws InvalidKeyException, InvalidKeySpecException, NoSuchAlgorithmException { Preconditions.checkNotNull(key, "key must be not null !");