日期:2014-05-20  浏览次数:20746 次

瞅瞅这个位运算题
public static void encode(byte[] in, byte[] out, int password) {
int len = in.length;

int seed = password ^ 0x1963f9c;
for (int i = 0; i < len; ++i) {
byte a = (byte) ((in[i] ^ seed) >>> 2);
byte b = (byte) (((((int) in[i]) << 13) ^ seed) >>> (13 - 6));
a &= 0x3f;
b &= 0xc0;
out[i] = (byte) (a | b);
seed = (((seed << 7) ^ seed ^ in[i]) + 5393887);
}
}

public static void decode(byte[] in, byte[] out, int password) {
int len = in.length;

int seed = password ^ 0x1963f9c;
for (int i = 0; i < len; ++i) {

byte a = (byte) ((( (byte) (in[i] & 0x3f) << 2) ^ seed) & 0xfc);
byte b = (byte) ((((((int) (byte) (in[i] & 0xc0)) << (13 - 6)) ^ seed) >> 13) & 0x03);

out[i] = (byte) (a | b);
seed = (((seed << 7) ^ seed ^ out[i]) + 5393887);

}
}

public static void main(String[] args) throws Exception {
int password = 0x99b90094;
byte[] buf1 = { 113, 87, 1, -29, 55, -5, 65, 13, -85, 78, 50, 23, -33,
-29, 73, 117, -97, 106, 77, -17, -107, -62, -110, -126, -15,
32, -48, -128, };
byte[] buf2 = new byte[buf1.length];
decode(buf1, buf2, password);
System.out.println(new String(buf2, "GBK"));
}

疑问标红色部分的是怎么算出来的啊?直接反操作结果不对,麻烦大家帮瞅瞅

------解决方案--------------------
Java code

byte a = (byte) ((( (byte) (in[i] & 0x3f) << 2) ^ seed) & 0xfc);
byte b = (byte) ((((((int) (byte) (in[i] & 0xc0)) << (13 - 6)) ^ seed) >> 13) & 0x03);