Bang Hu
2 天以前 2b0b64182263d922b946ec898070e59b602382dc
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
package com.webmanage.util;
 
import com.webmanage.common.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
 
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 
/**
 * AES加密工具类
 *
 * @author August
 * @date 2022-07-29
 */
@Slf4j
public class AESUtil {
 
    private AESUtil() {
    }
 
    /**
     * AES_KEY
     */
    private static final String AES_KEY = "sD6i0YYW1a5FnzF6vRf13VtHocZabRcI";
 
    /**
     * GCM_IV
     */
    private static final String GCM_IV = "1SzB4YR0c0E9";
 
    /**
     * GCM_TAG长度
     */
    private static final int GCM_TAG_LENGTH = 16;
 
    /**
     * 加密
     *
     * @param plaintext 待加密文本
     * @param key       AES_KEY
     * @param iv        GCM_IV
     * @return 密文
     */
    public static String encrypt(String plaintext, byte[] key, byte[] iv) {
        if (StringUtils.isBlank(plaintext) || key == null || iv == null) {
            throw new BusinessException("加密参数不能为空");
        }
 
        try {
            Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
 
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
 
            GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv);
 
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec);
 
            // Perform Encryption
            byte[] cipherText = cipher.doFinal(plaintext.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : cipherText) {
                String hex = Integer.toHexString(b & 0xFF);
                if (hex.length() == 1) {
                    hex = '0' + hex;
                }
                sb.append(hex);
            }
            return sb.toString();
        } catch (Exception e) {
            throw new BusinessException("加密失败");
        }
    }
 
    /**
     * 解密
     *
     * @param cipherText 密文
     * @param key        AES_KEY
     * @param iv         GCM_IV
     * @return 解密结果
     */
    public static String decrypt(String cipherText, byte[] key, byte[] iv) {
        if (StringUtils.isBlank(cipherText) || key == null || iv == null) {
            throw new BusinessException("解密参数不能为空");
        }
 
        try {
            byte[] cipherTextByte = new byte[cipherText.length() / 2];
            for (int i = 0; i < cipherText.length() / 2; i++) {
                int high = Integer.parseInt(cipherText.substring(i * 2, i * 2 + 1),
                        16);
                int low = Integer.parseInt(
                        cipherText.substring(i * 2 + 1, i * 2 + 2), 16);
                cipherTextByte[i] = (byte) (high * 16 + low);
            }
 
            Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
 
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
 
            GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv);
 
            cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);
 
            byte[] decryptedText = cipher.doFinal(cipherTextByte);
 
            return new String(decryptedText);
        } catch (Exception e) {
            throw new BusinessException("解密失败");
        }
    }
 
    /**
     * 使用默认密钥和IV进行解密
     * @param cipherText 以十六进制字符串表示的密文
     * @return 明文
     */
    public static String decryptWithDefaultKey(String cipherText) {
        return decrypt(cipherText, AES_KEY.getBytes(), GCM_IV.getBytes());
    }
 
    public static void main(String[] args) {
        // String plainText = "zynlpt2024";
        String plainText = "postgres";
 
        log.info("Original Text : " + plainText);
 
        String cipherText = encrypt(plainText, AES_KEY.getBytes(), GCM_IV.getBytes());
        log.info("Encrypted Text : " + cipherText);
 
        String decryptedText = decrypt(cipherText, AES_KEY.getBytes(), GCM_IV.getBytes());
        log.info("DeCrypted Text : " + decryptedText);
    }
}