xiejun
2024-11-01 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 *      Copyright (c) 2018-2028, DreamLu All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: DreamLu 卢春梦 (596392912@qq.com)
 */
package org.springblade.core.tool.utils;
 
import org.springframework.lang.Nullable;
 
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.util.Objects;
 
/**
 * DES加解密处理工具
 *
 * @author L.cm
 */
public class DesUtil {
    /**
     * 数字签名,密钥算法
     */
    public static final String DES_ALGORITHM = "DES";
 
    /**
     * 生成 des 密钥
     *
     * @return 密钥
     */
    public static String genDesKey() {
        return StringUtil.random(16);
    }
 
    /**
     * DES加密
     *
     * @param data     byte array
     * @param password 密钥
     * @return des hex
     */
    public static String encryptToHex(byte[] data, String password) {
        return HexUtil.encodeToString(encrypt(data, password));
    }
 
    /**
     * DES加密
     *
     * @param data     字符串内容
     * @param password 密钥
     * @return des hex
     */
    @Nullable
    public static String encryptToHex(@Nullable String data, String password) {
        if (StringUtil.isBlank(data)) {
            return null;
        }
        byte[] dataBytes = data.getBytes(Charsets.UTF_8);
        return encryptToHex(dataBytes, password);
    }
 
    /**
     * DES解密
     *
     * @param data     字符串内容
     * @param password 密钥
     * @return des context
     */
    @Nullable
    public static String decryptFormHex(@Nullable String data, String password) {
        if (StringUtil.isBlank(data)) {
            return null;
        }
        byte[] hexBytes = HexUtil.decode(data);
        return new String(decrypt(hexBytes, password), Charsets.UTF_8);
    }
 
    /**
     * DES加密
     *
     * @param data     byte array
     * @param password 密钥
     * @return des hex
     */
    public static String encryptToBase64(byte[] data, String password) {
        return Base64Util.encodeToString(encrypt(data, password));
    }
 
    /**
     * DES加密
     *
     * @param data     字符串内容
     * @param password 密钥
     * @return des hex
     */
    @Nullable
    public static String encryptToBase64(@Nullable String data, String password) {
        if (StringUtil.isBlank(data)) {
            return null;
        }
        byte[] dataBytes = data.getBytes(Charsets.UTF_8);
        return encryptToBase64(dataBytes, password);
    }
 
    /**
     * DES解密
     *
     * @param data     字符串内容
     * @param password 密钥
     * @return des context
     */
    public static byte[] decryptFormBase64(byte[] data, String password) {
        byte[] dataBytes = Base64Util.decode(data);
        return decrypt(dataBytes, password);
    }
 
    /**
     * DES解密
     *
     * @param data     字符串内容
     * @param password 密钥
     * @return des context
     */
    @Nullable
    public static String decryptFormBase64(@Nullable String data, String password) {
        if (StringUtil.isBlank(data)) {
            return null;
        }
        byte[] dataBytes = Base64Util.decodeFromString(data);
        return new String(decrypt(dataBytes, password), Charsets.UTF_8);
    }
 
    /**
     * DES加密
     *
     * @param data   内容
     * @param desKey 密钥
     * @return byte array
     */
    public static byte[] encrypt(byte[] data, byte[] desKey) {
        return des(data, desKey, Cipher.ENCRYPT_MODE);
    }
 
    /**
     * DES加密
     *
     * @param data   内容
     * @param desKey 密钥
     * @return byte array
     */
    public static byte[] encrypt(byte[] data, String desKey) {
        return encrypt(data, Objects.requireNonNull(desKey).getBytes(Charsets.UTF_8));
    }
 
    /**
     * DES解密
     *
     * @param data   内容
     * @param desKey 密钥
     * @return byte array
     */
    public static byte[] decrypt(byte[] data, byte[] desKey) {
        return des(data, desKey, Cipher.DECRYPT_MODE);
    }
 
    /**
     * DES解密
     *
     * @param data   内容
     * @param desKey 密钥
     * @return byte array
     */
    public static byte[] decrypt(byte[] data, String desKey) {
        return decrypt(data, Objects.requireNonNull(desKey).getBytes(Charsets.UTF_8));
    }
 
    /**
     * DES加密/解密公共方法
     *
     * @param data   byte数组
     * @param desKey 密钥
     * @param mode   加密:{@link Cipher#ENCRYPT_MODE},解密:{@link Cipher#DECRYPT_MODE}
     * @return des
     */
    private static byte[] des(byte[] data, byte[] desKey, int mode) {
        try {
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
            Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
            DESKeySpec desKeySpec = new DESKeySpec(desKey);
            cipher.init(mode, keyFactory.generateSecret(desKeySpec), Holder.SECURE_RANDOM);
            return cipher.doFinal(data);
        } catch (Exception e) {
            throw Exceptions.unchecked(e);
        }
    }
 
}