JAVA实现MYSQL AES加解密

package com.arvato.report.common.util;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;

public class AESMySQLUtil {

    private AESMySQLUtil() {}

        public static String encrypt(String content, String key) {
        try {
            if (StringUtils.isNotBlank(content) && StringUtils.isNotBlank(key)) {
                SecretKey aesKey = generateMySQLAESKey(key, "ASCII");
                Cipher cipher = Cipher.getInstance("AES");
                cipher.init(Cipher.ENCRYPT_MODE, aesKey);
                byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
                byte[] cipherBytes = cipher.doFinal(contentBytes);
                return Hex.encodeHexString(cipherBytes).toUpperCase();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public static String decrypt(String content, String key) {
        try {
            if (StringUtils.isNotBlank(content) && StringUtils.isNotBlank(key)) {
                SecretKey aesKey = generateMySQLAESKey(key, "ASCII");
                Cipher cipher = Cipher.getInstance("AES");
                cipher.init(Cipher.DECRYPT_MODE, aesKey);
                byte[] contentBytes = Hex.decodeHex(content.toCharArray());
                byte[] cipherBytes = cipher.doFinal(contentBytes);
                return new String(cipherBytes, StandardCharsets.UTF_8);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public static SecretKeySpec generateMySQLAESKey(final String key, final String encoding) {
        try {
            final byte[] keyBytes = new byte[16];
            int i = 0;
            for (byte b : key.getBytes(encoding))
                keyBytes[i++ % 16] ^= b;
            return new SecretKeySpec(keyBytes, "AES");
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    private static ArrayList<List<Integer>> splitArray(ArrayList<Integer> arrayList, int subNum) {
        int counter = arrayList.size() % subNum == 0 ? arrayList.size() / subNum : arrayList.size() / subNum + 1;
        ArrayList<List<Integer>> arrayLists = new ArrayList<>();
        for(int i = 0; i < counter; i++) {
            int index = i * subNum;
            List<Integer> tempList = new ArrayList<>();
            int j = 0;
            while(j < subNum && index < arrayList.size()) {
                tempList.add(arrayList.get(index++));
                j++;
            }
            arrayLists.add(tempList);
        }
        return arrayLists;
    }


    public static void main(String[] args) {
        String value = "70B7D5C6478B70ECBB1F4853674482D867C4F033FDD221D6C8053C840B00BBAA0D9133814917F5E38C4F3EF026352887F5BA1D7681D32056B7FFF82D045B104766CAB1A7782DDC3DA13B4CE022CED5F6EE77D8C645A36B00B4F7349EE6068453F30EA8865C64044E1A89A9227749D3812049D0731F98EE15D7DEB01692AE1BFD772652D388C85993CBB66B1D09452C42ACEA107B904CDEC253D92F4AA40BE006CCB053AB4E9FF68B10D6082EC319F116D1A0F07E250FECE47EF43D134DE153DD";
        String key = "xxxx";
        //String content = encrypt(value, key);
       // System.out.println("加密后:" + content);
        System.out.println("解密后:" + decrypt(value, key));
    }
}
评论