sinake
6 天以前 cb01a55fa3d8883edff7837f7d87a098e39b0ccb
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
package com.ruoyi.common.utils.sign;
 
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
 
public class AesUtil {
 
    private static final String KEY = "QfOpO2026@SecretKey#Aes256!00001";
    private static final String IV = "1234567890123456";
 
    public static String decrypt(String cipherText) {
        String data = "";
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            byte[] original = cipher.doFinal(Base64.getDecoder().decode(cipherText));
            data = new String(original);
        } catch (Exception e) {
            String m=e.getMessage();
        }
        return data;
    }
 
    public static String encrypt(String data) {
        String text = "";
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY.getBytes(), "AES"), new IvParameterSpec(IV.getBytes()));
            byte[] encryptedBytes = cipher.doFinal(data.getBytes());
            text = Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            String m = e.getMessage();
        }
        return text;
    }
}