/*
 * Created on 2005-7-4
 * Project redmaple
 */
package bggxy.CL.sjg;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.crypto.spec.*;
import java.security.*;
import javax.crypto.*;/**
 * 加密解密。
 * 
 * @author editor>ghost </a>
 */
public class SecurityCode
{
    /**
     *  
     */
    private static final Log logger = LogFactory.getLog(SecurityCode.class);    public static final String DES = "DES";    public static final String DESede = "DESede";    public static final String Blowfish = "Blowfish";    private String key = null;    private SecretKey deskey = null;    private String algorithm = DES;    public SecurityCode()
    {
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
    }    public void generateKey()
    {
        try
        {
            KeyGenerator keygen = KeyGenerator.getInstance(algorithm);
            deskey = keygen.generateKey();
            this.key = byte2hex(deskey.getEncoded());
        } catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
    }    public String encrypt(String info)
    {
        return encrypt(info, null, null);
    }    public String encrypt(String info, String key)
    {
        return encrypt(info, key, null);
    }    /**
     * 加密
     * 
     * @param info
     * @param key
     * @param algorithm
     * @return
     */
    public String encrypt(String info, String key, String algorithm)
    {
        if (info == null)
        {
            //logger.error("info1 is null");
            return null;
        }
        if (key != null)
            this.setKey(key);
        if (algorithm != null)
            this.setAlgorithm(algorithm);
        if (this.deskey == null)
        {
            logger.error("deskey is null");
            return null;
        }        try
        {
            //加密
            Cipher c = Cipher.getInstance(this.algorithm);
            c.init(Cipher.ENCRYPT_MODE, this.deskey);
            byte[] cipherByte = c.doFinal(info.getBytes());
            return byte2hex(cipherByte);
        } catch (java.security.NoSuchAlgorithmException e1)
        {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2)
        {
            e2.printStackTrace();
        } catch (java.lang.Exception e3)
        {
            e3.printStackTrace();
        }
        return null;
    }    public String decrypt(String info)
    {
        return decrypt(info, null, null);
    }    public String decrypt(String info, String key)
    {
        return decrypt(info, key, null);
    }    /**
     * 解密
     * 
     * @param info
     * @return
     */
    public String decrypt(String info, String key, String algorithm)
    {
        if (info == null)
        {
            //logger.error("info is null");
            return null;
        }
        if (key != null)
            this.setKey(key);
        if (algorithm != null)
            this.setAlgorithm(algorithm);
        if (this.deskey == null)
        {
            logger.error("deskey is null");
            return null;
        }
        try
        {
            Cipher c = Cipher.getInstance(this.algorithm);
            c.init(Cipher.DECRYPT_MODE, this.deskey);
            byte[] cipherByte = c.doFinal(hex2byte(info));
            return (new String(cipherByte));
        } catch (java.security.NoSuchAlgorithmException e1)
        {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2)
        {
            e2.printStackTrace();
        } catch (java.lang.Exception e3)
        {
            e3.printStackTrace();
        }
        return null;
    }    /**
     * 二进制转字符串。
     * 
     * @param b
     * @return
     */
    private String byte2hex(byte[] b)
    {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++)
        {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
            if (n < b.length - 1)
                hs = hs + ":";
        }
        return hs.toUpperCase();
    }    /**
     * 字符串转二进制。
     * 
     * @param hex
     * @return
     */
    private byte[] hex2byte(String hex)
    {
        String[] h = hex.split(":");
        byte[] b = new byte[h.length];
        for (int i = 0; i < h.length; i++)
        {
            int byteint = Integer.parseInt(h[i], 16) & 0xFF;
            b[i] = new Integer(byteint).byteValue();
        }
        return b;
    }    /**
     * @return Returns the algorithm.
     */
    public String getAlgorithm()
    {
        return algorithm;
    }    /**
     * @param algorithm
     *            The algorithm to set.
     */
    public void setAlgorithm(String algorithm)
    {
        this.algorithm = algorithm;
    }    /**
     * @return Returns the key.
     */
    public String getKey()
    {
        return key;
    }    /**
     * @param key
     *            The key to set.
     */
    public void setKey(String key)
    {
        this.key = key;
        this.deskey = new SecretKeySpec(hex2byte(key), algorithm);
    }
}