kongdeqiang
2022-12-16 daf6a95086087ec99232eea8b4648b7541881f7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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);
    }
}