package com.dxhy.common.utils;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;


/**
 * 3DES加解密工具类
 *
 * @author ZSC-DXHY
 * @date 创建时间: 2020-08-14 15:45
 */
@Slf4j
public class TripleDesUtil {
    private static final String LOGGER_MSG = "(3DES解密)";
    
    /**
     * 定义 加密算法,可用 DES,DESede,Blowfish
     */
    private static final String ALGORITHM = "DESede";
    
    /**
     * 加密算法
     * password为加密密钥，长度24字节
     * src为被加密的数据缓冲区
     */
    public static byte[] encryptMode(String password, byte[] src) {
        try {
            //生成密钥
            SecretKey deskey = new SecretKeySpec(password.getBytes(StandardCharsets.UTF_8), ALGORITHM);
            //加密
            Cipher c1 = Cipher.getInstance(ALGORITHM);
            c1.init(Cipher.ENCRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (Exception e1) {
            log.error("{}3DES加密异常:{}", LOGGER_MSG, e1);
        }
        return null;
    }
    
    
    /**
     * 解密算法
     * password为解密密钥，长度24字节
     * src为被加密的数据缓冲区
     */
    public static byte[] decryptMode(String password, byte[] src) {
        try {
            //生成密钥
            SecretKey deskey = new SecretKeySpec(password.getBytes(StandardCharsets.UTF_8), ALGORITHM);
            //解密
            Cipher c1 = Cipher.getInstance(ALGORITHM);
            c1.init(Cipher.DECRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (Exception e2) {
            log.error("{}3DES解密异常:{}", LOGGER_MSG, e2);
        }
        return null;
    }


    public static void main(String[] args) {
        String src = "cyjvv73vv70R77+977+977+9AAAAAABADO+/vVXvv73vv73vv70B77+977+9bO+/ve+/vT4dJu+/vTU077+977+977+9Pzdjfe+/vTDvv70t77+9Be+/ve+/ve+/vWsi77+9E38GMu+/vTt6HTjThlfvv73vv73vv73vv71K77+9B++/vc+pb2bvv73vv70ee0tiBO+/vVLvv70Z77+9Ryzvv70977+9fnnvv71377+977+9ORF/fu+/vV9D77+9VO+/vU7vv73vv70B77+9FGPvv73vv73vv71A77+9yKXvv71Pae+/vQEBZDBGIe+/vU9tBW8377+9DO+/ve+/vcmPHxrvv73vv70q77+9Ee+/vRJ6VgRI77+9TGDvv73KtUTvv70K77+977+977+9O++/ve+/ve+/vVtgBzvvv70P77+9djPvv73vv73vv70h77+977+9M3M7CUg/SO+/vVnvv70GOSzro4jvv71i77+9xJfvv70p77+9Ee+/ve+/vR/vv70e77+9fu+/vdSEa0nvv70z77+977+977+9GGg877+977+9PO+/vVAQ77+9GAsT17Tvv73SlRjvv73vv70hAUfvv73vv70+yIdtEO+/ve+/vSnvv73vv73Fjz4M77+977+9Yu+/vd+b77+9OSpJd++/ve+/ve+/vXfvv71COO+/vVPvv71R65KOS++/ve+/ve+/ve+/vRfvv71M77+9UGxoxLcK77+9Mu+/vWTvv73vv71yIO+/vRcG77+9CO+/vS7vv70h77+977+977+9NFkI77+977+977+9VO+/vdabN++/ve+/vRDMku+/vUbvv70E77+977+9e++/vd+Q77+9RnYD77+9fO+/ve+/vUXvv73vv73vv71rP++/ve+/ve+/ve+/ve+/vQNgWu+/vUDvv71mah9E77+977+977+9BkVj77+977+977+9U13vv70N77+9Nu+/ve+/ve+/vRp3JO+/ve+/ve+/vTLcue+/vU3vv73vv70hIO+/vQrvv73vv707A++/ve+/ve+/ve+/vVDvv73vv73vv73vv70/77+977+9TO+/vdqU77+9UO+/ve+/vX0O77+9XO+/ve+/vTBj0aoIe++/vRwYfe+/vRvvv70gJmbvv70M77+977+9Ocqw77+977+9XA8G77+9de+/ve+/ve+/vSZDLjcDQxMWBEsjxLsWwr7vv71zI++/vVwqEi57Se+/vUUj77+9QXk+77+977+9Pe+/vX/vv73vv73vv71LYzpD77+9ewbvv70iDGhLB++/vSHvv73FlO+/vdmo77+9G++/vRrvv71977+9LCE2Qe+/ve+/vSBR77+977+977+9Ve+/ve+/vQ/vv71bI++/ve+/ve+/ve+/vWZ6fyt+77+977+977+9za1jayDvv70qWW8o77+9JxJWzKotLe+/vUxhV++/vQXvv73vv73vv71NcSsPZFnvv73vv70OS++/ve+/ve+/vVvvv70Xae+/ve+/vRxCKGZT77+9ewfvv70/DDcq77+977+977+9UO+/vR/vv73vv70l34Fa77+9WV/vv70qQWYHHB7vv71+Ngjvv70s77+9UO+/vSbvv71obO+/vR7vv73vv73vv73vv73vv71kdu+/vcO1XO+/ve+/ve+/ve+/ve+/ve+/vQXvv73vv70FGO+/vXHvv73vv73vv73vv71daO+/ve+/vXPvv73vv73XvQBXUu+/vXQm77+9ZiYA77+9XmLvv73vv73vv71o77+977+9QO+/vRvvv73vv71UXu+/ve+uptuI77+9e++/ve+/vd+D77+9KXXvv71pfRPvv70W77+977+9KO+/ve+/vQA=";
        System.out.println(decryptMode("c2FucWljZXNoaUFwcHNlY0tl", src.getBytes()));
    }
    
}
