前提
50版本的解密算法
具体代码
System.out.println(unpackageHEX("~50031bd0e8003e0000020102030405060708090a0b0c0c0f0f101112131415161718191a1b1c1d1e1f202122232425262728292a393b281e2f303132996135639d38393a3b82"));
public static Boolean unpackageHEX(String hex) {
System.out.println(hex);
int[] m_recv_buf = new int[1024];
String[] m_recv_buf_hex = new String[1024];
m_recv_buf[0] = '~';
m_recv_buf_hex[0] = "~";
int j = 1;
for (int i = 0 ; i <(hex.length()-1)/2; i++) {
m_recv_buf_hex[i+1] = hex.substring(j,j+2);
m_recv_buf[i+1] = Integer.parseInt(hex.substring(j,j+2),16);
j+=2;
}
String t1[] = Arrays.copyOfRange(m_recv_buf_hex, 0, (hex.length()-1)/2+1);
int t2[] = Arrays.copyOfRange(m_recv_buf, 0, (hex.length()-1)/2+1);
m_recv_buf_hex = t1;
m_recv_buf = t2;
System.out.println(Arrays.toString(m_recv_buf_hex));
System.out.println(Arrays.toString(m_recv_buf));
int[] hex_recv =new int[1024];
int lenth = m_recv_buf.length;
int i;
int enc_l = 0, enc_h = 0, enc_key;
enc_key = (byte)(enc_l + enc_h);
enc_key = (byte)(~enc_key + 1);
int ii = 0;
int v1, v2;
int chksum = 0;
int chk = 0;
int offet = 0;
//todo:这里存在问题,去掉eoi 和校验和即可。2个字节。于亚注释错误。
for (i = 1; i < lenth - 1; i++) {
v1 = m_recv_buf[i];
chksum += v1;
chksum = chksum <0?chksum & 0xff : chksum;
hex_recv[offet++] = v1;
if (i == 9)
{
enc_l = hex_recv[7];
enc_h = hex_recv[8];
enc_key = (enc_l + enc_h);
enc_key = (~enc_key + 1);
}
if (i > 9 && hex_recv[3] == 0xd0)
{
hex_recv[offet-1] ^= enc_key;
enc_key++;
}
}
v1 = m_recv_buf[i];
chk = v1;
hex_recv[offet++] = chk;
hex_recv[offet++] = 0x0d;
chksum = ~chksum;
chksum = chksum <0?chksum & 0xff : chksum;
chksum ++;
chksum = chksum <0?chksum & 0xff : chksum;
System.out.println("期望:"+Integer.toHexString(chk));
System.out.println("计算:"+Integer.toHexString(chksum));
if (chk == chksum){
int b[] = Arrays.copyOfRange(hex_recv, 0, offet);
System.out.println(Arrays.toString(b));
System.out.println(intArrayToHex(b));
return true;
}
else
return false;
}
public static String intArrayToHex(int[] a) {
StringBuilder sb = new StringBuilder(a.length * 2);
for(int b: a)
sb.append(String.format("%02x", b));
return sb.toString();
}
网友评论