package com.wgcloud.util;
|
|
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
|
import cn.hutool.crypto.symmetric.SymmetricCrypto;
|
import com.wgcloud.common.ApplicationContextHelper;
|
import com.wgcloud.config.CommonConfig;
|
import org.apache.commons.lang3.StringUtils;
|
|
/**
|
* DES加密解密工具类
|
*/
|
public class DESUtil {
|
private static CommonConfig commonConfig = (CommonConfig) ApplicationContextHelper.getBean(CommonConfig.class);
|
private static SymmetricCrypto des = null;
|
|
/**
|
* 初始化des,密钥使用wgToken
|
*/
|
private static void initDes() {
|
if (null == des) {
|
//密钥(长度小于24字节自动补足,大于24取前24字节),担心wgToken长度不够,后面补20个数字
|
String wgTokenPack = commonConfig.getWgToken() + "20220913211220220913";
|
des = new SymmetricCrypto(SymmetricAlgorithm.DES, wgTokenPack.getBytes());
|
}
|
}
|
|
/**
|
* des解密
|
*
|
* @param content 密文
|
* @return 解密后的明文
|
*/
|
public static String decrypt(String content) {
|
if (StringUtils.isEmpty(content)) {
|
return "";
|
}
|
if (des == null) {
|
initDes();
|
}
|
return des.decryptStr(content);
|
}
|
|
/**
|
* des加密
|
*
|
* @param content 明文
|
* @return 加密后的密文
|
*/
|
public static String encryption(String content) {
|
if (StringUtils.isEmpty(content)) {
|
return "";
|
}
|
if (des == null) {
|
initDes();
|
}
|
return des.encryptHex(content);
|
}
|
}
|