package com.unis.crk.utils;

import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;

@SuppressWarnings("all")
public class SecureMngr {

    /**
     * 数据解密
     */
    public static String decryptor(String data, String password) throws Exception {
        //对string进行BASE64Encoder转换
        sun.misc.BASE64Decoder base64en = new sun.misc.BASE64Decoder();
        byte[] bt = decrypt(base64en.decodeBuffer(data), password);
        String strs = new String(bt, "utf-8");
        return strs;
    }

    /**
     * 解密函数
     */
    private static byte[] decrypt(byte[] src, String key) throws Exception {
        // DES算法要求有一个可信任的随机数源
        SecureRandom random = new SecureRandom();
        // 创建一个DESKeySpec对象
        DESKeySpec desKey = new DESKeySpec(key.getBytes());
        // 创建一个密匙工厂
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // 将DESKeySpec对象转换成SecretKey对象
        SecretKey securekey = keyFactory.generateSecret(desKey);
        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance("DES");
        // 用密匙初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, random);
        // 真正开始解密操作
        return cipher.doFinal(src);
    }

    /**
     * 数据加密
     */
    public static String encrypt(String data, String password) throws UnsupportedEncodingException {
        //对string进行BASE64Encoder转换
        byte[] bt = encryptByKey(data.getBytes("utf-8"), password);
        BASE64Encoder base64en = new BASE64Encoder();
        String strs = new String(base64en.encode(bt));
        return strs;
    }

    /**
     * 加密函数
     */
    private static byte[] encryptByKey(byte[] datasource, String key) {
        try {
            SecureRandom random = new SecureRandom();

            DESKeySpec desKey = new DESKeySpec(key.getBytes());
            //创建一个密匙工厂，然后用它把DESKeySpec转换成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            //Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("DES");
            //用密匙初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            //现在，获取数据并加密
            //正式执行加密操作
            return cipher.doFinal(datasource);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }


    // -------------------------- AES --------------------------

    public static final String AES_KEY = "shanxihlhtaeskey";

    private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String AES_KEY_ALGORITHM = "AES";

    // 用AES加密, 加密算法为 AES/CBC/PKCS5Padding
    public static String encryptByAES(String key, String data) throws Exception {
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE,
                new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), AES_KEY_ALGORITHM),
                new IvParameterSpec(key.getBytes(StandardCharsets.UTF_8)));
        byte[] out = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(out);
    }

    // 用AES解密, 解密算法为 AES/CBC/PKCS5Padding
    public static String decryptByAES(String key, String dataBase64) throws Exception {
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE,
                new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), AES_KEY_ALGORITHM),
                new IvParameterSpec(key.getBytes(StandardCharsets.UTF_8)));
        byte[] out = cipher.doFinal(Base64.getDecoder().decode(dataBase64));
        return new String(out, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) throws Exception {
        //String s = "11aa22bb33cc+=";
    	String s = "[{\"ODS_UP_LSKCXXJK\":[{\"HWDM\":\"91610139294257724C001003700101\",\"LSPZDM\":\"2123000\",\"LSXZDM\":\"122\",\"LSDJDM\":\"04\",\"SHND\":\"2021\",\"GB\":\"156\",\"CD\":\"650000\",\"BGY\":\"薛成刚\",\"LQGSDWDM\":\"91610139294257724C\",\"GLFS\":\"01\",\"SCDD\":\"1\",\"CLFS\":\"1\",\"HWZT\":\"4\",\"RCSJ\":\"2023-07-05 08:45:47\",\"FCRQ\":\"2023-07-05\",\"CCWCSJ\":null,\"QCSJ\":null,\"SJSL\":2000000,\"JJSL\":2000000,\"BCLBS\":0,\"SJZLXG\":0,\"LDTJ\":0,\"BZ\":null,\"CZBZ\":\"u\",\"ZHGXSJ\":\"2023-07-05 08:45:47\",\"ID\":\"1659954498468188160\",\"IS_UPLOAD\":0,\"LOAD_DATE\":\"2023-07-05 08:31:09\",\"DEP_DWDM\":null,\"DEP_KQDM\":null,\"CHWQSH\":0}]}]";
        String s1 = encryptByAES("qhhlhtaeskey", s);
        System.out.println(s1);
    	
    	
    	/*String b = "9463IawKbYpP2JQ25rq98Bq7iz3EqCsmnPogxgK3EarEKyl9jA4H0qYVxjmUQ76ze6YduVg0BohtbQBgpSFfnKV2EiaCIWOloBmsz6Hms30AP+aHsX+lG3Twy98CXytJU4+FYGKKuoOB5NmzxEH4Vsa/EKucVlq7goxBl0xmYaq1098guA8OybWwV1zOzlBX2EHQFAeT+bf6U2WW9ovboPM7hp7cJubccL1QKZR1Fn1O64GWRPEWltbx/slg5oY7O46y6AxyypH4pFXZ9Hk2D+oJSwtTMxZWeO0/UgBe2I4heBm9uk70eUnJdnEAYiF4j8YZ4YceJWOFX/mq96gaa3MIwBMyr2pxjgXe+Fzx+taLn38dCXZXnB83vywxrSAPYZJsKd4xEmXEazEU+0q2OTCR8EleERm6Ef5pcP1zrZVBGIkNUN4GV8kY5n6phDHiLiHMb0hcmuxaRYIwtmkzNT0Sy7hmPna2Ckt+t1vmXTln7vKIrOz7ZeFDdvbOk922ndmrNs1eN8UvEVWLx9I+XRUxQjjNFsvRhE0ICLMwpx+GhikRBSiyJVJGxP9q+u2verXDNhHXx/S44TmJ5WbLZgU4Cjecd2cg/HxoiIfIsf6+uHfXnAfXDUVGIxfaudTisb6LuwtXORbwyZllJLv95V/zqU95dg1l3IDYYLHBfw/JWMAmFULmXsp+PR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uImNS90Wex0c97HhcNrUY6AlBdPWZV869f0hPpOBHZeNFph0JI38DNyDzh1gMn5/LZos8C50XBQkM20QmXZ08O3dkLBgi1w==";
        String a  = decryptByAES("qhhlhtaeskey",b);
        System.out.println(a);*/
    }
    
    

}
