AESEncryptUtils.java 3.83 KB
package com.glsx.libble.util;

import com.blankj.utilcode.util.LogUtils;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * @Package: com.glsx.ble_module.util
 * @ClassName: AESUtil
 * @Company GLSX
 * @Email yangks@didihu.com.cn
 * @Description: 作用描述:
 * @Author: yangks
 * @CreateDate: 2021/4/16 9:32
 * @Version:
 */
public class AESEncryptUtils {
    //秘钥算法
    private static final String KEY_ALGORITHM = "AES";
    //加密算法:algorithm/mode/padding 算法/工作模式/填充模式
    private static final String CIPHER_ALGORITHM = "AES/CBC/NoPadding";
    //秘钥
    //private static final String KEY = "12345678";//DES 秘钥长度必须是8 位或以上
    //private static String KEY = "1234567890123456";//AES 秘钥长度必须是16 位

    //加密
    public static byte[] encryption(byte[] mingwen, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        if (key.length() != 16) {
            LogUtils.eTag("TAG", "key is err");
            return "keylength is error".getBytes();
        }
        //将明文填充0补足128位
        byte[] tcmingwen = new byte[16];
        System.arraycopy(mingwen, 0, tcmingwen, 0, mingwen.length);
        //初始化秘钥
        SecretKey encryptionKey = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        //AES、DES 在CBC 操作模式下需要iv 参数
        IvParameterSpec iv = new IvParameterSpec(key.getBytes());
        //加密
        cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, iv);
        byte[] result = cipher.doFinal(tcmingwen);
        //LogUtils.e("yhj","加密密文是("+result.length+"):"+ToolUtils.bytes2HexString(result));
        return result;
    }

    //解密
    public static byte[] decryption(byte[] miwen, String key) {
        if (key.length() != 16) {
            LogUtils.eTag("TAG", "key is err");
            return "keylength is error".getBytes();
        }
        //初始化秘钥
        SecretKey decryptionKey = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM);
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
        //AES、DES 在CBC 操作模式下需要iv 参数
        IvParameterSpec iv = new IvParameterSpec(key.getBytes());
        //解密
        try {
            cipher.init(Cipher.DECRYPT_MODE, decryptionKey, iv);
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        byte[] result = new byte[0];
        try {
            result = cipher.doFinal(miwen);
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
//        //读取消息体长度,删除多余的0
//        byte[] msgBodyAttributes = Arrays.copyOfRange(result, 2, 4);
//        int BodyLength = BitOperator.msgHeaderGetBodyLength(msgBodyAttributes);
//        byte[] realresult = Arrays.copyOfRange(result, 0, BodyLength+13);
//
//        LogUtils.e("yhj","去0明文是("+realresult.length+"):"+bytesToHexString(realresult));
        return result;
    }
}