MessageTestDemo/src/main/java/com/example/demo/kafka/encrypt/Util.java

34 lines
981 B
Java
Raw Normal View History

2024-11-15 10:14:00 +08:00
package com.example.demo.kafka.encrypt;
public class Util {
public static String getHexString(byte[] bytes) {
String ret = "";
for (int i = 0; i < bytes.length; i++) {
ret += Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1);
}
return ret.toUpperCase();
}
public static byte[] hexStringToBytes(String hexString)
{
if (hexString == null || hexString.equals(""))
{
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++)
{
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c)
{
return (byte) "0123456789ABCDEF".indexOf(c);
}
}