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/mqtt/AesUtil.java | 286 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 286 insertions(+), 0 deletions(-) diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/mqtt/AesUtil.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/mqtt/AesUtil.java new file mode 100644 index 0000000..01fbe3f --- /dev/null +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/mqtt/AesUtil.java @@ -0,0 +1,286 @@ +package com.hdl.sdk.link.core.utils.mqtt; + + + +import com.hdl.sdk.link.common.utils.LogUtils; +import com.hdl.sdk.link.core.utils.HexUtil; + +import org.bouncycastle.jce.provider.BouncyCastleProvider; + +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.security.Security; + +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/ECB/PKCS5Padding"; + 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(), "AES/CBC/PKCS7Padding", true, null); + } + + /** + * AES瑙e瘑 + * + * @param key + * @return + * @throws Exception + */ + public static byte[] aesDecrypt(byte[] content, String key) { + return decrypt(content, key.getBytes(), "AES/CBC/PKCS7Padding", true, null); + } + + /** + * 鍔犲瘑 + * + * @param content 寰呭姞瀵嗗瓧绗︿覆 + * @param keyHex aesKye鐨刪exStr + * @return + */ + public static String encrypt(String content, String keyHex) { + return HexUtil.encodeHexStr(encrypt(content.getBytes(), HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null)); + } + + /** + * 鍔犲瘑 + * + * @param content 寰呭姞瀵嗗瓧绗︿覆 + * @param key 瀵嗛挜 + * @return + */ + public static String encrypt(String content, byte[] key) { + return HexUtil.encodeHexStr(encrypt(content.getBytes(), key, CIPHER_INSTANCE, false, null)); + } + + /** + * 鍔犲瘑 + * + * @param content 寰呭姞瀵嗗瓧绗︿覆 + * @param keyHex aesKye鐨刪exStr + * @return + */ + public static byte[] encryptReturnByte(String content, String keyHex) { + return encrypt(content.getBytes(), HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null); + } + + /** + * 鍔犲瘑 + * + * @param content 寰呭姞瀵嗗瓧绗︿覆 + * @param key 瀵嗛挜 + * @return + */ + public static byte[] encryptReturnByte(String content, byte[] key) { + return encrypt(content.getBytes(), key, CIPHER_INSTANCE, false, 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 contentHex 寰呰В瀵嗗緟瀛楃涓瞙exStr + * @param keyHex desKye鐨刪exStr + * @return + */ + public static String decrypt(String contentHex, String keyHex) { + return new String(decrypt(HexUtil.decodeHex(contentHex), HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null)); + } + + /** + * 瑙e瘑 + * + * @param contentHex 寰呰В瀵嗗緟瀛楃涓瞙exStr + * @param key 瀵嗛挜 + * @return + */ + public static String decrypt(String contentHex, byte[] key) { + return new String(decrypt(HexUtil.decodeHex(contentHex), key, CIPHER_INSTANCE, false, null)); + } + + /** + * 瑙e瘑 + * + * @param content 寰呰В瀵嗗緟鍐呭 + * @param keyHex 瀵嗛挜 + * @return + */ + public static String decrypt(byte[] content, String keyHex) { + return new String(decrypt(content, HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null)); + } + + /** + * 瑙e瘑 + * + * @param content 寰呰В瀵嗗緟鍐呭 + * @param key 瀵嗛挜 + * @return + */ + public static String decrypt(byte[] content, byte[] key) { + return new String(decrypt(content, key, CIPHER_INSTANCE, false, 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; + } + + + public static void main(String[] args) { +// jiemi(); + //jiami(); + } + + public static void jiami() { + String key = "1f6714fc-fb9b-4a"; + String content = "{\"id\":\"123\",\"objects\":[{\"sid\":\"0001011565879801020200010101\",\"type\":\"PWD\",\"local_id\":\"2\"}],\"time_stamp\":\"1603281282000\"}"; + byte[] res = aesEncrypt(content.getBytes(), key); + System.out.println(HexUtil.encodeHexStr(res)); + } + + /** + * + * @param str + * @param houseId + * @return + */ + public static String jiemi(byte[] str, String houseId) { + byte[] rs = aesDecrypt(str, HouseIdSecretUtil.getSecret(houseId)); + String content = new String(rs); + return content; + } + +// public static void main(String[] args) { +// try { +// //绗竴姝ワ細 鐢熸垚KEY +// //KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_ALGORITHM); +// //keyGenerator.init(256); +// //绗簩姝ワ細 浜х敓瀵嗛挜 +// //SecretKey secretKey = keyGenerator.generateKey(); +// //绗笁姝ワ細 鑾峰彇瀵嗛挜 +// //byte[] keyBytes = secretKey.getEncoded(); +// //System.out.println(byteArrayToHexStr(keyBytes)); +// String src = "aaaaaaaaaVVVVaaaAAAA鏃�"; +// String pwd = "41151AF257BFDB7859EEC62FB341EE95EE42E648FE24E1F8CE8DADE287CC1E5C"; +// String mw = encrypt(src,pwd); +// System.out.println("mw:"+mw); +// String jm = decrypt(mw,pwd); +// System.out.println("jm:"+jm); +// +// }catch (Exception e){ +// +// } +// +// } +} -- Gitblit v1.8.0