当前位置: 首页 > news >正文

SM4加密算法

    SM4加密算法

/*** SM4加密工具类* SM4算法是一种分组密码算法,分组长度为128位,密钥长度为128位*/
public class SM4Util {static {// 加入BouncyCastle支持Security.addProvider(new BouncyCastleProvider());}// 算法名称public static final String ALGORITHM_NAME = "SM4";// 加密算法/分组加密模式/填充方式public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";public static final String ALGORITHM_NAME_CBC_PADDING = "SM4/CBC/PKCS5Padding";// 密钥长度,128位public static final int KEY_SIZE = 128;/*** 生成ECB模式的密钥* @return 16位密钥* @throws Exception 异常*/public static String generateKey() throws Exception {KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);kg.init(KEY_SIZE, new SecureRandom());return ByteUtils.toHexString(kg.generateKey().getEncoded());}/*** 生成CBC模式的初始化向量* @return 16位初始化向量*/public static String generateIV() {SecureRandom random = new SecureRandom();byte[] iv = new byte[16];random.nextBytes(iv);return ByteUtils.toHexString(iv);}/*** ECB模式加密* @param plainText 明文* @param key 密钥(16位十六进制字符串)* @return 加密后的Base64字符串* @throws Exception 异常*/public static String encryptEcb(String plainText, String key) throws Exception {// 将密钥转换为字节数组byte[] keyData = ByteUtils.fromHexString(key);// 初始化密钥Key secretKey = new SecretKeySpec(keyData, ALGORITHM_NAME);// 实例化加密器Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_ECB_PADDING, BouncyCastleProvider.PROVIDER_NAME);cipher.init(Cipher.ENCRYPT_MODE, secretKey);// 加密byte[] encryptedData = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));// 返回Base64编码结果return Base64.getEncoder().encodeToString(encryptedData);}/*** ECB模式解密* @param cipherText 加密后的Base64字符串* @param key 密钥(16位十六进制字符串)* @return 解密后的明文* @throws Exception 异常*/public static String decryptEcb(String cipherText, String key) throws Exception {// 解码Base64byte[] encryptedData = Base64.getDecoder().decode(cipherText);// 将密钥转换为字节数组byte[] keyData = ByteUtils.fromHexString(key);// 初始化密钥Key secretKey = new SecretKeySpec(keyData, ALGORITHM_NAME);// 实例化解密器Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_ECB_PADDING, BouncyCastleProvider.PROVIDER_NAME);cipher.init(Cipher.DECRYPT_MODE, secretKey);// 解密byte[] decryptedData = cipher.doFinal(encryptedData);return new String(decryptedData, StandardCharsets.UTF_8);}/*** CBC模式加密* @param plainText 明文* @param key 密钥(16位十六进制字符串)* @param iv 初始化向量(16位十六进制字符串)* @return 加密后的Base64字符串* @throws Exception 异常*/public static String encryptCbc(String plainText, String key, String iv) throws Exception {// 将密钥和IV转换为字节数组byte[] keyData = ByteUtils.fromHexString(key);byte[] ivData = ByteUtils.fromHexString(iv);// 初始化密钥和IVKey secretKey = new SecretKeySpec(keyData, ALGORITHM_NAME);IvParameterSpec ivParameterSpec = new IvParameterSpec(ivData);// 实例化加密器Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_CBC_PADDING, BouncyCastleProvider.PROVIDER_NAME);cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);// 加密byte[] encryptedData = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedData);}/*** CBC模式解密* @param cipherText 加密后的Base64字符串* @param key 密钥(16位十六进制字符串)* @param iv 初始化向量(16位十六进制字符串)* @return 解密后的明文* @throws Exception 异常*/public static String decryptCbc(String cipherText, String key, String iv) throws Exception {// 解码Base64byte[] encryptedData = Base64.getDecoder().decode(cipherText);// 将密钥和IV转换为字节数组byte[] keyData = ByteUtils.fromHexString(key);byte[] ivData = ByteUtils.fromHexString(iv);// 初始化密钥和IVKey secretKey = new SecretKeySpec(keyData, ALGORITHM_NAME);IvParameterSpec ivParameterSpec = new IvParameterSpec(ivData);// 实例化解密器Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_CBC_PADDING, BouncyCastleProvider.PROVIDER_NAME);cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);// 解密byte[] decryptedData = cipher.doFinal(encryptedData);return new String(decryptedData, StandardCharsets.UTF_8);}
}

http://www.dtcms.com/a/354184.html

相关文章:

  • Karatsuba
  • 前端工程化与AI融合:构建智能化开发体系
  • 4-4.Python 数据容器 - 字典 dict(字典 dict 概述、字典的定义与调用、字典的遍历、字典的常用方法)
  • CPU 虚拟化之Cpu Models
  • 代码随想录刷题Day43
  • 时间轮定时器HashedWheelTimer
  • WSL设置静态IP
  • window程序打包
  • Libvio网站与客户端访问故障排查指南(专业版)
  • 什么是低空经济?
  • JMeter 5.3 性能测试:文件下载脚本编写与导出文件接收完整指南
  • QT鼠标事件中的QMouseEvent :e
  • 深度学习---卷积神经网络CNN
  • PLC_博图系列☞基本指令”S_ODT:分配接通延时定时器参数并启动“
  • HTML5超详细学习内容
  • 程序(进程)地址空间(1)
  • 基于MATLAB/Simulink的单机带负荷仿真系统搭建
  • LeetCode-23day:技巧经典
  • 疯狂星期四文案网第52天运营日记
  • 野火STM32Modbus主机读取寄存器/线圈失败(二)-解决CRC校验错误
  • 让ai写一个类github首页
  • Web前端之JavaScript时间体系全解析、performance、Date、now
  • Go语言循环性能终极对决:for vs range 深度剖析
  • 如何用Postman做接口测试?
  • k8s中的服务(Service),详细列举
  • JavaSE:类和对象2
  • Redis集群介绍——主从、哨兵、集群
  • 单兵图传设备如何接入指挥中心平台?国标GB/T28181协议的20位ID有何含义?如何进行配置?
  • [手写系列]Go手写db — — 第二版
  • spring-boot-test与 spring-boot-starter-test 区别