package com.ms.biz.tool;

import com.ms.biz.tool.security.AES;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;

public class SecurityTool {

    public static String key = "dighgfghf^&)l3sedi&(.D@BC";

    /**
     * AES加密
     */
    public static String encodeByAES(String context){
        byte[] aesByte;
        try {
            AES aes = new AES(key);
            aesByte = aes.encode(context);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // 将ase_encode的结果base64_encode处理
        Base64.Encoder encode = Base64.getEncoder();
        return encode.encodeToString(aesByte);
    }

    /**
     * AES解密
     */
    public static String decodeByAES(String aesString){
        Base64.Decoder decoder = Base64.getDecoder();
        //将加密后的字符串先base64_decode处理
        byte[] aesByte = decoder.decode(aesString);
        //解密
        String desString;
        try {
            AES aes = new AES(key);
            desString = aes.decode(aesByte);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return desString;
    }

    public static String MD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8));
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 抖店的消息解密
     * @param sSrc
     * @param sKey
     * @return
     */
    public static String decryptDoudianCallback(String sSrc, String sKey) {
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            int blockSize = cipher.getBlockSize();
            IvParameterSpec iv = new IvParameterSpec(sKey.substring(0,blockSize).getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }

    public static void main(String[] args) {
//        String msgSecret = "rzYLiyrSFtW8vSQWrlRTtsSHcH6KZdJGEpCGNZRjNcdXrn1HR39YSMXhj7z36gbeHdqPqR2zbkzO2CMOJz6l1aDFX5ZYTYHsLAIdOyN3RyQYJDihcLzhlACdNf/fEf28srQI37N/7+gVwSApAmnvP7CvBg8X5Hb+2A2cK13jMT8GAskYsNieL982MMPiAZmAF8ktAqN2bLJo/IpxYrW+OXLudrRylWXeLa5fbqO+pZVAtItoGN995lf6iPF8JnWJpQaSBPVuUgU/6Ns5sFWl7A==";
        String msgSecret = "rzYLiyrSFtW8vSQWrlRTtsSHcH6KZdJGEpCGNZRjNcdXrn1HR39YSMXhj7z36gbeHdqPqR2zbkzO2CMOJz6l1aDFX5ZYTYHsLAIdOyN3RyTiAMe+84MuHM3uxeNkW/JIKQHQ0OBWp23RHqn1mxKaiaij7cG+jkAC/nrQW027QRqWW+y74MK5v79h3ex4BvlIcPq6hALSMAso7pWuo7KTUjI+QRM8CBafWYNRzcYMl3jyEKZTozWHHKXb+aZMVcjS1hiNNTRTAfZWFfkutSk2kA==";
        String appSecret = "4c927c89-d55c-42cf-805c-694269a4b165";
        String key = appSecret.replaceAll("-", "");
        String decrypt = decryptDoudianCallback(msgSecret,key);
        System.out.println("解密之后的数据为: "+decrypt);
    }
}
