From 14de918a79943e4961b09fa01ed320c6cad41f2e Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 28 六月 2023 17:14:51 +0800
Subject: [PATCH] Revert "Revert "Merge branch 'hxb' into wjc""

---
 HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/AesUtil.java |  150 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 150 insertions(+), 0 deletions(-)

diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/AesUtil.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/AesUtil.java
new file mode 100644
index 0000000..942e76e
--- /dev/null
+++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/AesUtil.java
@@ -0,0 +1,150 @@
+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瑙e瘑
+     *
+     * @param key
+     * @return
+     * @throws Exception
+     */
+    public static byte[] aesDecrypt(byte[] content, String key) {
+        if (content == null || TextUtils.isEmpty(key)) {
+            LogUtils.i("鏁版嵁瑙e瘑锛�","content锛�"+content+" key:"+key);
+            return null;
+        }
+        return decrypt(content, key.getBytes(), CIPHER_INSTANCE, true, null);
+    }
+
+
+    /**
+     * 鍔犲瘑
+     *
+     * @param content        寰呭姞瀵嗗瓧绗︿覆
+     * @param keyByte        aesKye鐨刪exStr
+     * @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;
+    }
+
+    /**
+     * 瑙e瘑
+     *
+     * @param contentByte    寰呰В瀵嗗緟瀛楃涓瞙exStr
+     * @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);
+            //瑙e瘑
+            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;
+    }
+
+}

--
Gitblit v1.8.0