AES加解密

package com.chinaunicom.standard.product.ucenter.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.UUID;

/**
 * @author ggw
 */
public class AESUtils {

    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";//默认的加密算法
    private static final String IV_STRING = "ABCDEF1234123412";
    private static final String UTF8 = "utf-8";
    private static String AES_KEY = null;
    public static String MOBILE_AES_KEY = "825ef073c3ac4186";



    private static String generateKey() {
        AES_KEY = UUID.randomUUID().toString().replaceAll("-", "");
        if (AES_KEY.length() > 16) {
            AES_KEY = AES_KEY.substring(0, 16);
        }
        return AES_KEY;
    }

    public static String getAesKey() {
        if (checkAesKey()) {
            return AES_KEY;
        } else {
            return generateKey();
        }
    }

    public static boolean checkAesKey() {
        if (AES_KEY == null) {
            return false;
        } else {
            return true;
        }
    }

    public static String encrypt(String content) {
        return encrypt(content, getAesKey());
    }

    public static String encrypt(String content, String key) {
        try {
            byte[] byteContent = content.getBytes(UTF8);
            //这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
            byte[] enCodeFormat = key.getBytes(UTF8);

            //指定加密的算法、工作模式和填充方式
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
            byte[] initParam = IV_STRING.getBytes(UTF8);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

            byte[] encryptedBytes = cipher.doFinal(byteContent);
            //对加密后数据进行 base64 编码
            return Base64.encodeToString(encryptedBytes, Base64.NO_WRAP);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decrypt(String content) {
        return decrypt(content, getAesKey());
    }

    public static String decrypt(String content, String key) {
        try {
            // base64 解码
            byte[] encryptedBytes = Base64.decode(content, Base64.NO_WRAP);
            byte[] enCodeFormat = key.getBytes(UTF8);

            //指定加密的算法、工作模式和填充方式
            SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
            byte[] initParam = IV_STRING.getBytes(UTF8);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);

            byte[] result = cipher.doFinal(encryptedBytes);
            return new String(result, UTF8);
        } catch (Exception e) {

        }
        return null;
    }


    public static String encrypt(String content, String key,boolean useIvParameterSpec) {
        try {
            byte[] byteContent = content.getBytes(UTF8);
            //这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
            byte[] enCodeFormat = key.getBytes(UTF8);

            //指定加密的算法、工作模式和填充方式
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
            byte[] initParam = IV_STRING.getBytes(UTF8);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            if (useIvParameterSpec){
                IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
                cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
            }else{
                cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            }

            byte[] encryptedBytes = cipher.doFinal(byteContent);
            //对加密后数据进行 base64 编码
            return Base64.encodeToString(encryptedBytes, Base64.NO_WRAP);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值