YYC
2023-08-17 9a29c498c851a412f5f64e6b0a293f27ed469702
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.fzzy.api.utils;
 
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
 
import java.util.Base64;
/**
 
* @ClassName: AESUtils  
 
* @Description: AES 对称算法加密/解密工具类
 
* @author zhupig3028
 
* @date 2022年5月10日  
  
 
*/
public class AESUtils {
 
 
    /**
     * 加解密统一编码方式
     */
    private final static String ENCODING = "utf-8";
 
    /**
     * 加解密方式
     */
    private final static String ALGORITHM  = "AES";
    private final static String ALGORITHM_ECB  = "ECB";
    private final static int KEY_SIZE_128 = 128;
 
    /**
     *加密模式及填充方式
     */
    private final static String PATTERN = "AES/ECB/pkcs5padding";
 
    /**
     * AES加密
     * @param plainText
     * @param key
     * @return
     * @throws Exception
     */
    public static String encrypt(String plainText, String key) throws Exception {
        if (key == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
//        if (key.length() != 16) {
//            System.out.print("Key长度不是16位");
//            return null;
//        }
        SecretKey secretKey = new SecretKeySpec(key.getBytes(ENCODING), ALGORITHM);
        // AES加密采用pkcs5padding填充
        Cipher cipher = Cipher.getInstance(PATTERN);
        //用密匙初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        //执行加密操作
        byte[] encryptData = cipher.doFinal(plainText.getBytes(ENCODING));
        return Base64.getEncoder().encodeToString(encryptData);
    }
 
 
    /**
     * AES解密
     * @param plainText
     * @param key
     * @return
     * @throws Exception
     */
    public static String decrypt(String plainText, String key) throws Exception {
        SecretKey secretKey = new SecretKeySpec(key.getBytes(ENCODING), ALGORITHM);
        // 获取 AES 密码器
        Cipher cipher = Cipher.getInstance(PATTERN);
        // 初始化密码器(解密模型)
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        // 解密数据, 返回明文
        byte[] encryptData = cipher.doFinal(Base64.getDecoder().decode(plainText));
        return new String(encryptData,ENCODING);
    }
 
    /**
     * AES加密-ECB方式
     * @param plainText
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptByEcb(String plainText, String key) throws Exception {
        if (key == null) {
            System.out.print("Key为空null");
            return null;
        }
 
        // 判断Key是否为16位
//        if (key.length() != 16) {
//            System.out.print("Key长度不是16位");
//            return null;
//        }
        SecretKey secretKey = new SecretKeySpec(key.getBytes(ENCODING), ALGORITHM_ECB);
        // AES加密采用pkcs5padding填充
        Cipher cipher = Cipher.getInstance(PATTERN);
        //用密匙初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        //执行加密操作
        byte[] encryptData = cipher.doFinal(plainText.getBytes(ENCODING));
        return Base64.getEncoder().encodeToString(encryptData);
    }
 
 
}