1
13693261870
2022-09-16 fee60c3e25fac0982f3b8cb8feea7225c4ed22f8
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
package com.terra.proxy.util;
 
 
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.util.Base64;
 
 
/**
 * 基础加密组件
 *
 * @author TanBin
 */
public abstract class CoderUtils {
    public static final String KEY_SHA = "SHA";
    public static final String KEY_MD5 = "MD5";
 
    /**
     * MAC算法可选以下多种算法
     * <pre>
     * HmacMD5
     * HmacSHA1
     * HmacSHA256
     * HmacSHA384
     * HmacSHA512
     * </pre>
     */
    public static final String KEY_MAC = "HmacMD5";
 
    /**
     * BASE64加密
     *
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(byte[] data) {
        return Base64.getEncoder().encodeToString(data);
    }
 
    /**
     * BASE64解密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptBASE64(String key) {
        return Base64.getDecoder().decode(key);
    }
 
    /**
     * MD5加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] encryptMD5(byte[] data) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
        md5.update(data);
        return md5.digest();
    }
 
    /**
     * 蓝图的MD5加密算法
     *
     * @param text
     * @return
     * @throws Exception
     */
    public static String lantuEncryptMD5(String text) throws Exception {
        byte[] b = encryptMD5(text.getBytes("UTF-8"));
        String key = convertToHexString(b);
        //转为大写
        key = key.toUpperCase();
        //倒序
        char[] array = key.toCharArray();
        String reverseKey = "";
        for (int i = array.length - 1; i >= 0; i--) {
            reverseKey += array[i];
        }
        return reverseKey;
    }
 
    /**
     * 字节数组转为32位字符串
     *
     * @param data
     * @return
     */
    private static String convertToHexString(byte data[]) {
        StringBuffer strBuffer = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            String haxHex = Integer.toHexString(data[i] & 0xFF);
            if (haxHex.length() < 2) {
                strBuffer.append("0");
            }
            strBuffer.append(haxHex);
        }
        return strBuffer.toString();
    }
 
    /**
     * SHA加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] encryptSHA(byte[] data) throws Exception {
        MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
        sha.update(data);
        return sha.digest();
    }
 
    /**
     * 初始化HMAC密钥
     *
     * @return
     * @throws Exception
     */
    public static String initMacKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
        SecretKey secretKey = keyGenerator.generateKey();
        return encryptBASE64(secretKey.getEncoded());
    }
 
    /**
     * HMAC加密
     *
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
        SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        return mac.doFinal(data);
    }
 
    public static void main(String[] args) {
        String inputStr = "123456";
        System.err.println("原文:" + inputStr);
        try {
            String codeBASE64 = encryptBASE64(inputStr.getBytes("UTF-8"));
            String codeMD5 = lantuEncryptMD5(inputStr);
            byte[] codeSHA = encryptSHA(inputStr.getBytes("UTF-8"));
 
            String key = initMacKey();
            System.err.println("Mac密钥:" + key);
            byte[] codeHMAC = encryptHMAC(inputStr.getBytes("UTF-8"), key);
 
            System.err.println("\nBASE64:" + codeBASE64);
            System.err.println("BASE64解密:" + new String(decryptBASE64(codeBASE64), "UTF-8"));
            System.err.println("MD5:" + codeMD5);
            System.err.println("SHA:" + codeSHA);
            System.err.println("HMAC:" + codeHMAC);
 
        } catch (Exception e) {
 
            e.printStackTrace();
        }
    }
}