您的位置:首页 > 理论基础 > 计算机网络

搜狗网络评测 java 移位编码解码

2011-09-19 21:58 375 查看
 

搜狗网络评测 java 移位编码解码
2011-09-09 12:47

很遗憾的是,我当晚没有做出来,第二天早上才搞清楚的,交了白卷:

// 注意:a^(a^b) = b

public class Test {

public static void encode(byte[] in, byte[] out, int password) {

int len = in.length;

int seed = password ^ 0xb1c07965;

for (int i = 0; i < len; ++i) {

byte a = (byte) ((in[i] ^ seed) >>> 5); // in[i]与seed异或后,把最低八位的高三位右移变成低3位

byte b = (byte) (((((int) in[i]) << 20) ^ seed) >>> (20 - 3)); // in[i]与seed的第21位开始异或后,把异或结果右移17位,即把异或后in[i]的低五位变成了高五位

a &= 0x7; // 取出a的低3三 即 异或结果的高三位

b &= 0xf8; // 取出b的高五位 即 异或结果的低五位

out[i] = (byte) (a | b); // 或预算后结果,其实是异或结果把高三位和低五位互换位置

seed = ((seed ^ in[i]) * 48475829 + in[i]);

}

}

public static void decode(byte[] in, byte[] out, int password) {

int len = in.length;

int seed = password ^ 0xb1c07965;

for (int i = 0; i < len; ++i) {

// fill the code here

byte a = (byte) ((in[i] & 0x07) << 5); // 把密文的第三位变成高三位

byte b = (byte) ((in[i] & 0xf8) >>> 3); // 把密文的高五位变成低五位

a = (byte) (a ^ seed); // 求出加密异或前in[i]的高三位

a &= 0xe0; // 求出加密异或前in[i]的高三位

b = (byte) (((((int)b) << 20) ^ seed) >>> 20); // 求出加密异或前in[i] 的低五位

b &= 0x1f; // 求出加密异或前in[i] 的低五位 注意:要和seed的第21位开始异或

out[i] = (byte) (a | b); // 重组高三位和低五位得到一个完整的byte

seed = ((seed ^ out[i]) * 48475829 + out[i]);

}

}

public static void main(String[] args) throws Exception {

int password = 0x38431c82;

byte[] buf1 = { -103, 121, -6, 20, -50, -107, -30, -122, -8, 13, 118,

54, -117, -84, -17, -75, 45, -7, 116, -13, -63, -27, -124, -26,

-75, 108, 80, -87, 47, -122, -44, 117, -16, 29, 21, 104, -15,

109, -60, -16, -114, -30, 109, -38, 46, -47, -2, 19, 5, -112,

-108, 33, 15, -25, 55, -58, -14, 73, 59, 38, -29, 58, -14, 80,

-23, 28, 102, -2, 44, -90, -59, 24, 37, -37, 18, -47, 87, 23, };

byte[] buf2 = new byte[buf1.length];

decode(buf1, buf2, password);

System.out.println(new String(buf2, "GBK"));

}

}

运行结果:搜狗浏览器是目前速度最快、最稳定、最安全、功能最强大的“双核”浏览器!!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息