国密算法-SM2

package com.kingxunlian.tax.utils.bcprov.detail;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import com.kingxunlian.tax.base.InvoiceLoggerFactory;
import com.kingxunlian.tax.biz.verify.dao.req.InvoiceSignRequestSign;
import com.kingxunlian.tax.utils.asnUtil.AsnUtils;
import org.bouncycastle.math.ec.ECPoint;
import org.slf4j.Logger;

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Map;


/**
 * @author leijie.gao
 * @version 1.0.0
 * @ClassName Test1.java
 * @Description TODO
 * @createTime 2021年04月08日 14:02:00
 */
public class SM2Util {

    private static final Logger logger = InvoiceLoggerFactory.getLogger(SM2Util.class);

    /**
     * SM2加签
     * @param data 内容
     * @return
     */
    public static byte[] signData(byte[] data, byte[] privateKey) {
        //需要签名的明文,得到明文对应的字节数组
        SM2 sm2 = SmUtil.sm2(privateKey, null);
        sm2.usePlainEncoding();
        byte[] signVal = sm2.sign(data, null);
        logger.info("内容:{},私钥:{},签名值:{}",Base64.encode(data),Base64.encode(privateKey),Base64.encode(signVal));
        return signVal;
    }

    /**
     * SM2验签
     * @param data 内容
     * @param publicKey 数组
     * @param signVal 签名值
     * @return
     */
    public static boolean verifyData(byte[] data,byte[] publicKey,byte[] signVal){
        SM2 sm2 = SmUtil.sm2(null, publicKey);
        sm2.usePlainEncoding();
        boolean verify = sm2.verify(data, signVal);
        logger.debug("验签结果: {}",verify);
        logger.info("内容:{},公钥:{},签名值:{},验签结果:{}",Base64.encode(data),Base64.encode(publicKey),Base64.encode(signVal),verify);
        return verify;
    }


    /**
     * SM2加密
     */
    public static byte[] encrpt(String content,byte[] publicKey){
        SM2 sm2 = SmUtil.sm2(null, publicKey);
        sm2.usePlainEncoding();
        // 公钥加密
        byte[] encryptStr = sm2.encrypt(content, KeyType.PublicKey);
        logger.info("加密结果: {}",encryptStr);
        return encryptStr;
    }

    /**
     * SM2加密
     */
    public static byte[] encrpt(byte[] content,byte[] publicKey){
        SM2 sm2 = SmUtil.sm2(null,publicKey);
        sm2.usePlainEncoding();
        // 公钥加密
        byte[] encryptStr = sm2.encrypt(content, KeyType.PublicKey);
        logger.info("加密结果: {}",encryptStr);
        return encryptStr;
    }


    /**
     * SM2解密
     */
    public static byte[] decrpt(byte[] content,byte[] privateKey){
        SM2 sm2 = SmUtil.sm2(privateKey,null);
        sm2.usePlainEncoding();
        // 私钥解密
        byte[] encrypt = sm2.decrypt(content,KeyType.PrivateKey);
        logger.info("解密结果: {}",encrypt);
        return encrypt;
    }

    /**
     * 生成SM2密钥对
     * @return
     */
    public static Map createKeyPair(){
        KeyPair pair = SecureUtil.generateKeyPair("SM2");
        PrivateKey privateKey = pair.getPrivate();
        String privateKeyStr = Base64.encode(privateKey.getEncoded());
        logger.info("生成私钥: {}", privateKeyStr);
        PublicKey publicKey = pair.getPublic();
        ECPoint ecPoint = BCUtil.toParams(publicKey).getQ();
        String publicKeyStr = Base64.encode(ecPoint.getEncoded(false));
        logger.info("生成公钥: {}",publicKeyStr);
        Map pairMap = MapUtil.newHashMap();
        pairMap.put("privateKey",privateKeyStr);
        pairMap.put("publicKey",publicKeyStr);
        return pairMap;
    }

