Java-Mysql对称加解密工具类(自用保存)

本文介绍了一种使用 Java 实现 MySQL 的 AES 加密和解密方法,并提供了详细的代码实现。通过具体示例展示了如何生成密钥、进行加密及解密操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mysql 解密函数:
SELECT AES_DECRYPT(UNHEX('*****'),'*****')

Java 代码

public class AesTools {

    private AesTools() {}

        public static String encrypt(String content, String key) {
        try {
            if (StringUtils.isNotBlank(content) && StringUtils.isNotBlank(key)) {
                SecretKey aesKey = generateMySQLAESKey(strKey, "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 = "******";
        String key = "*******";
        String content = encrypt(value, key);
        System.out.println("加密后:" + content);
        System.out.println("解密后:" + decrypt(content, key));
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值