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