JAVA 【SM2】加密解密

JAVA 【SM2】加密解密

前言:最近项目中必须用到SM2的加密解密

引入的Maven依赖

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
</dependency>
<!-- SM2加密 -->
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.64</version>
</dependency>

 

一个工具类搞定!

package com.dtccd.md.biz.opof.util;


import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.security.KeyPair;
/**
 * @ClassName: SM2Util
 * @Author: yxp
 * @Data: 2023/2/23 0023 18:14
 */
@Slf4j
public class SM2Util {
    private static SM2 sm2;
    /**私钥*/
    final private static String privateKey = "xxxxxxxxxx";//自己调用下面的getSM2Key生成
    /**公钥*/
    final private static String publicKey = "xxxxxxxxx";//自己调用下面的getSM2Key生成

    private static synchronized SM2 getSm2() {
        if (sm2 == null) {
//            Environment environment = SpringBeanUtil.getApplicationContext().getEnvironment();
//            String privateKey = environment.getProperty("encrypt.sm2.privateKey");
//            String publicKey = environment.getProperty("encrypt.sm2.publicKey");
            sm2 = SmUtil.sm2(Base64.decodeBase64(privateKey), Base64.decodeBase64(publicKey));
        }
        return sm2;
    }

    /**
     * 公钥加密
     *
     * @param cipherTxt
     * @return
     */
    public static String encrypt(String cipherTxt) {
        if (!StringUtils.hasText(cipherTxt)) {
            return cipherTxt;
        }
        String encryptStr = getSm2().encryptBcd(cipherTxt, KeyType.PublicKey);
        return encryptStr;
    }

    /**
     * 私钥解密
     *
     * @param plainTxt
     * @return
     */
    public static String decrypt(String plainTxt) {

        if (!StringUtils.hasText(plainTxt)) {
            return plainTxt;
        }
        String decryptStr = StrUtil.utf8Str(getSm2().decryptFromBcd(plainTxt, KeyType.PrivateKey));
        return decryptStr;
    }

    /**
     * 生成一对 C1C2C3 格式的SM2密钥
     *
     * @return 处理结果
     */
    public static void getSM2Key() {
        KeyPair pair = SecureUtil.generateKeyPair("SM2");
        byte[] privateKey = pair.getPrivate().getEncoded();
        byte[] publicKey = pair.getPublic().getEncoded();
        try {
            System.out.println("私钥" + new String(Base64.encodeBase64(privateKey), CharsetUtil.UTF_8));
            System.out.println("公钥" + new String(Base64.encodeBase64(publicKey), CharsetUtil.UTF_8));
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage());
        }
    }

    public static void main(String[] args) {
        String name = "张三";
        String mi = encrypt(name);
        System.out.println(mi);
        System.out.println(decrypt(mi));
    }
}

 

posted @ 2023-02-24 09:34  Asura2017  阅读(4489)  评论(0编辑  收藏  举报