forked from gzzfw/backEnd/gz-dyh

xusd
2024-09-18 6ae0901878f342a9c1025c8edc302bdf03f58a6d
dyh-service/dyh-base/src/main/java/cn/huge/base/common/utils/AesUtils.java
@@ -2,10 +2,17 @@
import cn.huge.base.common.exception.BaseException;
import com.thoughtworks.xstream.core.util.Base64Encoder;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import javax.crypto.Cipher;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
 * @title: aes加密工具
@@ -29,8 +36,13 @@
     */
    public static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    private static final String iv = "1qaz#EDC123456(,";
    private static final String Algorithm = "AES";
    private static final String AlgorithmProvider = "AES/CBC/PKCS5Padding";
    /**
     * 加密
     *
     * @param content
     * @return
     * @throws Exception
@@ -51,6 +63,7 @@
    /**
     * 加密
     *
     * @param content
     * @return
     * @throws Exception
@@ -71,6 +84,7 @@
    /**
     * 加密
     *
     * @param data 返回数据
     * @return ReturnBo
     * @throws Exception
@@ -78,7 +92,7 @@
    public static byte[] encodeByte(Object data) {
        try {
            //加密
            byte[] raw =AES_PASSWORD.getBytes();
            byte[] raw = AES_PASSWORD.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
@@ -92,6 +106,7 @@
    /**
     * 解密
     *
     * @param content
     * @return
     * @throws Exception
@@ -113,6 +128,7 @@
    /**
     * 解密
     *
     * @param content
     * @return
     * @throws Exception
@@ -128,12 +144,35 @@
            byte[] contentByte = new Base64().decode(content);
            byte[] result = cipher.doFinal(contentByte);
            //AES解密
            return new String(result,"utf-8");
            return new String(result, "utf-8");
        } catch (Exception e) {
            throw new BaseException("AesUtils.decode", e);
        }
    }
    public static String decrypt(String enc, String uniqueKey) throws NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidAlgorithmParameterException, InvalidKeyException, DecoderException, IllegalBlockSizeException, BadPaddingException {
        byte[] key = uniqueKey.getBytes();
        SecretKey secretKey = new SecretKeySpec(key, Algorithm);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("utf-8"));
        Cipher cipher = Cipher.getInstance(AlgorithmProvider);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
        byte[] hexBytes = hexStringToBytes(enc);
        byte[] plainBytes = cipher.doFinal(hexBytes);
        return new String(plainBytes, "UTF-8");
    }
    /**
     * 将16进制字符串转换为byte数组
     *
     * @param hexString
     * @return
     */
    private static byte[] hexStringToBytes(String hexString) throws DecoderException {
        return Hex.decodeHex(hexString);
    }
    /**
     * 字节数组转Base64编码
     *