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));
}
}

最新文章

  1. Android基础总结(一)
  2. log4net一些配置说明
  3. akka优势
  4. eclipse +VISUALSVN SERVER 创建版本控制器,防止误操作(可视化操作,简单方便,不需要修改配置文件)
  5. lintcode:最大子正方形
  6. 在Eclipse中显示空格(space)和制表符(tab)
  7. data source 和initial catalog
  8. android入门——UI(4)
  9. [转]PT_DENY_ATTACH
  10. Lightoj1205——Palindromic Numbers(数位dp+回文数)
  11. CSS的box-sizing属性
  12. thinkjs升级到3.0后的图片上传
  13. Kubernetes外挂配置管理—ConfigMap介绍
  14. spring boot 1.4 整合 mybatis druid
  15. nginx支持android、ios、微信扫一扫
  16. 【linux】suse linux 常用命令
  17. 【Math】协方差矩阵
  18. 安装JIRA
  19. ffmpeg开发基础知识
  20. Elementary Sorts

热门文章

  1. java入门与进阶P-2.3
  2. CF1466H Finding satisfactory solutions
  3. SpringCloud 消费请求Eureka调用服务提供者报错
  4. 计算机网络基础08 Socket网络通信
  5. websocket-sharp 实现websocket
  6. jquery(二:jquery的DOM操作)
  7. ctfshow_web入门 PHP特性
  8. Postgresql清理WAL日志
  9. 自己动手从零写桌面操作系统GrapeOS系列教程——8.x86介绍
  10. mongodump导出mongodb中的数据