package com.tenlionsoft.baselib.utils;


import android.util.Base64;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

//AesUtil.aesCommonDecoder(ISystemConstant.APP_TOKEN_AES_KEY, new String(Base64.decodeBase64(token), "UTF-8"));
public class AesUtil {

    private static final String IV_STRING = "16-Bytes--String";

    /**
     * AES加密
     *
     * @param encodeRule
     * @param content
     * @return
     * @throws Exception
     */
    public static String aesEncoder(String encodeRule, String content) throws Exception {
        // 1.构造秘钥生成器
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        // 2.根据编码规则构建秘钥生成器
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(encodeRule.getBytes());
        keyGenerator.init(128, random);
        // 3.产生堆成秘钥
        SecretKey secretKey = keyGenerator.generateKey();
        // 4.对称秘钥原始数组
        byte[] secretKeyByte = secretKey.getEncoded();
        // 5.生成AES秘钥
        SecretKey key = new SecretKeySpec(secretKeyByte, "AES");
        // 6.初始化密码器
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        // 8.获取加密内容字节数组
        byte[] contentByte = content.getBytes("UTF-8");
        // 9.加密
        return new String(Base64.encode(cipher.doFinal(contentByte), Base64.DEFAULT));
    }

    /**
     * AES解密
     *
     * @param encodeRule
     * @param content
     * @return
     * @throws Exception
     */
    public static String aesDecoder(String encodeRule, String content) throws Exception {
        // 1.构建秘钥生成器
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        // 2.根据编码规则构建秘钥生成器
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(encodeRule.getBytes());
        keyGenerator.init(128, random);
        // 3.生成对称解密秘钥
        SecretKey secretKey = keyGenerator.generateKey();
        // 4.对称秘钥原始数组
        byte[] secretKeyByte = secretKey.getEncoded();
        // 5.生成AES秘钥
        SecretKey key = new SecretKeySpec(secretKeyByte, "AES");
        // 6.初始化密码器
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        // 7.要解密内容的字节数组
        byte[] contentByte = Base64.decode(content, Base64.DEFAULT);
//         Base64.decodeBase64(content);
        // 8.解密
        return new String(cipher.doFinal(contentByte), "UTF-8");
    }

    /**
     * 通用aes加密，兼容IOS
     *
     * @param key
     * @param content
     * @return
     * @throws Exception
     */
    public static String aesCommonEncoder(String key, String content) throws Exception {
        byte[] byteContent = content.getBytes("UTF-8");
        byte[] enCodeFormat = key.getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
        byte[] initParam = IV_STRING.getBytes();
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(byteContent);
        return new String(Base64.encode(encryptedBytes, Base64.DEFAULT), "UTF-8");
    }

    /**
     * aes通用解密，兼容IOS
     *
     * @param key
     * @param content
     * @return
     * @throws Exception
     */
    public static String aesCommonDecoder(String key, String content) throws Exception {
        byte[] encryptedBytes = Base64.decode(content, 0);
        byte[] enCodeFormat = key.getBytes();
        SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");
        byte[] initParam = IV_STRING.getBytes();
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
        byte[] result = cipher.doFinal(encryptedBytes);
        return new String(result, "UTF-8");
    }


    //    public static final byte[] a = new byte[]{
//        120, 119, 57, 80, 52, 110, 102, 56, 114, 66,
//        75, 75, 65, 110, 119, 53};
    public static final byte[] a = new byte[]{
        120, 119, 57, 80, 52, 110, 102, 56, 114, 66,
        75, 75, 65, 110, 119, 53};

    public static String test(String paramString) {
        byte[] arrayOfByte = "1234567890123456".getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(a, "AES");
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(1, secretKeySpec, new IvParameterSpec(arrayOfByte));
            byte[] bytes = cipher.doFinal(paramString.getBytes());
            return new String(Base64.encode(bytes, Base64.NO_PADDING), "UTF-8");
        } catch (Exception exception) {
            exception.printStackTrace();
            return null;
        }
    }
}
