package com.qs.mp.common.utils;

import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.qs.mp.common.exception.ServiceException;
import org.springframework.stereotype.Component;

/**
 * AES加密工具类
 * @author Cup
 * @date 2022/6/6
 */
@Component
public class AESUtil {


    private static String key = "oZODJRrW2h3txLey";
    /**
     * AES加密
     * @param str   加密字符串
     * @return
     */
    public static String encrypt(String str) {
        try {
            SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key.getBytes());
            return aes.encryptHex(str);
        } catch (Exception e) {
            throw new ServiceException("AES加密异常，str:" + str, e);
        }
    }


    /**
     * AES解密
     * @param str   加密字符串
     * @return
     */
    public static String decrypt(String str) {
        try {
            SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key.getBytes());
            return aes.decryptStr(str);
        } catch (Exception e) {
            throw new ServiceException("AES解密异常，str:" + str, e);

        }
    }


}
