package com.biz.utils;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.DES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import com.haier.hopm.biz.service.common.enums.ResultMsgEnum;
import org.springframework.util.Assert;
public class SafeUtil {
/**
* 加密
*
* @param content
* @param secretkey
* @return
*/
public static String desEncryp(String content, String secretkey) {
try {
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue(), secretkey.getBytes()).getEncoded();
//构建
DES des = SecureUtil.des(key);
//加密为16进制
return des.encryptHex(content);
} catch (Exception e) {
Assert.isTrue(Boolean.FALSE, ResultMsgEnum.PARAM_ERROR.toString());
}
return null;
}
/**
* 解密
*
* @param encryptHex
* @param secretkey
* @return
*/
public static String desDecryptStr(String encryptHex, String secretkey) {
try {
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue(), secretkey.getBytes()).getEncoded();
//构建
DES des = SecureUtil.des(key);
//解密为原字符串
return des.decryptStr(encryptHex);
} catch (Exception e) {
Assert.isTrue(Boolean.FALSE, ResultMsgEnum.PARAM_ERROR.toString());
}
return null;
}
public static void main(String[] args) throws Exception {
String content = "123";
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue(), "app_cGhVo5".getBytes()).getEncoded();
//构建
DES des = cn.hutool.crypto.SecureUtil.des(key);
//加密解密
byte[] encrypt = des.encrypt(content);
byte[] decrypt = des.decrypt(encrypt);
//加密为16进制,解密为原字符串
String encryptHex = des.encryptHex(content);
String decryptStr = des.decryptStr(encryptHex);
System.out.println(encryptHex);
System.out.println(decryptStr);
}
}
DES加密工具类
于 2024-06-28 13:31:59 首次发布