Vue+Spring boot 使用SM2加解密

spring boot端代码

1、引入依赖包

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.70</version>
</dependency>
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.9</version>
</dependency>

2、编写加解密工具类

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.ECKeyUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.SmUtil;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.signers.PlainDSAEncoding;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;

import java.util.HashMap;
import java.util.Map;

public class SM2CryptUtils {

    private static String privateKey="";
    private static String publicKey="";

    //生成秘钥对
    public static Map<String,String> createSM2Key(){
        SM2 sm2=SmUtil.sm2();
        sm2.setMode(SM2Engine.Mode.C1C3C2);               privateKey=HexUtil.encodeHexStr(BCUtil.encodeECPrivateKey(sm2.getPrivateKey()));
        publicKey = HexUtil.encodeHexStr(((BCECPublicKey)         sm2.getPublicKey()).getQ().getEncoded(false));
        Map<String,String> keys=new HashMap<>();
        keys.put(privateKey,publicKey);
        return keys;
    }

    //加密
    public static String encrypt(String data){
        String publicKeyTmp = publicKey;
        if (publicKey.length() == 130) {
            //这里需要去掉开始第一个字节 第一个字节表示标记
            publicKeyTmp = publicKey.substring(2);
        }
        String xhex = publicKeyTmp.substring(0, 64);
        String yhex = publicKeyTmp.substring(64, 128);
        ECPublicKeyParameters ecPublicKeyParameters = BCUtil.toSm2Params(xhex, yhex);
        //创建sm2 对象
        SM2 sm2 = new SM2(null, ecPublicKeyParameters);
        sm2.usePlainEncoding();
        sm2.setMode(SM2Engine.Mode.C1C3C2);
        String hex = sm2.encryptHex(data,KeyType.PublicKey).substring(2);
        return hex;

    }

    //解密
    public static String decrypt(String data){
        //ECKeyUtil.toSm2PrivateParams()
        SM2 sm2 = new SM2(ECKeyUtil.toSm2PrivateParams(privateKey), null);
        sm2.setMode(SM2Engine.Mode.C1C3C2);
        sm2.setEncoding(new PlainDSAEncoding());
        String encryptStr = sm2.decryptStr("04"+data, KeyType.PrivateKey);
        return encryptStr;
    }
}

 3、使用

SM2CryptUtils.createSM2Key();
String data = SM2CryptUtils.encrypt("test");
String result = SM2CryptUtils.decrypt(data);

Vue端代码 (用的是vue3)

1、安装依赖包

 npm install --save sm-crypto

2.创建一个sm2Utils.js的工具类(不需要引入包,sm2Utils.js的内容就是下面的几个代码)

export  const sm2Utils =(data)=> {

  let publicKey='后台生成的公钥'

  // 1 - C1C3C2,0 - C1C2C3,默认为1

  let cipherMode = 1

  let sm2 = require('sm-crypto').sm2

  return sm2.doEncrypt(data,publicKey,cipherMode)

}

3、使用,引入工具类,使用方法

import { sm2Utils } from '../utils/sm2Utils'

let data =  sm2Utils('test') 

以上就是我使用sm2的核心代码

猜你喜欢

转载自blog.csdn.net/liyayou/article/details/127982048