package com.das.common.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;

import java.nio.charset.Charset;

/**
 * @author xxx
 * @date 2020-09-16 11:17
 **/
@Component
public class AESUtil {

    /**
     * AES加密
     * @param content 待加密的内容
     * @return 加密后的Base64字符串
     */
//    public String encrypt(String content, String iv) {
//        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, key.getBytes(), iv.getBytes());
//        return Base64.encode(aes.encrypt(content, Charset.forName("UTF-8")));
//    }

    /**
     * AES解密
     * @param encryptStr 已加密的Base64字符串
     * @return 解密后的内容
     */
    public static String decrypt(String key, String encryptStr, String iv) {
        AES aes = new AES(Mode.CBC, Padding.ZeroPadding, key.getBytes(), iv.getBytes());
        return aes.decryptStr(Base64.decode(encryptStr), Charset.forName("UTF-8"));
    }
}
