jinpengyong
2021-09-29 d7b233b6ed1793255adee6ed1e0cbe950e6c7e9f
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
package com.moral.util;
 
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.util.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
/**
 * @ClassName AESUtil
 * @Description AES工具类
 * @Author 陈凯裕
 * @Date 2021/3/9 14:25
 * @Version TODO
 **/
@Slf4j
@Component
public class AESUtils {
    //密钥
    public static String key ;
    //字符集
    private static String charset = "utf-8";
    // 偏移量
    private static int offset = 16;
    //AES种类
    private static String transformation = "AES/CBC/PKCS5Padding";
    private static String algorithm = "AES";
 
 
    @Value("${AES.KEY}")
    public  void setKey(String key) {
        AESUtils.key = key;
    }
 
 
 
    //加密
    public static String encrypt(String content) {
        return encrypt(content, key);
    }
 
    //解密
    public static String decrypt(String content) {
        return decrypt(content, key);
    }
 
    //加密
    public static String encrypt(String content, String key) {
        try {
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
            Cipher cipher = Cipher.getInstance(transformation);
            byte[] byteContent = content.getBytes(charset);
            cipher.init(Cipher.ENCRYPT_MODE, skey, iv);// 初始化
            byte[] result = cipher.doFinal(byteContent);
            return new Base64().encodeToString(result); // 加密
        } catch (Exception e) {
            log.error("Encryption failed!");
            e.printStackTrace();
        }
        return null;
    }
 
    //解密
    public static String decrypt(String content, String key) {
        try {
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.DECRYPT_MODE, skey, iv);// 初始化
            byte[] result = cipher.doFinal(new Base64().decode(content));
            return new String(result); // 解密
        } catch (Exception e) {
            log.error("Decryption failed!");
            e.printStackTrace();
        }
        return null;
    }
 
    public static void main(String[] args) {
       // System.out.println(encrypt("123456","AD42F7787B035B7580000EF93BE20BAD"));
        //123456 KoWjfDMZQhJMLlG1crBPqQ==
        String str = decrypt("KoWjfDMZQhJMLlG1crBPqQ==", "AD42F7787B035B7580000EF93BE20BAD");
        System.out.println(str);
    }
 
}