New file |
| | |
| | | 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);
|
| | | }
|
| | |
|
| | | //判断一个byte数值在另外一个byte数组中对应的游标值
|
| | | public static int getByteIndexOf(byte[] sources, byte[] src, int startIndex) {
|
| | | return getByteIndexOf(sources, src, startIndex, sources.length);
|
| | | }
|
| | |
|
| | |
|
| | | //判断一个byte数值在另外一个byte数组中对应的游标值,指定开始的游标和结束的游标位置
|
| | | 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;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 字符串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[]{};
|
| | | }
|
| | |
|
| | | public static String byte2hex(byte[] bytes) {
|
| | | StringBuilder sb = new StringBuilder();
|
| | | String tmp = null;
|
| | | for (byte b : bytes) {
|
| | | //将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为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;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 解密
|
| | | *
|
| | | * @param contentByte 待解密待字符串hexStr
|
| | | * @param contentByte 密钥
|
| | | * @return
|
| | | */
|
| | | public static byte[] decrypt(byte[] contentByte) {
|
| | | try {
|
| | | //KEY转换
|
| | | Key key = new SecretKeySpec("HDLRDCENTER1985.".getBytes(), "AES");
|
| | | //解密
|
| | | 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;
|
| | | }
|
| | |
|
| | | }
|