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

一道JAVA搜狗在现测评题目,大家看看哪里出问题了,谢谢
public class Test {

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

int seed = password ^ 0x329b8425;
for (int i = 0; i < len; ++i) {
byte a = (byte) ((in[i] ^ seed) >>> 1);
byte b = (byte) (((((int) in[i]) << 16) ^ seed) >>> (16 - 7));
a &= 0x7f;
b &= 0x80;
out[i] = (byte) (a | b);
seed = (((seed << 7) ^ seed ^ out[i]) + 608347);
}
}

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

int seed = password ^ 0x329b8425;
for (int i = 0; i < len; ++i) {
// fill the code here

byte a = (byte)(in[i] & 0x7f);
byte b = (byte)(in[i] & 0x80);
a = (byte)(((a << 1) ^ seed) & 0xfc);
b = (byte)((((((int)b) << (16-7)) ^ seed) >> 16) & 0x03);
out[i] = (byte)(a | b);
seed = (((seed << 7) ^ seed ^ in[i]) + 608347);  
}
}

public static void main(String[] args) throws Exception {
int password = 0xa700adcb;
byte[] buf1 = { 42, -44, -70, 109, -92, 127, -77, 73, -93, -83, -99,
102, -22, -20, -90, 66, 49, 53, 61, -113, 84, -51, };
byte[] buf2 = new byte[buf1.length];
decode(buf1, buf2, password);
System.out.println(new String(buf2, "GBK"));
}

}


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

public class Test {

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

    int seed = password ^ 0x329b8425;
    for (int i = 0; i < len; ++i) {
        byte a = (byte) ((in[i] ^ seed) >>> 1);
        byte b = (byte) (((((int) in[i]) << 16) ^ seed) >>> (16 - 7));
        a &= 0x7f;
        b &= 0x80;
        out[i] = (byte) (a | b);
        seed = (((seed << 7) ^ seed ^ out[i]) + 608347);
    }
    }

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

    int seed = password ^ 0x329b8425;
    for (int i = 0; i < len; ++i) {
        // fill the code here
        byte a = (byte) ((in[i]) << 1); //低位自动补0                    
        byte b = (byte) ((in[i]) >>> 7);//高位自动补0
        a = (byte) (a ^ seed);
        a &= 0xfe;//清除第0位
        b = (byte) (((((int) b) << 16) ^ seed) >>> 16);
        b &= 0x01;//清除第1位到第7位
        out[i] = (byte) (a | b);
        seed = (((seed << 7) ^ seed ^ in[i]) + 608347);

    }
    }

    public static void main(String[] args) throws Exception {
    int password = 0xa700adcb;
    byte[] buf1 = { 42, -44, -70, 109, -92, 127, -77, 73, -93, -83, -99,
        102, -22, -20, -90, 66, 49, 53, 61, -113, 84, -51, };
    byte[] buf2 = new byte[buf1.length];
    decode(buf1, buf2, password);
    System.out.println(new String(buf2, "GBK"));
    }
}
/*output:
欢迎加入搜狗!!!!!
*/