liulin
2024-07-30 ce04dfcdd664df7e791a63800cab2cd2d12e878c
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
package com.lunhan.xxx.common.security;
 
import com.lunhan.xxx.common.enums.ELogger;
import com.lunhan.xxx.common.util.LoggerUtil;
import org.slf4j.Logger;
 
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.UUID;
 
 
public final class Des3Util {
    private Des3Util() {
        throw new IllegalStateException("Utility class");
    }
 
    private static final Logger sysLogger = LoggerUtil.get(ELogger.SYS_ERROR);
 
    private static final String TITLE = "Des3加密工具";
 
    //默认加密偏移量
    private static byte[] DEFAULT_IV = { 0x23, (byte)0xf2, (byte)0xa3, (byte)0xc6, 0x3e, 0x2b, (byte)0xe7, 0x28 };
    //加密算法
    private static final String ALGORITHM = "DESede";
    // 加解密统一使用的编码方式
    private static final String DEFAULT_ENCODING = "utf-8";
    //密钥
    public static final String DEFAULT_KEY = "com.ctrip.tour.cq.soa.v1";
 
    /**
     * 生成加密偏移量
     * @param iv 偏移量
     */
    private static IvParameterSpec IvGenerator(byte[] iv) throws Exception {
        IvParameterSpec result = new IvParameterSpec(iv);
        return result;
    }
 
    /**
     * 随机生成一个key
     */
    public static String genKey() throws NoSuchAlgorithmException {
        byte[] keyBytes = UUID.randomUUID().toString().getBytes();
        KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
        generator.init(new SecureRandom( keyBytes ) );
        SecretKey key = generator.generateKey();
        return Base64Util.encodeStream(key.getEncoded());
    }
    /**
     * 根据明文字符串生成key(key长度必须 大于等于 24)
     */
    public static Key genKey(String key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
        DESedeKeySpec spec = new DESedeKeySpec(key.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(ALGORITHM);
        return keyfactory.generateSecret(spec);
    }
 
    /**
     * 加密以byte[]明文输入,byte[]密文输出
     * @param buffer 待加密的字节码
     * @param key 密钥(key长度必须 大于等于 24)
     * @return 加密后的字节码
     */
    private static byte[] encrypt(byte[] buffer, String key) {
        byte[] result = null;
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM +"/CBC/PKCS5Padding");
            IvParameterSpec IVSpec = IvGenerator(DEFAULT_IV);
            cipher.init(Cipher.ENCRYPT_MODE, genKey(key), IVSpec);
            byte[] temp = cipher.doFinal(buffer);
            result = Base64Util.encodeToStream(temp);
        } catch (Exception e) {
            sysLogger.error("Des3Util.encrypt", e);
        }
        return result;
    }
    /**
     * 解密以byte[]密文输入,以byte[]明文输出
     * @param buffer 待解密的字节码
     * @param key 密钥(key长度必须是8的倍数)
     * @return 解密后的字节码
     */
    private static byte[] decrypt(byte[] buffer, String key) {
        byte[] result = null;
        try {
            IvParameterSpec IVSpec = IvGenerator(DEFAULT_IV);
            Cipher cipher = Cipher.getInstance(ALGORITHM +"/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, genKey(key), IVSpec);
            byte[] temp = Base64Util.decodeToStream(buffer);
            result = cipher.doFinal(temp);
        } catch (Exception e) {
            sysLogger.error("Des3Util.decrypt", e);
        }
        return result;
    }
 
    /**
     * 加密String 明文输入密文输出
     * @param str 待加密的明文
     * @param key 密钥(key长度必须 大于等于 24)
     * @return 加密后的字符串
     */
    public static String encrypt(String str, String key) throws Exception {
        byte[] buffer = str.getBytes(Charset.forName(DEFAULT_ENCODING));
        byte[] result = encrypt(buffer, key);
        return new String(result);
    }
    /**
     * 解密String 以密文输入明文输出
     * @param str 需要解密的字符串
     * @param key 密钥(key长度必须 大于等于 24)
     * @return 解密后的字符串
     */
    public static String decrypt(String str, String key) {
        byte[] buffer = str.getBytes(Charset.forName(DEFAULT_ENCODING));
        byte[] result = decrypt(buffer, key);
        return new String(result).trim();
    }
 
    /**
     * 加密String 明文输入密文输出
     * @param str 待加密的明文
     * @return 加密后的字符串
     */
    public static String encrypt(String str) throws Exception {
        return encrypt(str, DEFAULT_KEY);
    }
    /**
     * 解密String 以密文输入明文输出
     * @param str 需要解密的字符串
     * @return 解密后的字符串
     */
    public static String decrypt(String str) {
        return decrypt(str, DEFAULT_KEY);
    }
}