package com.sipai.jspatrol.utils;


import android.util.Base64;

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

/**
 * @ClassName : EncryptUtils
 * @Description : 加密解密工具类
 * @Author : HJQ
 * @Date: 2020-10-09 11:11
 */
public class EncryptUtils {


    //参数分别代表 算法名称/加密模式/数据填充方式

    private static final String ALGORITHMSTR = "AES/ECB/PKCS7Padding";

//    static {
//        if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
//            System.out.println("security provider BC not found");
//            Security.addProvider(new BouncyCastleProvider());
//        }
//
//    }

    //可配置到Constant中，并读取配置文件注入,16位,自己定义
    private static final String KEY = "34bfeacAB39053deb006df63";


    /**
     * 加密
     *
     * @param content    加密的字符串
     * @param encryptKey key值
     * @return
     * @throws Exception
     */
    public static String encrypt(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"));
        byte[] b = cipher.doFinal(content.getBytes("utf-8"));
        // 采用base64算法进行转码,避免出现中文乱码
        return Base64.encodeToString(b,Base64.DEFAULT);

    }

    /**
     * 解密
     *
     * @param encryptStr 解密的字符串
     * @param decryptKey 解密的key值
     * @return
     * @throws Exception
     */
    public static String decrypt(String encryptStr, 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"));
        // 采用base64算法进行转码,避免出现中文乱码
        byte[] encryptBytes = Base64.decode(encryptStr,Base64.DEFAULT);
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

    public static String encrypt(String content) throws Exception {
        return encrypt(content, KEY);
    }

    public static String decrypt(String encryptStr) throws Exception {
        return decrypt(encryptStr, KEY);
    }


//    public static void main(String[] args) throws Exception {
//        String name = "sipai";
//        String pwd = "123456";
//
//        String nameContent = JSONObject.toJSONString(name);
//        String pwdContent = JSONObject.toJSONString(pwd);
//        System.out.println("加密前：" + nameContent);
//        System.out.println("加密前：" + pwdContent);
//
//        String encryptName = encrypt(name, KEY);
//        String encryptPwd = encrypt(pwd, KEY);
//        System.out.println("加密后：" + encryptName);
//        System.out.println("加密后：" + encryptPwd);
//
//        String decryptName = decrypt(encryptName, KEY);
//        String decryptPwd = decrypt(encryptPwd, KEY);
//        System.out.println("解密后：" + decryptName);
//        System.out.println("解密后：" + decryptPwd);
//    }

    /**
     * @Description: 根据body 获取数据
     * @Author: HJQ
     * @Param request
     * @Return: java.util.Map<java.lang.String, java.lang.Object>
     * @Date: 2020/10/9 14:56
     */
//    public static HashMap<String, String> getMap(HttpServletRequest request) {
//        HashMap<String, String> params = new HashMap<String, String>();
//        BufferedReader br;
//        try {
//            br = request.getReader();
//            String str, wholeStr = "";
//            while ((str = br.readLine()) != null) {
//                wholeStr += str;
//            }
//            if (StringUtils.isNotEmpty(wholeStr)) {
//                params = JSON.parseObject(wholeStr, HashMap.class);
//            }
//        } catch (Exception e1) {
//        }
//        return params;
//    }

}