AES算法加密解密工具类

package com.debug.auth.server.utils;

import com.debug.auth.server.dto.AccessTokenDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.GeneralSecurityException;

//AES加密解密工具
public class EncryptUtil {

    private static final String AES_ALG         = "AES";

    private static final String charset         = "utf-8";

    //AES算法
    private static final String AES_CBC_PCK_ALG = "AES/CBC/PKCS5Padding";

    private static final byte[] AES_IV          = initIv(AES_CBC_PCK_ALG);

    /**
     * 加密
     * @param content
     * @param encryptType
     * @param encryptKey
     * @param charset
     * @return
     * @throws Exception
     */
    public static String encryptContent(String content, String encryptType, String encryptKey,String charset) throws Exception {
        if (AES_ALG.equals(encryptType)) {
            return aesEncrypt(content, encryptKey);
        } else {
            throw new Exception("当前不支持该算法类型:encrypeType=" + encryptType);
        }

    }

    /**
     * 解密
     * @param content
     * @param encryptType
     * @param encryptKey
     * @param charset
     * @return
     * @throws Exception
     */
    public static String decryptContent(String content, String encryptType, String encryptKey,String charset) throws Exception {
        if (AES_ALG.equals(encryptType)) {
            return aesDecrypt(content, encryptKey);
        } else {
            throw new Exception("当前不支持该算法类型:encrypeType=" + encryptType);
        }

    }

    /**
     * AES加密
     * @param content
     * @param aesKey
     * @return
     * @throws Exception
     */
    public static String aesEncrypt(String content, String aesKey)throws Exception {
        try {
            Cipher cipher = Cipher.getInstance(AES_CBC_PCK_ALG);

            IvParameterSpec iv = new IvParameterSpec(AES_IV);
            cipher.init(Cipher.ENCRYPT_MODE,new SecretKeySpec(aesKey.getBytes(), AES_ALG), iv);

            byte[] encryptBytes = cipher.doFinal(content.getBytes(charset));
            return new String(Base64.encodeBase64(encryptBytes));
        } catch (Exception e) {
            throw new Exception("AES加密失败:Aescontent = " + content + "; charset = " + charset, e);
        }
    }

    /**
     * AES解密
     * @param content 待解密串
     * @param key 密钥
     * @return
     * @throws Exception
     */
    public static String aesDecrypt(String content, String key)throws Exception {
        try {
            Cipher cipher = Cipher.getInstance(AES_CBC_PCK_ALG);
            IvParameterSpec iv = new IvParameterSpec(initIv(AES_CBC_PCK_ALG));
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(),
                    AES_ALG), iv);

            byte[] cleanBytes = cipher.doFinal(Base64.decodeBase64(content.getBytes()));
            return new String(cleanBytes, charset);
        } catch (Exception e) {
            throw new Exception("AES解密失败:Aescontent = " + content + "; charset = " + charset, e);
        }
    }

    /**
     * 初始向量的方法, 全部为0. 这里的写法适合于其它算法,针对AES算法的话,IV值一定是128位的(16字节).
     * @param fullAlg
     * @return
     * @throws GeneralSecurityException
     */
    private static byte[] initIv(String fullAlg) {
        try {
            Cipher cipher = Cipher.getInstance(fullAlg);
            int blockSize = cipher.getBlockSize();
            byte[] iv = new byte[blockSize];
            for (int i = 0; i < blockSize; ++i) {
                iv[i] = 0;
            }
            return iv;
        } catch (Exception e) {

            int blockSize = 16;
            byte[] iv = new byte[blockSize];
            for (int i = 0; i < blockSize; ++i) {
                iv[i] = 0;
            }
            return iv;
        }
    }

//    public static void main(String[] args) throws Exception{
//        ObjectMapper objectMapper=new ObjectMapper();
//        AccessTokenDto info=new AccessTokenDto(1,"debug",1568034072458L,"368543454793703424");
//
//        String jsonStr=objectMapper.writeValueAsString(info);
//        System.out.println("明文: "+jsonStr);
//
//        String key="e2bd6cee47e0402db80862a09ff4dfd6";
//        String encryptStr=aesEncrypt(jsonStr,key);
//        System.out.println("密文: "+ encryptStr);
//
//        String srcInfo=aesDecrypt(encryptStr,key);
//        System.out.println("明文: "+ srcInfo);
//    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值