搜狗2012校園招聘自測題

Figo發表於2020-04-04
搜狗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"));
	}
}

執行結果如下:

搜狗雲輸入法是一款領先的概念性產品!!!!


相關文章