package BaoWen;
|
import java.math.BigInteger;
|
public class Jiaoyan {
|
|
/**»ñȡУÑéÂë³ýÈ¥°üÍ·Ïà¼ÓÈ¡·´*/
|
public static byte[] check(byte[] byt) {
|
|
//×Ö½ÚÊý×éתΪ16½øÖÆ×Ö·û´®/2ºóµÄ³¤¶È
|
int size=BinaryToHexString2(byt).length()/2;
|
String[] hex=hex(BinaryToHexString2(byt));
|
|
//ÇóºÍ
|
int sum=0;
|
for(int i=0;i<size;i++) {
|
sum+=decodeHEX(hex[i]);
|
}
|
|
//È¡·´×ªÎª16½øÖÆ×Ö·û´®
|
String hex16=Integer.toHexString(~sum);
|
|
//×Ö·û´®µÄ³¤¶È
|
int lenth=hex16.length();
|
|
byte[] jiaoyan=hexStringToByteArray(hex16.substring(lenth-4, lenth));
|
|
return jiaoyan;
|
|
}
|
|
|
/**»ñȡУÑéÂë³ýÈ¥°üÍ·Ïà¼ÓÈ¡·´*/
|
public static String check2(String byt) {
|
|
//×Ö½ÚÊý×éתΪ16½øÖÆ×Ö·û´®/2ºóµÄ³¤¶È
|
int size=byt.length()/2;
|
String[] hex=hex(byt);
|
|
//ÇóºÍ
|
int sum=0;
|
for(int i=2;i<size-2;i++) {
|
sum+=decodeHEX(hex[i]);
|
}
|
|
//È¡·´×ªÎª16½øÖÆ×Ö·û´®
|
String hex16=Integer.toHexString(~sum);
|
|
//×Ö·û´®µÄ³¤¶È
|
int lenth=hex16.length();
|
|
String jiaoyan=hex16.substring(lenth-4, lenth);
|
|
return jiaoyan.toUpperCase();
|
|
}
|
|
/**16½øÖÆ×ªÎª10½øÖÆ*/
|
public static int decodeHEX(String hexs){
|
hexs=hexs.replaceAll("\r\n|\r|\n", "");//È¡³öËùÓÐQÐкͻسµ
|
BigInteger bigint=new BigInteger(hexs, 16);
|
int numb=bigint.intValue();
|
return numb;
|
|
}
|
|
|
/**½«16½øÖÆ×Ö·û´®×ªÎªhex×Ö·û´®Êý×é2¸ö×Ö·û´®Ò»¸ö*/
|
public static String[] hex(String message) {
|
int size=message.length()/2;
|
String[] hex=new String[size];
|
for(int i=0;i<size;i++) {
|
hex[i]=message.substring(i*2, 2+i*2);
|
}
|
|
return hex;
|
}
|
|
|
/**½«×Ö½ÚÊý×éת»»Îª16½øÖÆ×Ö·û´®ÎÞ¿Õ¸ñ*/
|
public static String BinaryToHexString2(byte[] bytes) {
|
String hexStr = "0123456789ABCDEF";
|
String result = "";
|
String hex = "";
|
for (byte b : bytes) {
|
hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
|
hex += String.valueOf(hexStr.charAt(b & 0x0F));
|
result += hex + "";
|
}
|
return result;
|
}
|
|
|
/**
|
* 16½øÖƱíʾµÄ×Ö·û´®×ª»»Îª×Ö½ÚÊý×é
|
*
|
* @param hexString 16½øÖƱíʾµÄ×Ö·û´®
|
* @return byte[] ×Ö½ÚÊý×é
|
*/
|
public static byte[] hexStringToByteArray(String hexString) {
|
hexString = hexString.replaceAll(" ", "");
|
int len = hexString.length();
|
byte[] bytes = new byte[len / 2];
|
for (int i = 0; i < len; i += 2) {
|
// Á½Î»Ò»×飬±íʾһ¸ö×Ö½Ú,°ÑÕâÑù±íʾµÄ16½øÖÆ×Ö·û´®£¬»¹Ô³ÉÒ»¸ö×Ö½Ú
|
bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
|
.digit(hexString.charAt(i+1), 16));
|
}
|
return bytes;
|
}
|
|
}
|