import java.io.*;
import java.security.*;
public class Util {
/************************************************************
Function: // byteArrayToHexString(byte[] b)
Description: // 字節型數組轉化爲對應的16進制字符串
Input: // b爲帶轉化的byte[]
Output: // 轉化後的16進制字符串
Return: // String
*************************************************************/
public static String byteArrayToHexString(byte[] b){
StringBuffer sb = new StringBuffer(b.length * 2);
for (int i = 0; i < b.length; i++) {
int v = b[i] & 0xff;
if (v < 16) {
sb.append('0'); }
sb.append(Integer.toHexString(v));}
return sb.toString().toUpperCase(); }
/************************************************************
Function: // hexStringToByteArray(String s)
Description: // 16進制字符串轉化成對應的字節型數組
Input: // s爲待轉化的String
Output: // 轉化後的byte[]
Return: // byte[]
*************************************************************/
public static byte[] hexStringToByteArray(String s) {
System.out.println("=>"+s);
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2)
{ data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
}
return data;
}
}
/************************************************************
Function: // XOR(byte[] src1,byte[] src2)
Description: // XOR異或操作
Input: // byte[] src1,byte[] src2
Output: // 異或後的byte[]
Return: // byte[]
*************************************************************/
public static byte[] XOR(byte[] src1,byte[] src2){
byte[] temp=new byte[src1.length];
for(int i=0;i<src1.length;i++){
temp[i]=(byte) (src1[i]^src2[i]);
}
return temp;
}