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/common/utils/ByteUtils.java | 243 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 243 insertions(+), 0 deletions(-) diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/ByteUtils.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/ByteUtils.java new file mode 100644 index 0000000..e4f6a9d --- /dev/null +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/ByteUtils.java @@ -0,0 +1,243 @@ +package com.hdl.sdk.link.common.utils; + +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +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; + +/** + * Created by Tong on 2021/9/23. + */ +public class ByteUtils { + + public static byte[] toByteArray(List<Byte> list) { + Byte[] temps = list.toArray(new Byte[0]); + byte[] result = new byte[temps.length]; + for (int i = 0; i < result.length; i++) { + result[i] = temps[i]; + } + return result; + + } + + + public static List<Byte> toByteList(byte[] bytes) { + final List<Byte> list = new ArrayList<>(); + for (byte aByte : bytes) { + list.add(aByte); + } + return list; + + } + + public static byte[] getRangeBytes(List<Byte> list, int start, int end) { + Byte[] temps = Arrays.copyOfRange(list.toArray(new Byte[0]), start, end); + byte[] result = new byte[temps.length]; + for (int i = 0; i < temps.length; i++) { + result[i] = temps[i]; + } + return result; + + } + + public static byte[] copyBytes(byte bytes[], int index, int length) { + byte[] result = new byte[length]; + for (int i = 0; i < result.length; i++) { + result[i] = bytes[index + i]; + } + return result; + } + + /** + * 鎷兼帴byte + */ + public static byte[] concatBytes(byte[] bt1, byte[] bt2) { + if (bt1 == null) { + return bt2; + } + if (bt2 == null) { + return bt1; + } + byte[] bt3 = new byte[bt1.length + bt2.length]; + System.arraycopy(bt1, 0, bt3, 0, bt1.length); + System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length); + return bt3; + } + + + public boolean endWith(Byte[] src, byte[] target) { + if (src.length < target.length) { + return false; + } + for (int i = 0; i < target.length; i++) { + if (target[target.length - i - 1] != src[src.length - i - 1]) { + return false; + } + } + return true; + } + + + public static int byteIndexOf(byte[] searched, byte[] find, int start) { + boolean matched; + int end = find.length - 1; + int skip = 0; + for (int index = start; index <= searched.length - find.length; ++index) { + matched = true; + if (find[0] != searched[index] || find[end] != searched[index + end]) continue; + else skip++; + if (end > 10) + if (find[skip] != searched[index + skip] || find[end - skip] != searched[index + end - skip]) + continue; + else skip++; + for (int subIndex = skip; subIndex < find.length - skip; ++subIndex) { + if (find[subIndex] != searched[index + subIndex]) { + matched = false; + break; + } + } + if (matched) { + return index; + } + } + return -1; + + } + + public static int getByteIndexOf(byte[] sources, byte[] src) { + return getByteIndexOf(sources, src, 0, sources.length); + } + + //鍒ゆ柇涓�涓猙yte鏁板�煎湪鍙﹀涓�涓猙yte鏁扮粍涓搴旂殑娓告爣鍊� + public static int getByteIndexOf(byte[] sources, byte[] src, int startIndex) { + return getByteIndexOf(sources, src, startIndex, sources.length); + } + + + //鍒ゆ柇涓�涓猙yte鏁板�煎湪鍙﹀涓�涓猙yte鏁扮粍涓搴旂殑娓告爣鍊硷紝鎸囧畾寮�濮嬬殑娓告爣鍜岀粨鏉熺殑娓告爣浣嶇疆 + public static int getByteIndexOf(byte[] sources, byte[] src, int startIndex, int endIndex) { + + if (sources == null || src == null || sources.length == 0 || src.length == 0) { + return -1; + } + + if (endIndex > sources.length) { + endIndex = sources.length; + } + + int i, j; + for (i = startIndex; i < endIndex; i++) { + if (sources[i] == src[0] && i + src.length < endIndex) { + for (j = 1; j < src.length; j++) { + if (sources[i + j] != src[j]) { + break; + } + } + + if (j == src.length) { + return i; + } + } + } + return -1; + } + + /** + * 瀛楃涓瞭o Bytes + * + * @param str 瀛楃涓� + * @return + */ + public static byte[] stringToBytes(String str) { + try { + // 浣跨敤鎸囧畾鐨勫瓧绗﹂泦灏嗘瀛楃涓茬紪鐮佷负byte搴忓垪骞跺瓨鍒颁竴涓猙yte鏁扮粍涓� + return str.getBytes("utf-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return new byte[]{}; + } + + public static String byte2hex(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + String tmp = null; + for (byte b : bytes) { + //灏嗘瘡涓瓧鑺備笌0xFF杩涜涓庤繍绠楋紝鐒跺悗杞寲涓�10杩涘埗锛岀劧鍚庡�熷姪浜嶪nteger鍐嶈浆鍖栦负16杩涘埗 + tmp = Integer.toHexString(0xFF & b); + if (tmp.length() == 1) { + tmp = "0" + tmp; + } + sb.append(tmp + " "); + } + return sb.toString(); + } + + + public static int bytes2int(byte[] bytes) { + return bytes[3] & 0xFF | // + (bytes[2] & 0xFF) << 8 | // + (bytes[1] & 0xFF) << 16 | // + (bytes[0] & 0xFF) << 24; // + } + + + public static byte[] intToByteArray(int i) { + byte[] result = new byte[4]; + result[0] = (byte) ((i >> 24) & 0xFF); + result[1] = (byte) ((i >> 16) & 0xFF); + result[2] = (byte) ((i >> 8) & 0xFF); + result[3] = (byte) (i & 0xFF); + return result; + } + + public static int byteArrayToInt(byte[] b) { + int i = (b[0] & 0xFF) * 256 * 256 * 256 + (b[1] & 0xFF) * 256 * 256 + (b[2] & 0xFF) * 256 + (b[3] & 0xFF); + return i; + } + + /** + * 瑙e瘑 + * + * @param contentByte 寰呰В瀵嗗緟瀛楃涓瞙exStr + * @param contentByte 瀵嗛挜 + * @return + */ + public static byte[] decrypt(byte[] contentByte) { + try { + //KEY杞崲 + Key key = new SecretKeySpec("HDLRDCENTER1985.".getBytes(), "AES"); + //瑙e瘑 + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); + IvParameterSpec ivps = new IvParameterSpec("HDLRDCENTER1985.".getBytes()); + cipher.init(Cipher.DECRYPT_MODE, key, ivps); + 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