AES復号

5156 ワード

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 cipherThreadLocal = new ThreadLocal<>();
    private static ThreadLocal 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);
    }
}