package com.mints.goodmoney.utils;


import com.mints.library.utils.TLog;
import com.mints.goodmoney.common.Constant;
import com.mints.goodmoney.BuildConfig;

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

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;


public class AESUtils {

    private static final String vis = MD5.GetMD5Code("goodmoney_2020").substring(8, 24);
    //    public static final String key = "123456";
    public static String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"; // optional value AES/DES/DESede

    private static SecretKeySpec getKey(String strKey) throws Exception {
        byte[] arrBTmp = strKey.getBytes();
        byte[] arrB = new byte[16]; // 创建一个空的16位字节数组（默认值为0）

        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }
        SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");
        return skeySpec;
    }

    public static String encrypt(String message, String key) {
        try {
            SecretKeySpec skeySpec = getKey(key);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(vis.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(message.getBytes("UTF-8"));
            return new BASE64Encoder().encode(encrypted);
        } catch (Exception e) {
            TLog.e(Constant.TAG_NAME, "aes加密失败");
        }
        return "";
    }


    public static String detrypt(String message, String key) {
        try {
            byte[] res = new BASE64Decoder().decodeBuffer(message);
            SecretKeySpec skeySpec = getKey(key);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(vis.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] detrypted = cipher.doFinal(res);
            return new String(detrypted, "UTF-8");
        } catch (Exception e) {
            TLog.e(Constant.TAG_NAME, "aes解密失败");
        }
        return "";
    }

    private static String getBK1() {
        return BuildConfig.AppKeyPre;
    }

    private static String getBK2() {
        return "nnnnnn";
    }

    private static String getBK3(int size) {
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i <= size; i++) {
            sb.append(i);
        }
        return sb.toString();
    }

    public static String getDefaultKey() {
        StringBuffer sb = new StringBuffer();
        sb.append(getBK1())
                .append(getBK2())
                .append(getBK3(6));
        return sb.toString();
    }

    public static void main(String[] args) throws Exception {
//        String message = "123456wwe";
//        String entryptedMsg = encrypt(message, key);
//        System.out.println("encrypted message is below :");
//        System.out.println("加密结果：" + entryptedMsg);
//        String dexryptedmsg = "Dj4MYXI5X6gL3H+bBjAhspXibZVhO79bgzoIawpl/6NNJWsjTHKMJGLtTPmvInRmbK3ipi4NvxsU\n" + "FtSufnPbZCtpydpNldkXCJ3vNALju60=";
//        String decryptedMsg = detrypt(dexryptedmsg, key);
//        System.out.println("decrypted message is below :");
//        System.out.println(MD5.GetMD5Code("meijie_2016").substring(8, 24));

        System.out.println(detrypt("5EBue8TMfYS2P8YAZUyJUKnYvTVEblnJMwC00vC1yZHjvqq6Ws4TeaXQ+7l+iwdzoQNi2yJ9CLrG\n" +
                "cfQlCjaAbr5gmLokexHida7yLofB7V5ItaxTGlXyxK3CH+Wr/lvbWKmP8YROCrtzrreqP8svuf0M\n" +
                "AzKbOnPhoURvpfVS5di/dk3chtujIqC74xd5XpLJfHiXQadEX+12zq+IXa0xIWezTHjL6cJBTN6y\n" +
                "xg5w6+ey7EYzEdJ118HaSTr1tLu7AkxguXJez9QxhB9nifRBvgl7tlY0oL20+yGGZ3WgR/CmWh9u\n" +
                "PjEy0eB3NgmNd8SRmImXDYOQDMJqxa5AUMKjQkQ/NmrDLBdRO+9zy5Ujv7YL1KVoUmmxeH5EVPZR\n" +
                "nV+E", "abcdnnnnnn123456"));
    }
}
