JLChen
2021-11-15 44155b50cbb4f6ad78474f40331ed8838a3b0d49
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
package com.hdl.sdk.connect.utils;
 
import android.util.Base64;
 
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 
/**
 * Created by Tong on 2021/11/3.
 */
public class AESUtils {
 
    public static String encryptAES(String content, String key) throws NoSuchPaddingException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeyException {
        return encryptAES(content, key, "");
    }
 
    public static String encryptAES(String content, String key, String ivString)
            throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, UnsupportedEncodingException,
            InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        byte[] byteContent = content.getBytes("UTF-8");
        byte[] encryptedBytes = encryptAES(byteContent,key,ivString);
        return Base64.encodeToString(encryptedBytes, Base64.NO_WRAP);
    }
 
    public static byte[] encryptAES(byte[] byteContent, String key) throws NoSuchPaddingException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeyException {
        return encryptAES(byteContent, key, key);
    }
 
    public static byte[] encryptAES(byte[] byteContent, String key, String ivString)
            throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, UnsupportedEncodingException,
            InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
 
        byte[] enCodeFormat = key.getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
 
        byte[] initParam = ivString.getBytes();
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
 
        // 指定加密的算法、工作模式和填充方式
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
 
        byte[] encryptedBytes = cipher.doFinal(byteContent);
        return encryptedBytes;
    }
 
    public static String decryptAES(String content, String key) throws NoSuchPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
        return decryptAES(content, key, "");
    }
 
    public static String decryptAES(String content, String key, String ivString)
            throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidAlgorithmParameterException,
            IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
 
 
        // base64 解码
        byte[] encryptedBytes = Base64.decode(content, Base64.NO_WRAP);
 
        byte[] enCodeFormat = key.getBytes();
        SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");
 
        byte[] initParam = ivString.getBytes();
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
 
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
 
        byte[] result = cipher.doFinal(encryptedBytes);
 
        return new String(result, "UTF-8");
    }
 
}