package com.fr.tx.common.util;

import java.io.IOException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DesCmUtil {

	private final static String DES = "DES";
	
	private final static String ENKEY = "wang!@#$%";
	 
    public static void main(String[] args) throws Exception {
//        String data = "0e010c0104003d20151104103128010000000036930000001003000000151100004533000001294240201511041031380102041110050000000200000000000000000000c5";
//        String key = "wang!@#$%";
//        String kk = "Av1oyblZ+IwjUjsU81gnQzNqWbXSRozwemx2CYb2oqGYvTRmJGc+aa9qSzbMkl0DVOumFy9runlu8pH4ZcE+owmNEQJ8sqSV9uuDUkFJhPSMgUb7qdYlCpYY+BEmoaZqlW4eobDPu5+IW8BbvmImTRfBmHjaLpIrmL00ZiRnPmmYvTRmJGc+aYmz25LvTbtl";
//        String kk = "45RIr682fMQjUjsU81gnQ9TYBqRp4SfQPriipbfjGnJxcZ3G8skikDjCoOoTrtZvAZaXXXy3uAwYtlJCUHzBQl2b7soBkCauTwn04T6sQVUNx8k1Mb+5BhzQ8tciuPEXlW4eobDPu5+IW8BbvmImTfX6WoH8btD8mL00ZiRnPmmYvTRmJGc+abkTBLrb4hnm";
        String kk = "ghL7sFbtJRoK3vtRsFQUMdRcQZSOppG8YYCHCy+86mdDNsvw7wsuYZEX+IjrTPCzgdd9C2itrS3cE+jc65/2sb1LffyMe3ED";
//        String kk = "x1ogSI3LmKnbJYphlXF8zLCsIS+bJ+nuPNbFmGwJ/iAjBt4pndYsHTCTqBBFZnvNQ+VokiYOH/uxG2u3+0D8/BfEavPGIrJfA+2UpZ5oyCczyKK/+87EWrTGd3YtP74JJGY9yv7kFDmtEWOcQcEcPg==";
        System.out.println(decrypt(kk));
//        System.out.println(encrypt("001"));

//        System.out.println(encrypt(data, key));
//        System.out.println(decrypt(encrypt(data, key), key));
 
    }
    
    /**
     * Description 根据键值进行加密
     * @param data 
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    public static String encrypt(String data) throws Exception {
        byte[] bt = encrypt(data.getBytes(), ENKEY.getBytes());
        String strs = new BASE64Encoder().encode(bt);
        return strs;
    }
    
    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data) throws IOException,
            Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf,ENKEY.getBytes());
        return new String(bt);
    }
    
    /**
     * Description 根据键值进行加密
     * @param data 
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
        byte[] bt = encrypt(data.getBytes(), key.getBytes());
        String strs = new BASE64Encoder().encode(bt);
        return strs;
    }
 
    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException,
            Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf,key.getBytes());
        return new String(bt);
    }
 
    /**
     * 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);
 
        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
 
        return cipher.doFinal(data);
    }

}
