package com.hfkj.common;

import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;

import java.util.Base64;

public class Base64Util {
	   /**
     * @param data
     * @return str
     * @throws Exception
     */
    public static String encode(String data) throws Exception{
//        String encodeBase64 = new BASE64Encoder().encode(data.getBytes("utf-8"));
        String encodeBase64 = Base64.getEncoder().encodeToString(data.getBytes("utf-8"));
        String safeBase64Str = encodeBase64.replace('+', '-');
        safeBase64Str = safeBase64Str.replace('/', '_');
        safeBase64Str = safeBase64Str.replaceAll("=", "");
        return safeBase64Str.replaceAll("\\s*", "");
    }

    /**
     * @param safeBase64Str
     * @return str
     * @throws Exception
     */
    public static String decode(final String safeBase64Str) throws Exception{
        String base64Str = safeBase64Str.replace('-', '+');
        base64Str = base64Str.replace('_', '/');
        int mod4 = base64Str.length() % 4;
        if(mod4 > 0){
            base64Str += "====".substring(mod4);
        }

//        byte[] ret = new BASE64Decoder().decodeBuffer(base64Str);
        byte[] ret = Base64.getDecoder().decode(base64Str);
        return new String(ret, "utf-8");
    }

    public static void main(String[] args) throws Exception {
		//System.out.println(encode("abcd1234"));
        String data = new AesUtil("UXt3EGM5X6P71EXcNmszV1ONk7LX9IEj".getBytes())
                .decryptToString(
                        "mch_payment".getBytes(),
                        "pNkl4m74gbiO".getBytes(),
                        "HuOlyjwpF9fes1TAgOw5zzBER/YdGcxUWw3U+o3HXpN8CrporigxRPQNjV6N1Ksmhx9hhAaAnA+7k+1rkOd6SBi2xT6/g3HI6VszUrjSKN77BxWBh4q0gLTLdqyBTp1N3wKZh3Pg8d2Vktvf5nHzzoeTGNZ6llMsxME6qP5muCIbjelOBUq3LR/DNO+AjQHFaR/rkne0f+TT8kUcY51qhBAVGY4JRt5Y358OGkQqvMNCo/jp2DD1WlV0g1v4PEpjMlkWC/lqYvUbtrvOU52Y+olnrFVusuvLhsBP6AxUOTYlTuIA1UKtizSIfyGM+yAOuKbrQmAJCWUIda0Flo71h+r1N+TC31l1tOGTQgJca+4XT9JEENMVmxTh2lGM47abIyab6GbQezjD9Q6JzK7QZ2eINU57NY48+1oK12mcmM8M2OeYKyqu0vL+RC1hQltyshU=");
        System.out.println(data);
        // System.out.println(new AesUt("HuOlyjwpF9fes1TAgOw5zzBER/YdGcxUWw3U+o3HXpN8CrporigxRPQNjV6N1Ksmhx9hhAaAnA+7k+1rkOd6SBi2xT6/g3HI6VszUrjSKN77BxWBh4q0gLTLdqyBTp1N3wKZh3Pg8d2Vktvf5nHzzoeTGNZ6llMsxME6qP5muCIbjelOBUq3LR/DNO+AjQHFaR/rkne0f+TT8kUcY51qhBAVGY4JRt5Y358OGkQqvMNCo/jp2DD1WlV0g1v4PEpjMlkWC/lqYvUbtrvOU52Y+olnrFVusuvLhsBP6AxUOTYlTuIA1UKtizSIfyGM+yAOuKbrQmAJCWUIda0Flo71h+r1N+TC31l1tOGTQgJca+4XT9JEENMVmxTh2lGM47abIyab6GbQezjD9Q6JzK7QZ2eINU57NY48+1oK12mcmM8M2OeYKyqu0vL+RC1hQltyshU="));
	}
}
