工作中可能会有对数据进行异或操作来实现加密解密等操作,最近遇见了并实现了一下,在此记录一下。
当数据传来的时候,对数据进行分组,然后使用
ArrayList<byte[]> bodys_list=new ArrayList<>();
来进行保存,这样可以把传过来的数据进行分类储存,然后进行异或时可以从里面进行取值。
例如:
public static byte[] get_HMAC(byte[] receive_message){
ArrayList<byte[]> bodys_list=new ArrayList<>();
bodys_list = get_msg_by_page(receive_message);
int num = bodys_list.size();//一共有几个64;
int i = 0;
byte temp1;
byte[] hout = new byte[64];
byte[] hout1 = new byte[64];
byte[] temp = bodys_list.get(0);
int num1 =64;
if(num > 1) {
for (i = 1; i < num; i++) {
hout = bodys_list.get(i);
for (int j = 0; j < num1; j++) {
hout1[j] = (hout[j] ^= temp[j]);
}
}
}else {
for (int j = 0; j < num1; j++) {
hout1[j] = (hout[j] ^= temp[j]);
}
}
byte[]hout2 = new byte[8];
System.arraycopy(hout1, 0, hout2, 0, 8);
System.out.println("获取HMAC成功!");
return hout2;
}
这样的话就实现对数据进行异或操作。