xuhy
2023-08-11 3c1223bf3eb6df077b48637a7c93a93246a1a8ae
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
package com.stylefeng.guns.modular.system.util;
 
 
import com.stylefeng.guns.core.util.ToolUtil;
import org.springframework.util.Base64Utils;
 
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Base64;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 * 定义AES加密解密工具类
 */
public class AESUtil {
 
    private static final String KEY_ALGORITHM = "AES";//加密方式
 
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";//默认的加密算法
 
    private static final String KEY = "xqT86jictTPpHMem";//密码
 
    private static final String IV_PARAMETER = "xqT86jicxqT86jic";//偏移量
 
    private static final String CHARSET = "UTF-8";//编码
 
 
 
 
    /**
     * 加密操作
     * @param content  待加密内容
     * @return
     * @throws Exception
     */
    public static String encrypt(String content) {
        try {
            if(ToolUtil.isEmpty(content)){
                return content;
            }
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            byte[] raw = KEY.getBytes(CHARSET);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(content.getBytes(CHARSET));
            return Base64Utils.encodeToString(encrypted);
        }catch (Exception e){
            e.printStackTrace();
        }
        return content;
    }
 
    /**
     *解密操作
     * @param content 待解密内容
     * @return
     * @throws Exception
     */
    public static String decrypt(String content) {
        try {
            if(ToolUtil.isEmpty(content)){
                return content;
            }
            byte[] raw = KEY.getBytes(CHARSET);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
 
            byte[] encrypted1 = Base64Utils.decodeFromString(content);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, CHARSET);
            return originalString;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return content;
    }
 
 
    public static void main(String[] ages){
//        String encrypt = AESUtil.encrypt("19167181339");
//        System.err.println(encrypt);
//        String travel = AESUtil.decrypt("43um1VRZ1t1wL3Z9IWUV7e4Z/w29owZM");
//        System.err.println(travel);
 
        String decrypt = DESUtil.decrypt("xqT86jictTPpHMem", "43um1VRZ1t1wL3Z9IWUV7e4Z/w29owZM");
        System.err.println(decrypt);
    }
}