chenjiahe
2022-06-15 11f9478b32b710e022c328972fd2e07c72a62df8
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
package com.hx.util;
 
import org.apache.commons.codec.binary.Base64;
 
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
 
/**
 * AES加密工具类
 */
public class Aes {
 
    /**
     * 将二进制转换成16进制
     *
     * @param buf 字节数组
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }
 
    /**
     * 将16进制转换为二进制
     *
     * @param hexStr 16进制字符
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
 
    /**
     * 加密
     * @param str 待加密字符串
     * @param secret 密钥
     * @return
     */
    public static byte[] encrypt(String str, String secret) {
        if (null != str) {
            byte[] bytes = null;
 
            try {
                Base64 base64 = new Base64();
                String ALGORITHM = "AES";
                SecretKey desKey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥
                Cipher c;
                c = Cipher.getInstance(ALGORITHM);
                c.init(Cipher.ENCRYPT_MODE, desKey);
                bytes = c.doFinal(str.getBytes("UTF-8"));
                return bytes;
            } catch (Exception e) {
                e.printStackTrace();
            }
 
        }
        return null;
    }
 
    /**
     * 解密
     * @param str 待解密字符串
     * @param secret 密钥
     * @return
     */
    public static byte[] decrypt(byte[] str, String secret) {
        try {
            String ALGORITHM = "AES";
            SecretKey desKey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥
            Cipher c = Cipher.getInstance(ALGORITHM);
            c.init(Cipher.DECRYPT_MODE, desKey);
            return c.doFinal(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static void main(String[] args) throws UnsupportedEncodingException {
 
        String content = "apiKey=phitabfaceapi&userId=o5W1it6Pvqn6vPX6E1BGERH8J1lw";
        String password = "phitabfacesecret";
 
        byte[] encode = encrypt(content, password);
        String code = parseByte2HexStr(encode);
        System.out.println("密文字符串:" + code);
 
        //16进制字符串转成字节数组
        byte[] decode = parseHexStr2Byte(code);
        // 解密
        byte[] decryptResult = decrypt(decode, password);
        System.out.println("解密后:" + new String(decryptResult, "UTF-8")); //不转码会乱码
 
    }
}