package com.tcl.ah.security;

import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

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;


/*
 * @description     自定义工具类，实现open_ssl的AES加密算法
 *
 * @author      qinglei.zhao
 * @see
 * @since
 * @data        2019/10/30 19:06:00
 * @LastEditTime        2019/10/30 19:06:00
 */
public class OpenSslAES {
    public static final String TRANSFORMATION="AES/CBC/PKCS7Padding";
    public static final String ALGORITHM="AES";
    //用于加密或解密的KEY
    //private static final String KEY = "kPxT3zFSGIuvwKuxk7F3KeqbrvL6pm2xWHHjCNqAmjA=";

    /*
     * @description     实现PHP的openssl_encrypt，完成对数据的加密
     * @author      qinglei.zhao
     * @param   data    需要加密的数据
     * @param   iv  用于加密的IV向量
     * @return 加密以后的密文
     * <em>string类的密文<em>   success
     * <em>null<em>   failed
     * @since  0
     * @data        2019/10/30 19:10:00
     * @LastEditTime        2019/10/30 19:10:00
     */
    public static byte[] openssl_encrypt(byte[] key,byte[] data, byte[] iv) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException{
        /*
         * 调用createCipher()函数生成密码器（加密）
         * iv为用于加密的向量
         * Cipher.ENCRYPT_MODE，密码器的模式（加密）
         */
        Cipher cipher = createCipher(key,iv, Cipher.ENCRYPT_MODE);
        return cipher.doFinal(data);
    }

    /*
     * @description     实现PHP的openssl_decrypt，完成对加密数据的解密
     * @author      qinglei.zhao
     * @param   data    需要解密的数据
     * @param   iv  用于解密的IV向量
     * @return 解密以后的明文
     * <em>string类的明文<em>   success
     * <em>null<em>   failed
     * @since  0
     * @data        2019/10/30 19:10:00
     * @LastEditTime        2019/10/30 19:10:00
     */
    public static String openssl_decrypt(byte[] key,byte[] data, byte[] iv)throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException {
        /*
         * 调用createCipher()函数生成密码器（解密）
         * iv为用于解密的向量
         * Cipher.ENCRYPT_MODE，密码器的模式（解密）
         */
        Cipher cipher = createCipher(key,iv, Cipher.DECRYPT_MODE);
        return new String(cipher.doFinal(data), StandardCharsets.UTF_8);
    }

    /*
     * @description     创建用于加密或解密的密码器Cipher
     * @author      qinglei.zhao
     * @param iv
     * @param mode 加/解密模式
     * @return 密码器对象
     * <em>cipher<em>   success
     * <em> <em>   failed
     * @since  0
     * @data        2019/10/30 19:10:00
     * @LastEditTime        2019/10/30 19:10:00
     */
    private static Cipher createCipher(byte[] key,byte[] iv, int mode) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, InvalidAlgorithmParameterException {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(mode, new SecretKeySpec(key, ALGORITHM),
                new IvParameterSpec(iv));
        return cipher;
    }
}