AES解密

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FJcsdn/article/details/88797749
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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;
import java.security.*;
import java.util.Base64;

/**
 * AES算法工具类
 */
public class AESUtil {

    private final static Logger logger = LoggerFactory.getLogger(AESUtil.class);

    /**
     * 密码算法
     */
    private final static String CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding";
    /**
     * Key算法
     */
    private final static String KEY_ALGORITHM = "AES";

    /**
     * 密码
     */
    private static ThreadLocal<Cipher> cipherThreadLocal = new ThreadLocal<>();
    private static ThreadLocal<Cipher> cipherBCThreadLocal = new ThreadLocal<>();

//    static {
//        Security.addProvider(new BouncyCastleProvider());
//        try {
//            cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
//            logger.error("AESUtil produce cipher error.", e);
//            cipher = null;
//        }
//    }

    public static Cipher getCipher() {
        Security.addProvider(new BouncyCastleProvider());
        try {
            if (cipherThreadLocal.get() == null) {
                Cipher c = Cipher.getInstance(CIPHER_ALGORITHM);
                cipherThreadLocal.set(c);
            }
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            logger.error("AESUtil produce cipher error.", e);
            cipherThreadLocal = null;
        }
        return cipherThreadLocal.get();
    }

    /**
     * AES解密
     *
     * @param encryptedData 加密数据
     * @param encodeKey Base64编码的Key
     * @param encodeIv  Base64编码的偏移量
     * @return  解密后的数据
     */
    public static String decrypt(String encryptedData, String encodeKey, String encodeIv) {
        Cipher cipher = getCipher();
        if (cipher == null) {
            return null;
        }
        byte[] decodeData = decodeData(encryptedData);
        SecretKeySpec secretKey = decodeKey(encodeKey);
        IvParameterSpec ivParameter = decodeIv(encodeIv);

        try {
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameter);
            return new String(cipher.doFinal(decodeData));
        } catch (InvalidKeyException
                | InvalidAlgorithmParameterException
                | IllegalBlockSizeException
                | BadPaddingException e) {
            logger.error("AESUtil decrypt data error. openId:{}, e:{}", e);
            return null;
        }
    }


    public static Cipher getCipherBC() {
        Security.addProvider(new BouncyCastleProvider());
        try {
            if (cipherBCThreadLocal.get() == null) {
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
                cipherBCThreadLocal.set(cipher);
            }
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException e) {
            logger.error("AESUtil produce cipher error.", e);
            cipherBCThreadLocal = null;
        }
        return cipherBCThreadLocal.get();
    }

    /**
     * AES解密
     *
     * @param encryptedData 加密数据
     * @param encodeKey Base64编码的Key
     * @param encodeIv  Base64编码的偏移量
     * @return  解密后的数据
     */
    public static String decryptBC(String encryptedData, String encodeKey, String encodeIv) {
        Cipher cipher = getCipherBC();
        if (cipher == null) {
            return null;
        }
        byte[] decodeData = decodeData(encryptedData);
        SecretKeySpec secretKey = decodeKey(encodeKey);
        IvParameterSpec ivParameter = decodeIv(encodeIv);

        try {
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameter);
            return new String(cipher.doFinal(decodeData));
        } catch (InvalidKeyException
                | InvalidAlgorithmParameterException
                | IllegalBlockSizeException
                | BadPaddingException e) {
            logger.error("AESUtil decrypt data error. openId:{}, e:{}", e);
            return null;
        }
    }

    /**
     * 解码数据
     *
     * @param encodeData  编码数据
     * @return  数据
     */
    private static byte[] decodeData(String encodeData) {
        return Base64.getDecoder().decode(encodeData);
    }

    /**
     * 解码Key
     *
     * @param encodeKey  编码Key
     * @return  Key
     */
    private static SecretKeySpec decodeKey(String encodeKey) {
        byte[] decodeKey = Base64.getDecoder().decode(encodeKey);
        return new SecretKeySpec(decodeKey, KEY_ALGORITHM);
    }

    /**
     * 解码偏移量
     *
     * @param encodeIv  编码偏移量
     * @return  偏移量
     */
    private static IvParameterSpec decodeIv(String encodeIv) {
        byte[] decodeIv = Base64.getDecoder().decode(encodeIv);
        return new IvParameterSpec(decodeIv);
    }
}

猜你喜欢

转载自blog.csdn.net/FJcsdn/article/details/88797749