搜狗的一个笔考题

搜狗的一个笔试题

以下程序是一个信息编码的程序,阅读其encode部分,并补全其decode部分
最后运行程序,会打印出的一句话。这句话就是我们要求的答案。

注意!这句话是用GBK编码的!
题目code如下:

package com.db;

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

		int seed = password ^ 0x6466913d;
		for (int i = 0; i < len; ++i) {
			byte a = (byte) ((in[i] ^ seed) >>> 3);
			byte b = (byte) (((((int) in[i]) << 13) ^ seed) >>> (13 - 5));
			a &= 0x1f;
			b &= 0xe0;
			out[i] = (byte) (a | b);
			seed = (((seed << 7) ^ seed ^ in[i]) + 144123481);
		}
	}

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

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

	public static void main(String[] args) throws Exception {
		int password = 0xe9479a3c;
		byte[] buf1 = { 121, -82, 126, -49, 48, -10, -41, -37, -97, 31, -128,
				113, -107, 88, -124, -37, -2, -68, 94, 38, 89, -39, -66, 39,
				88, -66, -2, -31, -37, 83, -124, 104, -101, -128, 3, -118, -80,
				-125, 25, -31, -91, 55, 104, 102, -8, -108, -69, -126, 73, -48, };
		byte[] buf2 = new byte[buf1.length];
		decode(buf1, buf2, password);
		System.out.println(new String(buf2, "GBK"));
	}

}

 

 

 

 答案如下:

package com.db;

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

		int seed = password ^ 0x6466913d;
		for (int i = 0; i < len; ++i) {
			byte a = (byte) ((in[i] ^ seed) >>> 3);
			byte b = (byte) (((((int) in[i]) << 13) ^ seed) >>> (13 - 5));
			a &= 0x1f;
			b &= 0xe0;
			out[i] = (byte) (a | b);
			seed = (((seed << 7) ^ seed ^ in[i]) + 144123481);
		}
	}

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

		int seed = password ^ 0x6466913d;
		for (int i = 0; i < len; ++i) {
			// fill the code here
			byte a = (byte) (in[i] & 0x1f); // a低五位,实际上是高五位
			byte b = (byte) ((in[i] & 0xe0));// b高三位,实际上是低三位
			b = (byte) (((((int) b) << 8) ^ seed) >>> 13);
			b = (byte) (b & 0x7);
			a = (byte) (((a << 3) ^ seed) & 0xf8);
			out[i] = (byte) (a | b);
			seed = (((seed << 7) ^ seed ^ out[i]) + 144123481);

		}
	}

	public static void main(String[] args) throws Exception {
		int password = 0xe9479a3c;
		byte[] buf1 = { 121, -82, 126, -49, 48, -10, -41, -37, -97, 31, -128,
				113, -107, 88, -124, -37, -2, -68, 94, 38, 89, -39, -66, 39,
				88, -66, -2, -31, -37, 83, -124, 104, -101, -128, 3, -118, -80,
				-125, 25, -31, -91, 55, 104, 102, -8, -108, -69, -126, 73, -48, };
		byte[] buf2 = new byte[buf1.length];
		decode(buf1, buf2, password);
		System.out.println(new String(buf2, "GBK"));
	}

}

 

1 楼 韩悠悠 2011-12-15  
[list]
[*]
引用
[u][/u][i][/i][b][/b]
引用

  • [*]
[img][/img][url][/url][flash=200,200][/flash][*][*]
引用
[/list]搜狗的一个笔考题