package com.yx.face.boot.uitls;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.HexUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Arrays;
import java.util.TreeMap;


/**
 * @ProjectName: face-server
 * @Package: com.yx.face.boot.uitls
 * @ClassName: ForeignAesUtil
 * @Author: 崔哥
 * @Description:
 * @Date: 2021/9/8 11:03
 * @Version: 1.0
 */
@Component
public class ForeignAesUtil {

    /**
     * @author ngh
     * AES128 算法
     * <p>
     * CBC 模式
     * <p>
     * PKCS7Padding 填充模式
     * <p>
     * CBC模式需要添加一个参数iv
     * <p>
     * 介于java 不支持PKCS7Padding，只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别
     * 要实现在java端用PKCS7Padding填充，需要用到bouncycastle组件来实现
     */
    private Key key;
    private Cipher cipher;

    byte[] iv = "0103022405070878".getBytes();
    byte[] keyBytes = "fskm@0716".getBytes();

    public void init(byte[] keyBytes) {
        // 如果密钥不足16位，那么就补足.  这个if 中的内容很重要
        int base = 16;
        if (keyBytes.length % base != 0) {
            int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
            byte[] temp = new byte[groups * base];
            Arrays.fill(temp, (byte) 0);
            System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
            keyBytes = temp;
        }
        // 初始化
        Security.addProvider(new BouncyCastleProvider());
        // 转化成JAVA的密钥格式
        key = new SecretKeySpec(keyBytes, "AES");
        try {
            // 初始化cipher
            cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public String encrypt(String content) {
        return Base64.encode(encrypt(content.getBytes(), keyBytes));
    }

    public String encrypt(String content, String keyBytes) {
        return Base64.encode(encrypt(content.getBytes(), keyBytes.getBytes()));
    }

    public String encryptByHex(String content) {
        return HexUtil.encodeHexStr(encrypt(content.getBytes(), keyBytes));
    }

    public String encryptByHex(String content, String keyBytes) {
        return HexUtil.encodeHexStr(encrypt(content.getBytes(), keyBytes.getBytes()));
    }

    /**
     * 加密方法
     *
     * @param content  要加密的字符串
     * @param keyBytes 加密密钥
     * @return
     */
    public byte[] encrypt(byte[] content, byte[] keyBytes) {
        byte[] encryptedText = null;
        keyBytes = new String(keyBytes).getBytes();
        init(keyBytes);
        try {
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
            encryptedText = cipher.doFinal(content);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return encryptedText;
    }

    public String decrypt(String encryptedData) {
        return new String(decrypt(Base64.decode(encryptedData), keyBytes));
    }

    public String decrypt(String encryptedData, String keyData) {
        return new String(decrypt(Base64.decode(encryptedData), keyData.getBytes()));
    }

    public String decryptByHex(String encryptedData) {
        return new String(decrypt(HexUtil.decodeHex(encryptedData), keyBytes));
    }

    public String decryptByHex(String encryptedData, String keyData) {
        return new String(decrypt(HexUtil.decodeHex(encryptedData), keyData.getBytes()));
    }

    /**
     * 解密方法
     *
     * @param encryptedData 要解密的字符串
     * @param keyBytes      解密密钥
     * @return
     */
    public byte[] decrypt(byte[] encryptedData, byte[] keyBytes) {
        byte[] encryptedText = null;
        init(keyBytes);
        try {
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
            encryptedText = cipher.doFinal(encryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedText;
    }


    public static String getMD5BySort(TreeMap<String, String> map, String secret) {
        StringBuilder md5Param = new StringBuilder();

        for (String key : map.keySet()) {
            md5Param.append(map.get(key));
        }

        String signStr;

        if (StringUtils.isBlank(secret)) {
            signStr = DigestUtils.md5Hex(md5Param.toString());
        } else {
            signStr = DigestUtils.md5Hex(md5Param + secret);
        }

        if (signStr.length() < 25) {
            signStr = signStr.substring(5, signStr.length());
        } else {
            signStr = signStr.substring(5, 25);
        }

        return signStr;
    }


    public static void main(String[] args) {
        ForeignAesUtil foreignAesUtil = new ForeignAesUtil();
        //加密字符串
        String content = "222222";
        System.out.println("加密前的：" + content);
        // 加密方法
        String encStr = foreignAesUtil.encryptByHex(content);
        System.out.println("加密后的内容：" + encStr);
        // 解密方法
        String decStr = foreignAesUtil.decryptByHex(encStr);
        System.out.println("解密后的内容：" + decStr);
    }
}
