package com.yc.ship.module.resource.service.resource.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.logging.log4j.util.Base64Util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.security.SecureRandom;

/**
 * DesUtil for 同程 SameWay
 * @Title: DesUtil.java
 * @Package: com.wisesoft.scenic.util
 * &#064;Description: TODO
 * @author: zhoulonggui
 * @date: 2017年5月17日 下午2:51:24
 * @version: V1.0
 */
public class DesUtil {
	
	/** */
	private static final String DES = "DES";
	
	/**
     * Description 根据键值进行加密
     * @param data 
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
	public static String encrypt(String data, String key) throws Exception {
        /*byte[] bt = encrypt(data.getBytes("UTF-8"), key.getBytes("UTF-8"));
        String strs = new BASE64Encoder().encode(bt);
        return strs;*/
        //System.out.println("新方式加密：" + new String(Base64.encodeBase64(bt)));
		byte[] bt = encrypt(data.getBytes("UTF-8"), key.getBytes("UTF-8"));
        String strs = new String(Base64.encodeBase64(bt));
		return strs;
    }
    
    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws Exception,
            Exception {
        if (data == null) {
			return null;
		}
        /*BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf,key.getBytes("UTF-8"));
        return new String(bt, "UTF-8");*/
        byte[] dataByte = Base64.decodeBase64(data.getBytes("UTF-8"));
        byte[] btRs = decrypt(dataByte,key.getBytes("UTF-8"));
        //System.out.println("新方式解密：" + new String(btRs, "UTF-8"));
        return new String(btRs, "UTF-8");
    }
    
    /**
     * Description 根据键值进行加密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);
        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);
        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }
    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);
        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(/*"DES/CBC/PKCS5Padding"*/DES);
        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }
    
    public static void main(String[] args) throws Exception{
//        try {
//            String res = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><response><header><resultCode>1010</resultCode><resultMessage>邮件发送失败，请稍后重试！</resultMessage></header></response>";
//            Document resDoc = DocumentHelper.parseText(res);
//            Element root = resDoc.getRootElement();
//            Element header = root.element("header");
//            String resultCode = header.elementText("resultCode");
//            System.out.println(resultCode);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        System.out.println(Base64Util.encode("123"));
//        System.out.println(encrypt("111111","tnciEgpo9250iwX5fuJO0bG1"));
//        System.out.println(decrypt("jBx2+bhFv8BbDVZigUhtvroltKxKY6exLKoAXczhrptBbEwV0tuNWEoto77UoeBoHqcWOZIP5tXKaFucOtHcW/lWi6ynuL5hfFXVx+aSgzWsfaZ51hRQPj3h4B5DnSrYInn0jouTQsGSIQxZS/ugTJaziENsSjPYUFMRpiVEnwBQAkTqwRu85H4Tp3bj5piur6fkNwjn/5d24Q0sk5G2VAIje09Pzc8Q45DnSvj9GuVFbHh3UvaIsXLKlXuX4FS7EQtqEZqFz1UFFWuFMYmG2ihspWtwHZXuTQ3Z2HXNr/QuCBXNG9tpj8vfrM4SYB2RnHCHIxXHzQi0kBWzeAtYXvqWzXUYYiHMvOIGaMxiv9KKzt8R2Rv3iDXB7y7Peb3OHDZYF0OL06Q=","TFD9BEOK"));
	}
}