    public static void main(String[] args) throws Exception{

        InvoiceSignRequestSign invoiceSignRequest = new InvoiceSignRequestSign();
        invoiceSignRequest.setVersion(4);
        invoiceSignRequest.setSerialNumber(new BigInteger("1387010263949119488"));
        invoiceSignRequest.setDrawerID("09923843-c379-4022-940c-e8a704b7e695");
        invoiceSignRequest.setInvoiceInfomation(HexUtil.decodeHex("be9f6e6d55ed440bba6ccf6d5e5a8877cbff577548e22ba85d993c1eef66f40869c976f3ad70fde1172230750c4ba128695ae8631ce3db0208a3056a49bf608a94e876c8735b6fcc93d4e878051567076bae9f19cdafc3e4732a41ed72ca41469c8b31020fc2fb892cefb4bd14c0bd74a130de933afbc5004afed3c300f0438bb557b53bd52f35126ae3e6e37cf8325193d8d26a2d68f81a8b7fd4a9722b0efd94aab4a96305913cfa6f8b85bf8b5802466b9b1c355706f6b3340f0a0614c29673eb38c179f701f729f5ee2c55e12962a9e48b60398a846d5a546ec9cb8a1bc62f6f969ad2fe45b4cce39c93af0d986f178c478b39177ce1cb87edd525c4f734f8976327dd996ad3a760764539846f725099b23a6a4beb351d82b496a769729337f3ec0e5ed2f432916aad5ccbd33177f8167cf48c7a7de24095aad58b92fb039802babc19be9b5923457e60b2cc8c4bd7673f62180eca8627e18a0ce22fffcad7c07a78cd6a121cd6323500d9caff12ffa09accd9145a73a0192e6ef6973b5b407fef275acf47c545a08671785dfaae6c8ca822c368498c3a8e240ab5457d4846be2e97a661377719d9f385f8abb0745be7d0a5a29c9b4f43832c74292921d70d766f7ca4e8781024622b9d7dc6fe25ea1d50214ed4506dd8b0e24447214ad72003a8046f988286c84dfe06c4840f4d6f42954f9b2976f901c721e27da5674735480bb5185ab6ebc9b10f3e6519b91124c5cc49b7978a6f1ab68f3d96e1844f8b29c180ba664d50144104f9d8848641237fc5d70dd623ceeb92170d792744d68323d6a127981f6d0c3a7fbb354212f4454dffc7faa0e40e713bfa7670acd42619dc54d7a04aa7686a8b02f600691fba379e8327a717c748b6e5c4670b7c5010ebc37d8156cb8b4e1b6eddb1c4e85da5b98d327f66952c37d9775f5863d6c3b88f7444af56616ce7e6870a3dbe2fe97f339ea0595899d308accc18816a7919f1ff5197d55da8f02078422cb1010af4c06c17de041ad762865b43737a1768994525c2e7d698749172d18cf030272d9fccf503805f146e55af98541990c6f70caeddea28599be1d96205a523a4f92ff632154f2726c150cb2d45d877a673a3aebbe54c8b62b98e775a20ee732bef1b325a8e64a628c70152ae7cb206d43a5b1b668ac1f8708a97106beed2183243630b44abe764f20d9bf8a06018c262feed5d4fa741a1343640a9c34ead876c5541d3d8f8fa475e8f279c03fd7757f3c708f4308f62568c1ada421bfd550a319ff993ac1c59ce787dc6fec125c01abfc91dd35f8bd70886b12192e30c17732bd46b7ca4c568773f62d129b71ae881ac0ffcb7e82455bbc5f0c9c9d89e02f61c1d8036f21d01e8b379e2b7337bf94ba165189166dab091d0a78811ed26bdf6420e524e20776c8aca0fd0f02e4ca79a333a1bdcf4e309be380af4eed159aed346c8a2e2c70e8913e0d1df8e5e2edcc9bf9636cfc3d73a21e041c39374ea4fb25b15196f47"));
        String s = AsnUtils.convert(invoiceSignRequest);

        String publicKey = "BNSoW/GjW1Mnb4HRaRLG4Prc41BAcymm003l4qHnxQLNC5qKWj+CEL9LvcYV1X6SPtoyVrF7A4ErwmTVR8ovRFg=";
        String sign = "BMOo8soZOTEvqBTTsGl4TC4QmO4bznJBybzBoR8PE82UxEquoOoY3Jk6Fz49Ix9sL+JZeyGphSU+ifdsVznwrntZjIy7MgeX/1w+JzWM68H6b+FIOc1lHwt4Cbh1v4tJXRtVM8w3KHn72KHGKH2jfVQ=";
        boolean c  = verifyData(Base64.decode(s),Base64.decode(publicKey),Base64.decode(sign));
        System.out.println(c);

    }



}


你可能感兴趣的:(国密算法-SM2)