VUE3+Springboot实现SM2完整步骤

一.VUE3代码实现

1.安装依赖

 npm install --save sm-crypto

2.导入sm2

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

3.定义公钥私钥

var privateKey = "私钥";//解密使用
var publicKey  = "公钥";//加密使用

4.设置加密模式

//cipherMode [加密模式 C1C3C2:1, C1C2C3:0]
    const cipherMode = 1;//默认是1

5.加密

 
页面代码直接@click绑定getphone即可单击实现
 
 
 
 data() {
    return {
          copyphone:'',
          phone:'123545687',
 
     }}
methods: {
    getphone(){
        const sm2 = require('sm-crypto').sm2;
        var publicKey  = "公钥";//加密使用
        var encrText = 需要加密的字段;//例如var enxrText = this.phone;
        const cipherMode = 1;
        let decryptData = sm2.doEncrypt(encrText, publicKey, cipherMode) // 加密结果
        return '04' + decryptData;//04可不要具体看后端要求
       
      
                 }
           }

6.解密

 
 
页面代码直接@click绑定getphone即可单击实现
 
 
 
 data() {
    return {
          copyphone:'',
     }}
methods: {
    getphone(){
        const sm2 = require('sm-crypto').sm2;
       
        //
        var privateKey = "私钥";
        var encrText = 需要解密的字段; 
        //有04要截 var encrText = val.substring(2);val是后台传过来的加密字段,将‘04’截取掉
        const cipherMode = 1
        let decryptData = sm2.doDecrypt(encrText, privateKey, cipherMode) // 解密结果
        return decryptData ;
        this.copyphone = decryptData;//赋值方便处理
        console.log(this.copyphone);//直接打印出来看是否实现
                 }
           }

二、springboot代码实现

1.导入maven依赖

        
            org.bouncycastle
            bcprov-jdk15on
            1.70
        
        
            cn.hutool
            hutool-all
            5.8.9
        

2.代码实现

package com.gstanzer.supervise.sm2;

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;
import java.util.Set;

public class SM2CryptUtils {


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

    //加密
    public static String encrypt(String data, String publicKey){
        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);
        return hex;

    }

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

    public static void test1() {
        Map keymap = SM2CryptUtils.createSM2Key();
        Set> entries = keymap.entrySet();
        String publickey = "";
        String privatekey = "";
        for (Map.Entry entry : entries) {
            publickey = entry.getValue();
            privatekey = entry.getKey();
        }
        System.out.println("SM2国密算法公钥:{}" + publickey);
        System.out.println("SM2国密算法私钥:{}" + privatekey);
        String data = SM2CryptUtils.encrypt("CQmgh301%",publickey);
        System.out.println("加密后数据:" + data);
        String result = SM2CryptUtils.decrypt(data,privatekey);
        System.out.println("解密后数据:" + result);
    }

    public static void test2() {

        String publickey = "04daac50ec8a61ef628f79d738a71e543dcb969a5efbb6bf8290b73be5a5a80e3d34f74fb987b237ccf8cb1930a842a21d240e22807fdc66726a0a4368bf7483c6";
        String privatekey = "339521b8cd63e0e3c83fd89f6141b085000f9e0bfad66473288d2e6851ae8b77";

        System.out.println("SM2国密算法公钥:{}" + publickey);
        System.out.println("SM2国密算法私钥:{}" + privatekey);
//        String data = SM2CryptUtils.encrypt("CQmgh301%",publickey);
        String data = "04da994c0c7b224832cfb578be69445201dda1338515b5d5dcc94e12ff414a2a9fcee9c788344a0393178a341043d19c19619df62a5fbf6b731d2d559faa7470ff38e65b8a0a2b091de65277fccd0a91777e4d20d33845abafb0653edb557c85c7a4dcc78f3b87f35b";
        System.out.println("加密后数据:" + data);
        String result = SM2CryptUtils.decrypt(data,privatekey);
        System.out.println("解密后数据:" + result);
    }

    public static void main(String[] args) {
        //test1();
        test2();
    }
}

你可能感兴趣的:(spring,boot,后端,java)