package com.bonus.hn.bearing.capacity.backstage.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;

/**
 * AES的加密和解密
 * @author libo
 */
public class Aes {
    //密钥 (需要前端和后端保持一致)
    private static final String KEY = "bonus@admin12345";
    //算法
    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    /**
     * aes解密
     * @param encrypt   内容
     * @return
     * @throws Exception
     */
    public static String aesDecrypt(String encrypt) {
        try {
            String str=encrypt.replaceAll(" ","+");
            return aesDecrypt(str, KEY);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * aes加密
     * @param content
     * @return
     * @throws Exception
     */
    public static String aesEncrypt(String content) {
        try {
            return aesEncrypt(content, KEY);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 将byte[]转为各种进制的字符串
     * @param bytes byte[]
     * @param radix 可以转换进制的范围，从Character.MIN_RADIX到Character.MAX_RADIX，超出范围后变为10进制
     * @return 转换后的字符串
     */
    public static String binary(byte[] bytes, int radix){
        // 这里的1代表正数
        return new BigInteger(1, bytes).toString(radix);
    }

    /**
     * base 64 encode
     * @param bytes 待编码的byte[]
     * @return 编码后的base 64 code
     */
    public static String base64Encode(byte[] bytes){
        return Base64.encodeBase64String(bytes);
    }

    /**
     * base 64 decode
     * @param base64Code 待解码的base 64 code
     * @return 解码后的byte[]
     * @throws Exception
     */
    public static byte[] base64Decode(String base64Code) throws Exception{
        return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
    }


    /**
     * AES加密
     * @param content 待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的byte[]
     * @throws Exception
     */
    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));

        return cipher.doFinal(content.getBytes("utf-8"));
    }


    /**
     * AES加密为base 64 code
     * @param content 待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的base 64 code
     * @throws Exception
     */
    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey));
    }

    /**
     * AES解密
     * @param encryptBytes 待解密的byte[]
     * @param decryptKey 解密密钥
     * @return 解密后的String
     * @throws Exception
     */
    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);

        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }


    /**
     * 将base 64 code AES解密
     * @param encryptStr 待解密的base 64 code
     * @param decryptKey 解密密钥
     * @return 解密后的string
     * @throws Exception
     */
    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
    }

    /**
     * 测试
     */
    public static void main(String[] args) throws Exception {

        String  data="RNqNYAHeaDAbSxAX79iVtzB9WazgFguK7Xos0X6sOprX7O7rQnllVjSi1Cl95oVevDsXTVYfZgOyCKxWH5Xv30mox9wIqh2jrHpO0pRUk4Bh2gxG3ty64hH7hMiGQYXxWOsHxPCWCCDALp6mS7iKryRgolf/UFh5rkNQTXTbtjL2ehc/QAoUVDpAww9YLN2CgzcQvLTjoum9rAIoIx+j1sasaViKMiln0dGFZNLQJDnoYuX5SVl6dojhv7tjtTNRz0PIaYxf4dCiIu4D+8kmpY0VnUHmmf/Wh4N1C971w0nU+Sdv2zaTK8/lvJNXkO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vETygOQM0EqT2Jvdmx+OxF1t/YQosUmvUvzYwWQKDPb7CHPSCyWqKWq5vW95O/eWFJzCTJC3jX+lhLsGZB/5adeVqe/GJAAKJBBcwum/26Xc5u812XwN3kmIst4LLXV0PSDFkiH1UQBld8srZCzLx7c4Mn/Q5ykvRPkZsBTePNNns5hFKGq15yhmPm3Eclt1GcFMvsCuMZyNevt3WgoFjLTSPhlg+/ZkhpMIl4yitBmFhYbj1FUP/FZy5nlywqdaF4w9IfKAdnMuZUUNjllYGELvPbpfeYexSxtf5KNXK8gThFzpz/Jrzr6gHeLijhhNIY9Z61nfNcRx4Kce0w53QKtW7Rg8ZiL+5gblz4CobWLnBJfUlGtS+IF/nGoqbe3RsG3Gn2pPL2wYcVcjPUKs/c+K83Qh3TTfeaEEMNgVkPJhIVj/yxiKp0IZDUgH9gfko3cW4p25BYxz5tM5VJNu+n1fuA6UFRpr2iWHFsIov8zvHWLQgaMM/vOv8d07x/BowNIWDvqrKrFVl/XzP5tlWj5doBBh82T+sQLZ5KZx3f+J8F6ytGiKd3+QeNvzUjFqenZtYWctcqrGO9jmXpnPeD9ff39OVLxHqQyB+zIVNXpPL1YwTvHC/zmPHQBnlW6xXxrMWb9qnupcWti23LBHrBerh1lYI9uFonAmItDS2vfEbVtziYBNcp447rJNu5SFddVFEU8u375ksfQ98icblOzu7+wZLlYnhAiQEqFhmshIH255pMTYDPtSX8HQRvMdpPUdVAKJbsUjvl/I2HRmA48a7tXpHlEksfzVJE+7bPSgphF5lloOjt9xTAd5XAC8RuOuI6O7Ld/m4l7g4v7YrSqFn5FT8o9CvzXw1zWOTUgGgMGEzFoEYJToKI=";
        String  jm =aesEncrypt(data);
        System.out.println("解密后：" + jm);
//        String content = "test";
//        System.out.println("加密前：" + content);
//        System.out.println("加密密钥和解密密钥：" + KEY);
//        String encrypt = aesEncrypt(content, KEY);
//        System.out.println("加密后：" + encrypt);
//        String decrypt = aesDecrypt(encrypt, KEY);
//        System.out.println("解密后：" + decrypt);
//        String     result= AesEncryptUtils.decrypt("3qO06ijUcELmiJgymXD3CbVPiwD1+MGTxWRoC3TWm1Cv+vlWIXEKhNNtkJGmas/oG+F9yImSCFefMmRW5fkhFFmEemNrt4wVhte3mHbZpqNsEJANwixB4nT9o5HKeZtr/b8z1JfS6yuaWse46ZFNdqSt6gTErN7K+QYVLdnOpMUyVckLlYlj5H7h5of9KPoi6dEDbkJW7lpL6uFZRuE3kA==",AesEncryptUtils.KEY_NAME);
//        String str=  AesEncryptUtils.encrypt("8F12517D-0D",AesEncryptUtils.KEY_NAME);
//        System.out.println("解密后：" + result);
    }


}
