国密SM2 非对称加解密前后端工具
1.依赖
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.21</version></dependency><dependency><groupId>org.bouncycastle</groupId><artifactId>bcpkix-jdk18on</artifactId><version>1.78.1</version></dependency>
2.java工具类
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;public class Sm2Util {private static final String SM2_PUBLIC_KEY = "043bfa6294c822f76cdaf6ffaecc16a0fb49d4c8252a55944720a589c8276cc2d0d42b85e7659da7367dcaeab619e45d59793d72657eadc3e443f8f5bf9efea136";private static final String SM2_PRIVATE_KEY = "009f10794a9478e31715fe6f11fafb9a5954705a2e76ec018ad7fcac8ddd8caccc";/*** 获取SM2加密工具对象** @param privateKey 加密私钥* @param publicKey 加密公钥* @return 处理结果*/public static SM2 getSM2(String privateKey, String publicKey) {ECPrivateKeyParameters ecPrivateKeyParameters = null;ECPublicKeyParameters ecPublicKeyParameters = null;if (StringUtils.isNotBlank(privateKey)) {ecPrivateKeyParameters = BCUtil.toSm2Params(privateKey);}if (StringUtils.isNotBlank(publicKey)) {if (publicKey.length() == 130) {//这里需要去掉开始第一个字节 第一个字节表示标记publicKey = publicKey.substring(2);}String xhex = publicKey.substring(0, 64);String yhex = publicKey.substring(64, 128);ecPublicKeyParameters = BCUtil.toSm2Params(xhex, yhex);}//创建sm2 对象SM2 sm2 = new SM2(ecPrivateKeyParameters, ecPublicKeyParameters);sm2.usePlainEncoding();sm2.setMode(SM2Engine.Mode.C1C2C3);return sm2;}public static String encrypt(String data) {SM2 sm2 = getSM2(null, SM2_PUBLIC_KEY);return sm2.encryptBcd(data, KeyType.PublicKey);}public static String decrypt(String data) {SM2 sm2 = getSM2(SM2_PRIVATE_KEY, null);return StrUtil.utf8Str(sm2.decryptFromBcd(data, KeyType.PrivateKey));}public static JSONObject genSm2Keys() {SM2 sm2 = SmUtil.sm2();byte[] privateKeyByte = BCUtil.encodeECPrivateKey(sm2.getPrivateKey());//这里公钥不压缩 公钥的第一个字节用于表示是否压缩 可以不要byte[] publicKeyByte = ((BCECPublicKey) sm2.getPublicKey()).getQ().getEncoded(false);String privateKey = HexUtil.encodeHexStr(privateKeyByte);System.out.println("私钥:" + privateKey);String publicKey = HexUtil.encodeHexStr(publicKeyByte);System.out.println("公钥:" + publicKey);return new JSONObject().set("privateKey", privateKey).set("publicKey", publicKey);}
}
3 vue+ts工具类
// src/utils/crypto.ts
import * as sm from 'sm-crypto';const publicKey = '043bfa6294c822f76cdaf6ffaecc16a0fb49d4c8252a55944720a589c8276cc2d0d42b85e7659da7367dcaeab619e45d59793d72657eadc3e443f8f5bf9efea136';
const privateKey = '009f10794a9478e31715fe6f11fafb9a5954705a2e76ec018ad7fcac8ddd8caccc';export function encryptWithSM2(data: string): string {const encrypted = sm.sm2.doEncrypt(data, publicKey, 0);return '04'+encrypted;
}export function decryptWithSM2(dataHex: string): string {dataHex = dataHex.substring(2).toLocaleLowerCase()const decrypted = sm.sm2.doDecrypt(dataHex, privateKey, 0, { output: 'string' });return decrypted;
}
参考链接:Vue + Springboot 前后端完整使用国密算法 SM2 数据加密 传输 交互 完整解决方案_国密sm2加密算法 前后端加密互通实现-CSDN客文章浏览阅读8k次,点赞5次,收藏48次。该博文介绍了如何在SpringBoot项目中实现国密SM2算法的加解密功能,包括依赖引入、加密解密方法创建、拦截器处理,以及前后端数据交互的加密流程。主要涉及SM2非对称加密和SM4对称加密,通过自定义拦截器对请求和响应数据进行加解密处理,确保数据传输的安全性。https://blog.csdn.net/yssa1125001/article/details/121208118