public static void main(String[] args) {
// TODO Auto-generated method stub
String s="F648E81AB0";
String str="";
int XorKey[] = {0xB2, 0x09, 0xAA, 0x55, 0x93, 0x6D, 0x84, 0x47};
// int XorKey[] = {178, 9, 170, 85, 147, 109, 132, 71};
int a=(s.length())/2;
int j=0;
for(int i=1;i<=a;i++){
str = str + s.charAt( Integer.parseInt("0x"+s.substring(i * 2 - 2,2),16) ^ XorKey[j]); j = (j + 1) % 8;
}
System.out.println(str);
}
我自己写的一个解密的方法、把字符串S的值解密出来,但是一直过不去、试了很多种很方法、都没办法。
大概原理就是把字符串循环一个一个解密,首先取2字符的16进制对应的10进制值异或获得另外一个值的10进制之后取对应值的ASCII码、最后解密出来。。对JAVA了解尚且、求大神解答,感谢
// TODO Auto-generated method stub
String s="F648E81AB0";
String str="";
int XorKey[] = {0xB2, 0x09, 0xAA, 0x55, 0x93, 0x6D, 0x84, 0x47};
// int XorKey[] = {178, 9, 170, 85, 147, 109, 132, 71};
int a=(s.length())/2;
int j=0;
for(int i=1;i<=a;i++){
str = str + s.charAt( Integer.parseInt("0x"+s.substring(i * 2 - 2,2),16) ^ XorKey[j]); j = (j + 1) % 8;
}
System.out.println(str);
}
我自己写的一个解密的方法、把字符串S的值解密出来,但是一直过不去、试了很多种很方法、都没办法。
大概原理就是把字符串循环一个一个解密,首先取2字符的16进制对应的10进制值异或获得另外一个值的10进制之后取对应值的ASCII码、最后解密出来。。对JAVA了解尚且、求大神解答,感谢
这个好像应该改成这样,不知道对不对
str = str + s.charAt( Integer.parseInt(s.substring(i * 2 - 2,2),16) ^ XorKey[j]);
不过这样还是有错,s.charAt(index)的下标越界了,
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "F648E81AB0";
String str = ""; int XorKey[] = { 0xB2, 0x09, 0xAA, 0x55, 0x93, 0x6D, 0x84, 0x47 };
// int XorKey[] = {178, 9, 170, 85, 147, 109, 132, 71};
int a = s.length();
int j = 0; for (int i = 0; i < a; i += 2) {
int x = getIntFromChar(s.charAt(i));
int y = getIntFromChar(s.charAt(i + 1)); if (x < 0 || y < 0) {
System.out.println("数据有误!");
break;
}
int z = (x << 4) + y;// 相当于x*16 + y.
int z1 = z ^ XorKey[j];
str += z1;//以10进制形式表示。
//str += Integer.toHexString(z1);//16进制表示
j = (j + 1) % 8;
}
System.out.println(str);
} /**
* 把16进制表示的字符,换成整数。'A'('a') --> 10, 'F'('f')--> 15.
* '0'-->0,'1'-->1 ......
* @param a
* @return
*/
public static int getIntFromChar(char a) {
int result = 0;
if (a >= 'A' && a <= 'F') {
result = a - 'A' + 10;
} else if (a >= 'a' && a <= 'f') {
result = a - 'a' + 10;
} else if (a >= '0' && a <= '9') {
result = a - '0';
} else {
System.out.println("给定值 a 不合法!");
result = -1;
}
return result;
}
}