AES加密解密工具

 

package com.lanhuigu.base.common.utils;

import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


/**
 * AES 加解密
 */
public class AESUtil {
    /**
     * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
     */
    private static final String KEY = "!QA2Z@w1sxO*(-8L";
    private static final String VECTOR = "!WFNZFU_{H%M(S|a";
    private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * 加密
     * @param  content 明文
     * @return String  密文
     * @throws Exception
     */
    public static String encrypt(String content) throws Exception {
        return encrypt(content, KEY, VECTOR);
    }

    /**
     * 加密
     * @param  content 明文
     * @param  key     加密key
     * @return String  密文
     * @throws Exception
     */
    public static String encrypt(String content, String key) throws Exception {
        return encrypt(content, key, VECTOR);
    }

    /**
     * 加密
     * @param content 明文
     * @param key     加密key
     * @param vector
     * @return String 密文
     * @throws Exception
     */
    public static String encrypt(String content, String key, String vector) throws Exception {
        if (key == null) {
            return null;
        }
        if (key.length() != 16) {
            return null;
        }
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        /** #################使用CBC模式,需要一个向量iv,可增加加密算法的强度##################*/
        /**
         * IvParameterSpec iv = new IvParameterSpec(vector.getBytes());
         * Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
         * cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
         */

        /** ##################修改为ECB模式,去掉iv################## */
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(content.getBytes("UTF-8"));
        return Base64.encodeBase64String(encrypted);
    }

    /**
     * 解密
     * @param  content 密文
     * @return String  明文
     * @throws Exception
     */
    public static String decrypt(String content) throws Exception {
        return decrypt(content, KEY, VECTOR);
    }

    /**
     * 解密
     * @param  content 密文
     * @param  key     解密key
     * @return String  明文
     * @throws Exception
     */
    public static String decrypt(String content,String key) throws Exception {
        return decrypt(content, key, VECTOR);
    }

    /**
     * 解密
     * @param   content 密文
     * @param   key     解密key
     * @param   vector
     * @return  String  明文
     * @throws Exception
     */
    public static String decrypt(String content, String key, String vector) throws Exception {
        try {
            if (key == null) {
                return null;
            }
            if (key.length() != 16) {
                return null;
            }
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            // IvParameterSpec iv = new IvParameterSpec(vector.getBytes());
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            // cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = Base64.decodeBase64(content);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "UTF-8");
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }

}

 

你可能感兴趣的:(AES加密解密工具)