读书人

搜狗面试题,该怎么处理

发布时间: 2012-01-19 20:57:58 作者: rapoo

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

注意!这句话是用GBK编码的!
答案请复制粘贴, 不要手动输入
每次重新登录时请重新答题


public class Test {


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

int seed = password ^ 0xe84f172f;
for (int i = 0 ; i < len; ++i) {
byte a = (byte)( ( in[i] ^ seed ) >>> 5 );
byte b = (byte)( ( ( ((int)in[i]) << 13 ) ^ seed ) >>> (13-3) );
a &= 0x7;
b &= 0xf8;
out[i] = (byte)(a | b);
seed = (seed * 5393887 + in[i]);
}
}


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

int seed = password ^ 0xe84f172f;
for (int i = 0 ; i < len; ++i) {
// fill the code here
}
}
public static void main(String [] args) throws Exception
{
int password = 0xea5c3443;
byte[] buf1 = {-107, 88, 37, 78, -35, 5, 60, 6, -86, 36, -105, 43, 99, -83, -1, -82, -103, -3, 36, -124, -100, 62, -7, -42, -71, 119, 82, 41, -89, 95, 59, -31, -84, 43, 85, 44, };
byte[] buf2 = new byte[buf1.length];
decode(buf1, buf2, password);
System.out.println(new String(buf2, "GBK"));
}


}


[解决办法]

Java code
public class Test {    public static void encode(byte[] in, byte[] out, int password) {    int len = in.length;    int seed = password ^ 0xe84f172f;    for (int i = 0; i < len; ++i) {        byte a = (byte) ((in[i] ^ seed) >>> 5);        byte b = (byte) (((((int) in[i]) << 13) ^ seed) >>> (13 - 3));        a &= 0x7;        b &= 0xf8;        out[i] = (byte) (a | b);        seed = (seed * 5393887 + in[i]);    }    }    public static void decode(byte[] in, byte[] out, int password) {    int len = in.length;    int seed = password ^ 0xe84f172f;    for (int i = 0; i < len; ++i) {        // fill the code here        byte a = (byte) ((in[i] & 0x07) << 5); // 把密文的第三位变成高三位        //第0位到第2位置0,右移3位,b中第0位到第4位是有效位        byte b = (byte) ((in[i] & 0xf8) >>> 3); // 把密文的高五位变成低五位        a = (byte) (a ^ seed); // 还原        a &= 0xe0; //无效位再次置0,因为有可能,经过异或之后变成1了        b = (byte) (((((int) b) << 13) ^ seed) >>> 13); // 还原        b &= 0x1f; ////无效位再次置0,因为有可能,经过异或之后变成1了         out[i] = (byte) (a | b); //将临时变量中的数据串接,保存到out[i],这是真实的数据来了,        seed = (seed * 5393887 + out[i]);    }    }    public static void main(String[] args) throws Exception {    int password = 0xea5c3443;    byte[] buf1 = { -107, 88, 37, 78, -35, 5, 60, 6, -86, 36, -105, 43, 99,        -83, -1, -82, -103, -3, 36, -124, -100, 62, -7, -42, -71, 119,        82, 41, -89, 95, 59, -31, -84, 43, 85, 44, };    byte[] buf2 = new byte[buf1.length];    decode(buf1, buf2, password);    System.out.println(new String(buf2, "GBK"));    }}/* 搜狗输入法皮肤拥有数万款炫酷皮肤!! */ 

读书人网 >J2SE开发

热点推荐