package com.nuliji.tools;


import java.io.IOException;
import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

public class CipherHelp {

    public final static String AES = "AES";
    public final static String DES = "DES";
    public final static String DES3 = "3DESC";

    /**
     * encrypt base on DES or 3DES or AES.
     *
     * @param target
     *            The target (which is used to encrypt)
     * @param algorithm
     *            The algorithm can be DES or DESede or AES
     * @param key
     *            The key (which is key to handle this encrypt)
     * @return
     */
    public static String encrypt(String target, String algorithm, String key) {
        try {

            byte[] targetToByte = target.getBytes("UTF-8");
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.ENCRYPT_MODE, getKey(key, algorithm));
            byte[] result = cipher.doFinal(targetToByte);
            // System.out.println("base64:" +encryptByBase64(result));
            return Tools.encodeBase64(result); // 需要BASE64包装一下，否则会抛出异常

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * decrypt base on DES or 3DES or AES.
     *
     * @param target
     *            The target (which is used to decrypt)
     * @param algorithm
     *            The algorithm can be DES or DESede or AES
     * @param key
     *            The key (which is key to handle this decrypt)
     * @return
     */
    public static String decrypt(String target, String algorithm, String key) {

        try {

            byte[] targetToByte = Tools.decodeBase64(target); // 需要BASE64包装一下，否则会抛出异常
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.DECRYPT_MODE, getKey(key, algorithm));
            byte[] result = cipher.doFinal(targetToByte);
            // System.out.println("base:" + new String(result,"UTF-8"));
            return new String(result, "UTF-8");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Key getKey(String key, String algorithm) {
        try {
            KeyGenerator generator = KeyGenerator.getInstance(algorithm);
            generator.init(new SecureRandom(key.getBytes()));
            return generator.generateKey();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    public static void main(String[] args) {
        String target = "测试";
        // String target = "test";
        // DES
        String encrypt = encrypt(target, "DES", "im a key");
        System.out.println("加密后：" + encrypt);
        String decrypt = decrypt(encrypt, "DES", "im a key");
        System.out.println("解密后：" + decrypt);
        // 3DES
        /*
         * String encrypt = encrypt(target, "DESede", "im a key");
         * System.out.println("加密后：" + encrypt); String decrypt =
         * decrypt(encrypt, "DESede", "im a key"); System.out.println("解密后：" +
         * decrypt);
         */
        // AES
        /*
         * String encrypt = encrypt(target, "AES", "im a key");
         * System.out.println("加密后：" + encrypt); String decrypt =
         * decrypt(encrypt, "AES", "im a key"); System.out.println("解密后：" +
         * decrypt);
         */
    }

}

