[置顶] 搜狗2012校园招聘自测题

搜狗2012年校园招聘的在线自测题,每人只有三次机会,每次的内容不同,但是原理一致,其实就是根据一段字符串的加密写出解密算法,分析出原文内容,下为Java代码:
public class Test {

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

		int seed = password ^ 0x1e1ec1cb;
		for (int i = 0; i < len; ++i) {
			byte a = (byte) ((in[i] ^ seed) >>> 4);
			byte b = (byte) (((((int) in[i]) << 13) ^ seed) >>> (13 - 4));
			a &= 0xf;
			b &= 0xf0;
			out[i] = (byte) (a | b);
			seed = ((seed ^ out[i]) * 608347 + out[i]);
		}
	}

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

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

			// 想办法循环左移4位
			byte a = (byte) ((in[i]) << 4); // //有效位剩余4位
			byte b = (byte) ((in[i]) >>> 4); // //有效位剩余4位

			a = (byte) (a ^ seed); // 还原
			a &= 0xf0; // 无效位再次置0,因为有可能,经过异或之后变成1了
			b = (byte) (((((int) b) << 13) ^ seed) >>> 13); // 还原
			b &= 0xf; // //无效位再次置0,因为有可能,经过异或之后变成1了

			out[i] = (byte) (a | b);
			seed = ((seed ^ in[i]) * 608347 + in[i]);
		}
	}

	public static void main(String[] args) throws Exception {
		int password = 0xd397e77b;
		byte[] buf1 = { 39, 0, -50, 112, 92, 125, -77, 27, 89, 117, -6, -120,
				-70, 26, -102, -12, -115, 2, 15, -51, 87, -110, 10, -85, -77,
				-120, 22, -94, -120, 88, 88, 26, 106, 121, -85, -79, 84, -12,
				-96, -70, -36, -84, };
		byte[] buf2 = new byte[buf1.length];
		decode(buf1, buf2, password);
		System.out.println(new String(buf2, "GBK"));
	}
}

运行结果如下:

搜狗云输入法是一款领先的概念性产品!!!!


你可能感兴趣的:([置顶] 搜狗2012校园招聘自测题)