liding
3 天以前 7f9e375391e30fd3c367cb5a080a609a6e25e524
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
package com.zbkj.common.utils;
 
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.HashMap;
import java.util.Objects;
 
/**
 * @author chenbing
 * @company miracle
 * @date 2022/4/22   18:05
 */
@Slf4j
public class MiracleAECUtil {
 
    public static final Logger logger = LoggerFactory.getLogger(MiracleAECUtil.class);
 
    /**
     * key:必须16个字符,且要和前端保持一致
     */
    private final static String KEY = "123456789ZTTJMSF";
 
    /**
     * 偏移量:必须16个字符,且要和前端保持一致
     */
    private final static String IV = "ZTTJMSF123456789";
 
    public static void main(String[] args) {
        final String encrypt = encrypt("5");
        System.out.println("encrypt = " + encrypt);
        final String decrypt = decrypt(encrypt);
        System.out.println("decrypt = " + decrypt);
    }
 
    /**
     * 加密返回的数据转换成 String 类型
     *
     * @param content 明文
     */
    public static String encrypt(String content) {
        return parseByte2HexStr(Objects.requireNonNull(aesCbcEncrypt(content.getBytes(), KEY.getBytes(), IV.getBytes())));
    }
 
    /**
     * 将解密返回的数据转换成 String 类型
     *
     * @param content Base64编码的密文
     */
    public static String decrypt(String content) {
        return new String(Objects.requireNonNull(aesCbcDecrypt(parseHexStr2Byte(content), KEY.getBytes(), IV.getBytes())));
    }
 
 
 
    private static byte[] aesCbcEncrypt(byte[] content, byte[] keyBytes, byte[] iv) {
        try {
            SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
            //设置模式,编码,后端为PKCS5Padding,对应前端是Pkcs7
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
            return cipher.doFinal(content);
        } catch (Exception e) {
            log.info(e.toString());
        }
        return null;
    }
 
    private static byte[] aesCbcDecrypt(byte[] content, byte[] keyBytes, byte[] iv) {
        try {
            SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
            return cipher.doFinal(content);
        } catch (Exception e) {
            log.info(e.toString());
        }
        return null;
    }
 
    /**
     * 将byte数组转换成16进制String
     */
    public static String parseByte2HexStr(byte[] buf) {
        StringBuilder sb = new StringBuilder();
        for (byte b : buf) {
            String hex = Integer.toHexString(b & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }
 
    /**
     * 将16进制String转换为byte数组
     */
    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;
    }
}