package com.hdl.sdk.ttl.Utils.HDLUtlis; import android.util.Log; import java.io.UnsupportedEncodingException; 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) { 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) { Log.e("AesUtil",e.getMessage()); } catch (InvalidKeyException e) { Log.e("AesUtil",e.getMessage()); } catch (NoSuchPaddingException e) { Log.e("AesUtil",e.getMessage()); } catch (BadPaddingException e) { Log.e("AesUtil",e.getMessage()); } catch (IllegalBlockSizeException e) { Log.e("AesUtil",e.getMessage()); } catch (InvalidAlgorithmParameterException e) { Log.e("AesUtil",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) { Log.e("AesUtil",e.getMessage()); } catch (InvalidKeyException e) { Log.e("AesUtil",e.getMessage()); } catch (NoSuchPaddingException e) { Log.e("AesUtil",e.getMessage()); } catch (BadPaddingException e) { Log.e("AesUtil",e.getMessage()); } catch (IllegalBlockSizeException e) { Log.e("AesUtil",e.getMessage()); } catch (InvalidAlgorithmParameterException e) { Log.e("AesUtil",e.getMessage()); } return null; } /** * 字符串to Bytes * @param str 字符串 * @return */ public static byte[] stringToBytes(String str) { try { // 使用指定的字符集将此字符串编码为byte序列并存到一个byte数组中 return str.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return new byte[]{}; } }