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

一道编解码问题
补全// fill the code here
中的代码,以下程序是一个信息编码的程序,阅读其encode部分,并补全其decode部分
最后运行程序,会打印出的一句话。
Java code
public class Test {

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

        int seed = password ^ 0x985729e7;
        for (int i = 0; i < len; ++i) {
            byte a = (byte) ((in[i] ^ seed) >>> 3);
            byte b = (byte) (((((int) in[i]) << 17) ^ seed) >>> (17 - 5));
            a &= 0x1f;
            b &= 0xe0;
            out[i] = (byte) (a | b);
            seed = (seed * 608347 + in[i]);
        }
    }

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

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

    public static void main(String[] args) throws Exception {
        int password = 0x2a7bc155;
        byte[] buf1 = { -81, -104, -116, 107, 73, -112, 71, 71, 14, 112, -118,
                -32, -83, -13, -101, -86, 24, -109, 0, -2, 74, 68, 84, -1, 59,
                25, 24, -84, 10, -95, 66, -25, };
        byte[] buf2 = new byte[buf1.length];
        decode(buf1, buf2, password);
        System.out.println(new String(buf2, "GBK"));
    }

}



------解决方案--------------------
这都第4次出现这个题了吧。。。又是搜狗的?
http://topic.csdn.net/u/20110914/20/491492c0-da5e-4fbc-8144-84851a64cf67.html
------解决方案--------------------
public static void decode(byte[] in, byte[] out, int password) {
int len = in.length;

int seed = password ^ 0xd5dbf0dc;
for (int i = 0; i < len; ++i) {
// fill the code here
byte a = (byte) (in[i] & 0xf);
byte b = (byte) (in[i] & 0xf0);
a = (byte) (((a << 4) ^ seed) & 0xf0);
b = (byte) (((b << (13-4) ^ seed) >> 13) & 0x0f);
out[i] = (byte) (a | b);
seed = (((seed << 7) ^ seed ^ out[i]) + 1792013);

}
}