package com.hdl.sdk.link.core.utils;
|
|
|
import android.text.TextUtils;
|
|
import com.hdl.sdk.link.common.utils.LogUtils;
|
|
import java.security.InvalidAlgorithmParameterException;
|
import java.security.InvalidKeyException;
|
import java.security.Key;
|
import java.security.NoSuchAlgorithmException;
|
import javax.crypto.BadPaddingException;
|
import javax.crypto.Cipher;
|
import javax.crypto.IllegalBlockSizeException;
|
import javax.crypto.NoSuchPaddingException;
|
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.SecretKeySpec;
|
|
/**
|
* Aes 加解密工具类
|
*
|
* @author yangtao
|
* 2020年10月29日
|
*/
|
public class AesUtil {
|
|
private static final String CIPHER_INSTANCE = "AES/CBC/PKCS7Padding";
|
private static final String AES_ALGORITHM = "AES";
|
|
// static {
|
// //AES/CBC/PKCS7Padding 依赖
|
// Security.addProvider(new BouncyCastleProvider());
|
// }
|
|
/**
|
* AES加密
|
*
|
* @param key
|
* @return
|
* @throws Exception
|
*/
|
public static byte[] aesEncrypt(byte[] content, String key) {
|
return encrypt(content, key.getBytes(), CIPHER_INSTANCE, true, null);
|
}
|
|
/**
|
* AES解密
|
*
|
* @param key
|
* @return
|
* @throws Exception
|
*/
|
public static byte[] aesDecrypt(byte[] content, String key) {
|
if (content == null || TextUtils.isEmpty(key)) {
|
LogUtils.i("数据解密:","content:"+content+" key:"+key);
|
return null;
|
}
|
return decrypt(content, key.getBytes(), CIPHER_INSTANCE, true, null);
|
}
|
|
|
/**
|
* 加密
|
*
|
* @param content 待加密字符串
|
* @param keyByte aesKye的hexStr
|
* @param cipherInstance AES算法填充方式
|
* @param isIv 是否使用偏移量 ECB模式不可用
|
* @param iv 偏移量 不传默认密钥
|
* @return
|
*/
|
public static byte[] encrypt(byte[] content, byte[] keyByte, String cipherInstance, Boolean isIv, byte[] iv) {
|
try {
|
//KEY转换
|
Key key = new SecretKeySpec(keyByte, AES_ALGORITHM);
|
//加密
|
Cipher cipher = Cipher.getInstance(cipherInstance);
|
if (isIv != null && isIv) {
|
//偏移量 不传默认密钥
|
if (iv == null) {
|
iv = keyByte;
|
}
|
IvParameterSpec ivps = new IvParameterSpec(iv);
|
cipher.init(Cipher.ENCRYPT_MODE, key, ivps);
|
} else {
|
cipher.init(Cipher.ENCRYPT_MODE, key);
|
}
|
byte[] result = cipher.doFinal(content);
|
return result;
|
} catch (NoSuchAlgorithmException e) {
|
LogUtils.e(e.getMessage());
|
} catch (InvalidKeyException e) {
|
LogUtils.e(e.getMessage());
|
} catch (NoSuchPaddingException e) {
|
LogUtils.e(e.getMessage());
|
} catch (BadPaddingException e) {
|
LogUtils.e(e.getMessage());
|
} catch (IllegalBlockSizeException e) {
|
LogUtils.e(e.getMessage());
|
} catch (InvalidAlgorithmParameterException e) {
|
LogUtils.e(e.getMessage());
|
}
|
return null;
|
}
|
|
/**
|
* 解密
|
*
|
* @param contentByte 待解密待字符串hexStr
|
* @param contentByte 密钥
|
* @param cipherInstance AES算法填充方式
|
* @param isIv 是否使用偏移量 ECB模式不可用
|
* @param iv 偏移量 不传默认密钥
|
* @return
|
*/
|
public static byte[] decrypt(byte[] contentByte, byte[] keyByte, String cipherInstance, Boolean isIv, byte[] iv) {
|
try {
|
//KEY转换
|
Key key = new SecretKeySpec(keyByte, AES_ALGORITHM);
|
//解密
|
Cipher cipher = Cipher.getInstance(cipherInstance);
|
if (isIv != null && isIv) {
|
//偏移量 不传默认密钥
|
if (iv == null) {
|
iv = keyByte;
|
}
|
IvParameterSpec ivps = new IvParameterSpec(iv);
|
cipher.init(Cipher.DECRYPT_MODE, key, ivps);
|
} else {
|
cipher.init(Cipher.DECRYPT_MODE, key);
|
}
|
byte[] result = cipher.doFinal(contentByte);
|
return result;
|
} catch (NoSuchAlgorithmException e) {
|
LogUtils.e(e.getMessage());
|
} catch (InvalidKeyException e) {
|
LogUtils.e(e.getMessage());
|
} catch (NoSuchPaddingException e) {
|
LogUtils.e(e.getMessage());
|
} catch (BadPaddingException e) {
|
LogUtils.e(e.getMessage());
|
} catch (IllegalBlockSizeException e) {
|
LogUtils.e(e.getMessage());
|
} catch (InvalidAlgorithmParameterException e) {
|
LogUtils.e(e.getMessage());
|
}
|
return null;
|
}
|
|
}
|